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,)?
2 Answers
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.]]) >>> Comments
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.])