gpt4 book ai didi

python - NumPy矩阵加列向量

转载 作者:行者123 更新时间:2023-12-04 13:27:53 24 4
gpt4 key购买 nike

我正在使用numpy.matrix。如果我添加一个带有3x31x3,矢量的3x1矩阵,我会得到一个3x3矩阵。

应该不是undefined吗?
如果没有,对此有何解释?

例子:

a = np.matrix('1 1 1; 1 1 1; 1 1 1')
b = np.matrix('1 1 1')
a + b #or a + np.transpose(b)

输出:
matrix([[2, 2, 2],
[2, 2, 2],
[2, 2, 2]])

最佳答案

这称为“广播”。从manual:

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.

关于python - NumPy矩阵加列向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com