Numpy Array vs Vector

This notebook explains the difference between Numpy Array and Vector.
If you are new to Numpy, please checkout numpy basics tutorial first.

Let us create a numpy array.

In [1]:
import numpy as np
In [2]:
arr0 = np.random.randn(4)
arr0
Out[2]:
array([ 0.87942377, -0.69131025,  0.33220169,  1.76007805])
In [3]:
arr0.shape
Out[3]:
(4,)

The above is rank 1 or 1 dimensional array but this is neither a row or column.

let us create another array.

In [4]:
arr1 = np.random.randn(4).T
arr1
Out[4]:
array([-0.45719195,  0.78906387, -0.15142986, -0.3826037 ])
In [5]:
arr1.shape
Out[5]:
(4,)

For arr1, even though we applied the transpose, it is still a simple array without any row or column property.

Now let us calulate dot product.

In [6]:
res = np.dot(arr0,arr1)
res
Out[6]:
-1.6712710262811714

Note dot product didn't generate a matrix but a number only.

Let us repeat the above steps for Numpy Vector now.

In [7]:
arr0 = np.random.randn(4,1)
arr0
Out[7]:
array([[ 1.12964633],
       [ 1.29681385],
       [-0.53971566],
       [-0.17936079]])

Even though it is still array but note the difference, two square brackets vs one in numpy array earlier. Let us print the shape.

In [8]:
arr0.shape
Out[8]:
(4, 1)

As we can see shape is 4,1 that is 4 rows and one column

In [9]:
arr1 = np.random.randn(1,4)
arr1
Out[9]:
array([[-0.06850754, -0.01908695, -1.0186154 , -1.15776782]])
In [10]:
arr1.shape
Out[10]:
(1, 4)
In [11]:
res = np.dot(arr0,arr1)
res
Out[11]:
array([[-0.07738929, -0.02156151, -1.15067516, -1.30786817],
       [-0.08884152, -0.02475222, -1.32095457, -1.50140934],
       [ 0.03697459,  0.01030153,  0.54976269,  0.62486542],
       [ 0.01228757,  0.00342345,  0.18269966,  0.20765815]])

res is a numpy matrix.