I try to understand how to handle a 1D
array (vector in linear algebra) with NumPy
.
In the following example, I generate two numpy.array
a
and b
:
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([[1],[2],[3]]).reshape(1,3)
>>> a.shape
(3,)
>>> b.shape
(1, 3)
For me, a
and b
have the same shape according linear algebra definition: 1 row, 3 columns, but not for NumPy
.
Now, the NumPy
dot
product:
>>> np.dot(a,a)
14
>>> np.dot(b,a)
array([14])
>>> np.dot(b,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: objects are not aligned
I have three different outputs.
What's the difference between dot(a,a)
and dot(b,a)
? Why dot(b,b)
doesn't work?
I also have some differencies with those dot products:
>>> c = np.ones(9).reshape(3,3)
>>> np.dot(a,c)
array([ 6., 6., 6.])
>>> np.dot(b,c)
array([[ 6., 6., 6.]])