简体   繁体   中英

Numpy dotproduct weird behaviour

I am using python3.5 and i have question: Why np.dot() is behaving like this?

>> a = np.array([[1,2,3,4]])
>> b = np.array([123])
>> np.dot(a,b)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: shapes (1,4) and (1,) not aligned: 4 (dim 1) != 1 (dim 0)
>>np.dot(b,a)
array([123, 246, 369, 492])

From help(np.dot) , we learn that, np.dot(x,y) is a sum product over the last axis of x and the second-to-last of y

In the case of np.dot(a, b) , the last axis of a is 4 and the length of the only axis of b is 1. They don't match: fail.

In the case of np.dot(b, a) , the last axis of b is 1 and the 2nd to last of a is 1. They match: success.

Workarounds

Depending on what your intention is for np.dot(a,b) , you may want:

>>> np.dot(a, np.resize(b,a.shape[-1]))
array([1230])

From the documentation for numpy.dot(x, y) :

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors... For N dimensions it is a sum product over the last axis of x and the second-to-last of y :

So, where you have:

a = np.array([[1,2,3,4]])  # shape is (1, 4), 2-D array (matrix)
b = np.array([123])        # shape is (1,),   1-D array (vector)
  • np.dot(b, a) works ( (1,) * (1, 4) , the relevant dimensions agree)
  • np.dot(a, b) doesn't ( (1, 4) * (1,) , the relevant dimensions don't agree, the operation is undefined. Note that the 'second-to-last' axis of (1,) corresponds to its one and only axis)

This is the same behaviour as if you have two 2-D arrays, ie matrices:

a = np.array([[1,2,3,4]])  # shape is (1, 4)
b = np.array([[123]])      # shape is (1, 1)
  • np.dot(b, a) works ( (1, 1) * (1, 4) , inner matrix dimensions agree)
  • np.dot(a, b) doesn't ( (1, 4) * (1, 1) , inner matrix dimensions don't agree)

If however you have two 1-D arrays, ie vectors, neither operation works:

a = np.array([1,2,3,4])   # shape is (4,)
b = np.array([123])       # shape is (1,)
  • np.dot(b, a) doesn't work ( (1,) * (4,) , but can only define the inner product for vectors of the same length)
  • np.dot(a, b) doesn't work ( (4,) * (1) , same)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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