Update logistic_regression.py

This commit is contained in:
Harshil 2018-10-24 21:20:28 +02:00 committed by GitHub
parent 2638d57c71
commit 3fa8f7bc2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,29 +1,29 @@
#!/usr/bin/env python #!/usr/bin/python
# coding: utf-8 # -*- coding: utf-8 -*-
## Logistic Regression from scratch ## Logistic Regression from scratch
# In[62]: # In[62]:
# In[63]:
# importing all the required libraries
''' Implementing logistic regression for classification problem ''' Implementing logistic regression for classification problem
Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac''' Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac'''
# In[63]:
#importing all the required libraries
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# get_ipython().run_line_magic('matplotlib', 'inline')
from sklearn import datasets from sklearn import datasets
# In[67]: # In[67]:
# sigmoid function or logistic function is used as a hypothesis function in classification problems # sigmoid function or logistic function is used as a hypothesis function in classification problems
def sigmoid_function(z): def sigmoid_function(z):
return 1 / (1 + np.exp(-z)) return 1 / (1 + np.exp(-z))
@ -31,45 +31,41 @@ def sigmoid_function(z):
def cost_function(h, y): def cost_function(h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
# here alpha is the learning rate, X is the feature matrix,y is the target matrix # here alpha is the learning rate, X is the feature matrix,y is the target matrix
def logistic_reg(alpha,X,y,max_iterations=70000):
def logistic_reg(
alpha,
X,
y,
max_iterations=70000,
):
converged = False converged = False
iterations = 0 iterations = 0
theta = np.zeros(X.shape[1]) theta = np.zeros(X.shape[1])
while not converged: while not converged:
z = np.dot(X, theta) z = np.dot(X, theta)
h = sigmoid_function(z) h = sigmoid_function(z)
gradient = np.dot(X.T,(h-y))/y.size gradient = np.dot(X.T, h - y) / y.size
theta=theta-(alpha)*gradient theta = theta - alpha * gradient
z = np.dot(X, theta) z = np.dot(X, theta)
h = sigmoid_function(z) h = sigmoid_function(z)
J = cost_function(h, y) J = cost_function(h, y)
iterations += 1 # update iterations iterations += 1 # update iterations
if iterations == max_iterations: if iterations == max_iterations:
print("Maximum iterations exceeded!") print ('Maximum iterations exceeded!')
print("Minimal cost function J=",J) print ('Minimal cost function J=', J)
converged = True converged = True
return theta return theta
# In[68]: # In[68]:
if __name__ == '__main__': if __name__ == '__main__':
iris = datasets.load_iris() iris = datasets.load_iris()
X = iris.data[:, :2] X = iris.data[:, :2]
@ -78,6 +74,8 @@ if __name__=='__main__':
alpha = 0.1 alpha = 0.1
theta = logistic_reg(alpha, X, y, max_iterations=70000) theta = logistic_reg(alpha, X, y, max_iterations=70000)
print (theta) print (theta)
def predict_prob(X): def predict_prob(X):
return sigmoid_function(np.dot(X, theta)) # predicting the value of probability from the logistic regression algorithm return sigmoid_function(np.dot(X, theta)) # predicting the value of probability from the logistic regression algorithm
@ -85,14 +83,19 @@ if __name__=='__main__':
plt.figure(figsize=(10, 6)) plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0') plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1') plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1')
x1_min, x1_max = X[:,0].min(), X[:,0].max(), (x1_min, x1_max) = (X[:, 0].min(), X[:, 0].max())
x2_min, x2_max = X[:,1].min(), X[:,1].max(), (x2_min, x2_max) = (X[:, 1].min(), X[:, 1].max())
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max)) (xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max),
np.linspace(x2_min, x2_max))
grid = np.c_[xx1.ravel(), xx2.ravel()] grid = np.c_[xx1.ravel(), xx2.ravel()]
probs = predict_prob(grid).reshape(xx1.shape) probs = predict_prob(grid).reshape(xx1.shape)
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black'); plt.contour(
xx1,
plt.legend(); xx2,
probs,
[0.5],
linewidths=1,
colors='black',
)
plt.legend()