In logistic regression you need to compute where
and
Performance is much slower with for loop.
z=0 for i in range(n-x): z+= w[i]*x[i] z+=b
Vectorized approach is much more efficient that for loop.
z=np.dot(w,x)+b # it calculates w^Tx
Compare the time to calculate a dot product using for loop and vector.
import time a=np.random.rand(1000000) b=np.random.rand(1000000) tic=time.time() c=np.dot(a,b) toc=time.time() print(c) print("vectorized version:" +str(1000*(toc-tic)) +" ms") c=0 tic=time.time() for i in range(100000): c += a[i]*b[i] toc=time.time() print(c) print("for loop version:" +str(1000*(toc-tic)) +" ms")
It turns out that for loop took 327 times longer to compute. So, whenever possible, avoid explicit for loops.
u=np.zeros((n,1)) for i in range (n): u[i]=math.exp(v[i])
import numpy as np u=np.exp(v)
np.log(v) np.abs(v) np.maximum(v,0) v**2 1/v
$J=0;
dw_1=0; dw_2=0;USE
db=0;$
for to :
2nd loop; use
for to :
(n=2 features . add for loop over all the features. (in this case for loop j=1 to 2))
and divide them by m;
; , Use
db/=m$
dw=np.zeros((n_x,1))
; ;
for to :
for k=1 to m:
and divide them by m;