The Python Book

matrix dotproduct numpy
20160122

# Matrix multiplication : dot product

``````a= np.array([[2., -1., 0.],[-3.,6.0,1.0]])

array([[ 2., -1.,  0.],
[-3.,  6.,  1.]])

b= np.array([ [1.0,0.0,-1.0,2],[-4.,3.,1.,0.],[0.,3.,0.,-2.]])

array([[ 1.,  0., -1.,  2.],
[-4.,  3.,  1.,  0.],
[ 0.,  3.,  0., -2.]])

np.dot(a,b)

array([[  6.,  -3.,  -3.,   4.],
[-27.,  21.,   9.,  -8.]])``````

# Dot product of two vectors

Take the first row of above a matrix and the first column of above b matrix:

``````np.dot( np.array([ 2., -1.,  0.]), np.array([ 1.,-4.,0. ]) )
6.0``````

# Normalize a matrix

Normalize the columns: suppose the columns make up the features, and the rows the observations.

Calculate the 'normalizers':

``````norms=np.linalg.norm(a,axis=0)

print norms
[ 3.60555128  6.08276253  1. ]``````

Turn a into normalized matrix an:

``````an = a/norms

print an

[[ 0.5547002  -0.16439899  0.        ]
[-0.83205029  0.98639392  1.        ]]``````

Notes by Willem Moors. Generated on momo:/home/willem/sync/20151223_datamungingninja/pythonbook at 2019-07-31 19:22