简体   繁体   English

在numpy.linalg模块中如何实现矩阵/矢量点乘法?

[英]How could this matrix / vector dot multiplication be possible in numpy.linalg module?

The code as below... 代码如下...

>>> A
array([[1, 2],
       [3, 4]])
>>> b
array([5, 6])
>>> A.dot(b)   # <------- this is not possible in math
array([17, 39]) # <------ and the result should be 2x1 matrix instead of 1x2 matrix.
>>> A.dot(b.T) # <------- this is a bit better
array([17, 39]) # <------ but the result is still not well formed. 
>>> 

If you use np.ndarray to store the data instead of matrix , numpy always intends to store data in minimal dimension (don't remember where the doc says that). 如果您使用np.ndarray而不是matrix来存储数据,那么numpy总是打算以最小的维度存储数据(不记得文档在哪里说了)。 That's why bT changes nothing: 这就是bT不变的原因:

In [136]: b
Out[136]: array([5, 6])

In [137]: b.T
Out[137]: array([5, 6])

In [138]: b.T.shape
Out[138]: (2,)

In [139]: b.shape
Out[139]: (2,)

if you want b.shape be (2,1), use np.newaxis or None to slice it: 如果您希望b.shape为(2,1),请使用np.newaxisNone进行切片:

In [140]: bb=b[:,None]

In [141]: bb
Out[141]: 
array([[5],
       [6]])

In [142]: bb.shape
Out[142]: (2, 1)

then everything will looks right: 那么一切都会看起来正确:

In [143]: a.dot(bb)
Out[143]: 
array([[17],
       [39]])

In [144]: a.dot(bb).shape
Out[144]: (2, 1)

this link may help: python-numpy-transpose 此链接可能有帮助: python-numpy-transpose

You need to use numpy.matrix in order to get correct matrix mathematical behaviour: 您需要使用numpy.matrix以获得正确的矩阵数学行为:

In [2]: import numpy

In [3]: A = numpy.matrix('[1 2; 3 4]')

In [4]: A
Out[4]: 
matrix([[1, 2],
        [3, 4]])

In [5]: b = numpy.matrix('[5 6]')

In [6]: b
Out[6]: matrix([[5, 6]])

In [7]: A.dot(b)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-2a3d67ed3e0f> in <module>()
----> 1 A.dot(b)

ValueError: objects are not aligned

In [8]: A.dot(b.transpose())
Out[8]: 
matrix([[17],
        [39]])

You'll also get overloaded multiplication operators for numpy.matrix : 您还将获得numpy.matrix重载乘法运算符:

In [9]: A * b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-e4962082e335> in <module>()
----> 1 A * b

/usr/lib64/python2.7/site-packages/numpy/matrixlib/defmatrix.pyc in __mul__(self, other)
    339         if isinstance(other, (N.ndarray, list, tuple)) :
    340             # This promotes 1-D vectors to row vectors
--> 341             return N.dot(self, asmatrix(other))
    342         if isscalar(other) or not hasattr(other, '__rmul__') :
    343             return N.dot(self, other)

ValueError: objects are not aligned

In [10]: A * b.transpose()
Out[10]: 
matrix([[17],
        [39]])

See also: http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html#numpy.matrix 另请参阅: http : //docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html#numpy.matrix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM