Logistic Regression
Logistic Regression
Sargur N. Srihari
University at Buffalo, State University of New York
USA
Machine Learning Srihari
{ }
1−tn
p(t | w) = ∏ yn 1 − yn
tn
n =1
where t =(t1,..,tN)T and yn= p(C1|ϕn)
6
Machine Learning Srihari
{ }
1−tn
p(t | w) = ∏ yn 1 − yn
tn
n=1
{ }
E(w) = − ln p(t | w) = − ∑ tn ln yn + (1 − tn )ln(1 − yn )
n =1
What is Cross-entropy?
• Entropy of p(x) is defined as H(p) = − ∑ p(x)log p(x)
x
8
Machine Learning Srihari
{
E(w) = − ln p(t | w) = − ∑ tn ln yn + (1 − tn )ln(1 − yn )
n =1
}
where yn= σ(wTϕn)
( )
∇E(w) = ∑ yn − tn φn
where y = σ(w ϕ )
n =1
n
T
n
Finding db and dw
Derivative wrt p à Derivative wrt z.
https://towardsdatascience.com/
logistic-regression-from-
very-scratch-ea914961f320
Logistic Regression Code in Python
use sci-kit learn to create a data set.
import sklearn.datasets
import matplotlib.pyplot as plt
import numpy as np
X, Y = sklearn.datasets.make_moons(n_samples=500, noise=.2)
X, Y = X.T, Y.reshape(1, Y.shape[0])
epochs = 1000
learningrate = 0.01
def sigmoid(z):
return 1 / (1 + np.exp(-z))
losstrack = []
m = X.shape[1]
w = np.random.randn(X.shape[0], 1)*0.01
b=0
for epoch in range(epochs):
z = np.dot(w.T, X) + b
p = sigmoid(z)
cost = -np.sum(np.multiply(np.log(p), Y) + np.multiply((1 - Y), np.log(1 - p)))/m
losstrack.append(np.squeeze(cost))
dz = p-Y
dw = (1 / m) * np.dot(X, dz.T)
db = (1 / m) * np.sum(dz)
w = w - learningrate * dw
b = b - learningrate * db
plt.plot(losstrack)
Prediction: From the code above, you find p. It will be between 0 and
Machine Learning Srihari
σ(a)
0
!2
separable data !4
!6
!8
!4 !2 0 2 4 6 8
{
∇En = −∑ tn − wT φ(xn )
n=1
} φ(x ) n
T
without reg
⎡ N ⎤
{
∇En = ⎢ − ∑ tn − wT φ(x n ) } φ(x ) n
T
⎥ + λw with reg 13
⎣ n=1 ⎦
14
• Gradient
• Hessian
16
• Gradient
• Hessian
Number of iterations : 10
Markov
y I has value 1 when
y=1, else 0 Logistic Regression
i=1
18
Machine Learning Srihari
19
https://storage.ning.com/topology/rest/1.0/file/get/2408482975?profile=original