2

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. 
>>> 
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 1
    Just think of a one-dimensional vector (like `b`) as a column vector, and everything is fine. Since `b` has only one dimension, it kund of doesn't matter whether it's a row or a column. That's why `b.T` is the same as `b`. – Sven Marnach Jan 12 '14 at 14:05
  • 2
    All of this is documented perfectly well in the [`np.dot` docstring](http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html). – Fred Foo Jan 12 '14 at 14:06
  • I ran in this problem the other month. The only thing that's somewhat better in Matlab (as much as it hurts to admit :P). I use `reshape` to change vectors to column/row ones as appropriate and store them in default configuration. – Aleksander Lidtke Jan 12 '14 at 14:08
  • The behavior is fully consistent with being a tensor product. A_ij b_j. It's not "better" in Matlab, it's just that Matlab does not have 1D tensors. – pv. Jan 13 '14 at 09:23

2 Answers2

3

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). That's why b.T changes nothing:

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:

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

Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
1

You need to use numpy.matrix in order to get correct matrix mathematical behaviour:

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:

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

Joakim Nohlgård
  • 1,832
  • 16
  • 16
  • But the scipy reference doc(http://docs.scipy.org/doc/scipy/scipy-ref.pdf) says "*...Despite its convenience, the use of the numpy.matrix class is discouraged, since it adds nothing that cannot be accomplished with 2D numpy.ndarray objects...*". I simply cannot believe they ignore this apparent bug. – smwikipedia Jan 12 '14 at 14:00
  • 2
    @smwikipedia not a bug but a feature – alko Jan 12 '14 at 14:08
  • 5
    `np.matrix` is evil. It throws several of NumPy's conventions overboard and causes all kinds of trouble for library authors (including yours truly). – Fred Foo Jan 12 '14 at 14:09