I find that I often have to reshape (5,) into (5,1) in order to use dot product. What can't I just use a dot product with a vector of shape (5,)?
Asked
Active
Viewed 3,560 times
2 Answers
4
To use a dot product, you need matrices (represented with 2D arrays). Array with dimension (5,) is a flat array (1D array) of 5 items, where as (5, 1) is matrix with 1 column and 5 rows.
>>> import numpy as np
>>> np.zeros((5,))
array([ 0., 0., 0., 0., 0.]) # single flat array
>>> np.zeros((1,5))
array([[ 0., 0., 0., 0., 0.]]) # array with-in array
>>> np.zeros((5,1))
array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]])
>>>

emnoor
- 2,528
- 2
- 18
- 15
3
This is because when you create an array with arr = np.ones((5))
, it would get you a 1D array of 5 elements, on the other hand when you create array with arr = np.ones((5, 1))
, it creates a 2D array with 5 rows and 1 column. The following example would make it more clear to you :
>>> import numpy as np
>>> a = np.ones((5, 1))
>>> a
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
>>> a = np.ones((5))
>>> a
array([ 1., 1., 1., 1., 1.])

ZdaR
- 22,343
- 7
- 66
- 87