Vectorization
-
- if non-verctorized:
1234Z = 0for i in range(nx):z += W[i] * X[i]Z += b - if vectorized:
1Z = np.dot(W,X) + b - it is much faster
1234567891011121314151617181920import numpy as npimport timea = np.random.rand(1000000)b = np.random.rand(1000000)#check how much time was usedtic = time.time()c = np.dot(a,b)toc = time.time()print("Vectorized version : " + str(1000*(toc-tic) + "ms") # 1.5msc = 0tic = time.time()for i in range(1000000):c += a[i]*b[i]toc = time.time()print("For loop version : " + str(1000*(toc-tic) + "ms") #474.2ms
Vectorizing Logistic Regression
-
- b is real number, it will be automatically changed to vector to be added each element of matrix (python broadcasting)
1Z = np.dot(W.T,X) + b
- b is real number, it will be automatically changed to vector to be added each element of matrix (python broadcasting)
A note on python/numpy vectors
- to simplify code and to avoid bug, don't use rank 1 array