[SciPy-User] Basic Question About numpy.dot

Åsmund Hjulstad asmund.hjulstad at gmail.com
Sun Dec 12 08:41:38 EST 2010


2010/12/12 Scott Stephens <stephens.js at gmail.com>

> Why does this work:
>
> >>> import numpy as np
> >>> np.dot(np.array([1.,2.]),np.array([[10.,20.],[0.1,0.2]]))
> array([ 10.2,  20.4])
>
> But this doesn't:
>
> >>> import numpy as np
> >>> np.dot(np.array([1.,2.]),np.array([[10.]]))
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> ValueError: matrices are not aligned
>
>
For 2D matrices, np.dot is matrix multiplication, so the length of the first
dimension (rows) of the first array must match the length of the second
dimension (columns) of the second array.

In your first example you have arrays with shape (2,), and (2,2) so the
dimensions match.  Your second example has dimensions (2,) and (1,1), so
they don't match. (The matrices are not aligned.)

Hope it answers your question. (You may also look at "help np.dot")

Åsmund Hjulstad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20101212/1273ec16/attachment.html>


More information about the SciPy-User mailing list