2

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,)?

kwotsin
  • 2,642
  • 8
  • 27
  • 55
  • 3
    http://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r?rq=1 – Sohrab T Jul 11 '16 at 06:32
  • _"What can't I just use a dot product with a vector of shape (5,)"_ - you **can** do just that – Eric Jul 11 '16 at 07:14

2 Answers2

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,228
  • 1
  • 16
  • 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
  • 19,186
  • 6
  • 55
  • 76