Day 8 Logistic回归原理与实现
github: 100DaysOfMLCode
最大似然函数
L
=
∏
i
=
0
n
p
y
i
(
1
−
p
)
1
−
y
i
L = \prod_{i=0}^np^{y_i}(1-p)^{1-y_i}
L=∏i=0npyi(1−p)1−yi
取对数得
L
=
∑
i
=
0
n
y
i
l
o
g
(
p
)
+
(
1
−
y
i
)
l
o
g
(
1
−
p
)
L = \sum_{i=0}^ny_ilog(p)+(1-y_i)log(1-p)
L=∑i=0nyilog(p)+(1−yi)log(1−p)
p
(
z
)
=
1
1
+
e
z
p(z) = \frac{1}{1+e^{z}}
p(z)=1+ez1
z
=
w
x
+
b
z = wx + b
z=wx+b
因此梯度下降法求偏导数可得:
∂
L
∂
w
=
∂
L
∂
p
∂
p
∂
z
∂
z
∂
w
=
(
−
y
p
+
1
−
y
1
−
p
)
(
p
(
1
−
p
)
)
x
=
(
a
−
y
)
x
\frac{\partial{L}}{\partial{w}}=\frac{\partial{L}}{\partial{p}}\frac{\partial{p}}{\partial{z}}\frac{\partial{z}}{\partial{w}}=(\frac{-y}{p}+\frac{1-y}{1-p})(p(1-p))x=(a-y)x
∂w∂L=∂p∂L∂z∂p∂w∂z=(p−y+1−p1−y)(p(1−p))x=(a−y)x
∂
L
∂
b
=
(
a
−
y
)
\frac{\partial{L}}{\partial{b}}=(a-y)
∂b∂L=(a−y)
def weightInitialization(n_features):
w = np.zeros((1,n_features))
b = 0
return w,b
def sigmoid_activation(result):
final_result = 1/(1+np.exp(-result))
return final_result
def model_optimize(w, b, X, Y):
m = X.shape[0]
#Prediction
final_result = sigmoid_activation(np.dot(w,X.T)+b)
Y_T = Y.T
cost = (-1/m)*(np.sum((Y_T*np.log(final_result)) + ((1-Y_T)*(np.log(1-final_result)))))
#
#Gradient calculation
dw = (1/m)*(np.dot(X.T, (final_result-Y.T).T))
db = (1/m)*(np.sum(final_result-Y.T))
grads = {"dw": dw, "db": db}
return grads, cost
def model_predict(w, b, X, Y, learning_rate, no_iterations):
costs = []
for i in range(no_iterations):
#
grads, cost = model_optimize(w,b,X,Y)
#
dw = grads["dw"]
db = grads["db"]
#weight update
w = w - (learning_rate * (dw.T))
b = b - (learning_rate * db)
#
if (i % 100 == 0):
costs.append(cost)
#print("Cost after %i iteration is %f" %(i, cost))
#final parameters
coeff = {"w": w, "b": b}
gradient = {"dw": dw, "db": db}
return coeff, gradient, costs
def predict(final_pred, m):
y_pred = np.zeros((1,m))
for i in range(final_pred.shape[1]):
if final_pred[0][i] > 0.5:
y_pred[0][i] = 1
return y_pred