Vectorization if non-verctorized:

1 2 3 4 |
Z = 0 for i in range(nx): z += W[i] * X[i] Z += b |

if vectorized:

1 |
Z = np.dot(W,X) + b |

it is much faster

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import numpy as np import time a = np.random.rand(1000000) b = np.random.rand(1000000) #check how much time was used tic = time.time() c = np.dot(a,b) toc = time.time() print("Vectorized version : " + str(1000*(toc-tic) + "ms") # 1.5ms c = 0 tic = 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)

1 |
Z = np.dot(W.T,X) + b |

A note on python/numpy vectors to simplify code and to avoid bug, don't use rank 1 array