3

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
  • 3
    stackoverflow.com/questions/22053050/… Commented Jul 11, 2016 at 6:32
  • "What can't I just use a dot product with a vector of shape (5,)" - you can do just that Commented Jul 11, 2016 at 7:14

2 Answers 2

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.]]) >>> 
Sign up to request clarification or add additional context in comments.

Comments

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.]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.