numpy的矩阵向量乘法 [英] numpy matrix vector multiplication
问题描述
当我乘到numpy的同大小的数组为(N×N)*(N×1)我得到大小为(N×N)的矩阵。继普通矩阵乘法一个(N×1)向量的预期,但我根本无法找到有关如何在Python的numpy的模块完成的任何信息。
的事情是,我不想手动执行它preserve程序的速度。
举例code如下:
A = np.array([5,1,3],[1,1,1],[1,2,1])
B = np.array([1,2,3])打印* B
>>
[5 2 9]
[1 2 3]
[1 4 3]
我要的是:
打印A * B
>>
[16 6 8]
使用 numpy.dot
或 a.dot(B)
。这里查看文档的。
这是因为numpy的数组不是矩阵,标准作业 *,+, - ,/
工作逐元素的数组。相反,你可以尝试使用<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html\"><$c$c>numpy.matrix$c$c>,和 *
将像矩阵乘法处理。
When I multiply to numpy arrays with the sizes (n x n)*(n x 1) I get a matrix of size (n x n). Following normal matrix multiplication a (n x 1) vector is expected, but I simply cannot find any information about how this is done in Python's Numpy module.
The thing is that I don't want to implement it manually to preserve the speed of the program.
Example code is shown below:
a = np.array([[ 5, 1 ,3], [ 1, 1 ,1], [ 1, 2 ,1]])
b = np.array([1, 2, 3])
print a*b
>>
[[5 2 9]
[1 2 3]
[1 4 3]]
What i want is:
print a*b
>>
[16 6 8]
Use numpy.dot
or a.dot(b)
. See the documentation here.
This occurs because numpy arrays are not matrices, and the standard operations *, +, -, /
work element-wise on arrays. Instead, you could try using numpy.matrix
, and *
will be treated like matrix multiplication.
这篇关于numpy的矩阵向量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!