Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - Numpy: 1D array with various shape

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.]])
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Notice you are not only working with 1D arrays:

In [6]: a.ndim
Out[6]: 1

In [7]: b.ndim
Out[7]: 2

So, b is a 2D array. You also see this in the output of b.shape: (1,3) indicates two dimensions as (3,) is one dimension.

The behaviour of np.dot is different for 1D and 2D arrays (from the docs):

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors

That is the reason you get different results, because you are mixing 1D and 2D arrays. Since b is a 2D array, np.dot(b, b) tries a matrix multiplication on two 1x3 matrices, which fails.


With 1D arrays, np.dot does a inner product of the vectors:

In [44]: a = np.array([1,2,3])

In [45]: b = np.array([1,2,3])

In [46]: np.dot(a, b)
Out[46]: 14

In [47]: np.inner(a, b)
Out[47]: 14

With 2D arrays, it is a matrix multiplication (so 1x3 x 3x1 = 1x1, or 3x1 x 1x3 = 3x3):

In [49]: a = a.reshape(1,3)

In [50]: b = b.reshape(3,1)

In [51]: a
Out[51]: array([[1, 2, 3]])

In [52]: b
Out[52]:
array([[1],
       [2],
       [3]])

In [53]: np.dot(a,b)
Out[53]: array([[14]])

In [54]: np.dot(b,a)
Out[54]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [55]: np.dot(a,a)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-32e36f9db916> in <module>()
----> 1 np.dot(a,a)

ValueError: objects are not aligned

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...