Skip to content

Commit ade3ed3

Browse files
authoredOct 16, 2018
Logistic regression implementation
implementation of logistic regression for binary classification
1 parent 16aea14 commit ade3ed3

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
 
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# # Logistic Regression from scratch
5+
6+
# In[62]:
7+
8+
9+
''' Implementing logistic regression for classification problem
10+
Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac'''
11+
12+
13+
# In[63]:
14+
15+
16+
#importing all the required libraries
17+
import numpy as np
18+
import matplotlib.pyplot as plt
19+
get_ipython().run_line_magic('matplotlib', 'inline')
20+
from sklearn import datasets
21+
22+
23+
# In[67]:
24+
25+
26+
#sigmoid function or logistic function is used as a hypothesis function in classification problems
27+
def sigmoid_function(z):
28+
return 1/(1+np.exp(-z))
29+
30+
31+
def cost_function(h,y):
32+
return (-y*np.log(h)-(1-y)*np.log(1-h)).mean()
33+
34+
# here alpha is the learning rate, X is the featue matrix,y is the target matrix
35+
def logistic_reg(alpha,X,y,max_iterations=70000):
36+
converged=False
37+
iterations=0
38+
theta=np.zeros(X.shape[1])
39+
40+
num_iterations=0
41+
while not converged:
42+
z=np.dot(X,theta)
43+
h=sigmoid_function(z)
44+
gradient = np.dot(X.T,(h-y))/y.size
45+
theta=theta-(alpha)*gradient
46+
47+
z=np.dot(X,theta)
48+
h=sigmoid_function(z)
49+
e=cost_function(h,y)
50+
print('J=',e)
51+
J=e
52+
53+
iterations+=1 #update iterations
54+
55+
56+
if iterations== max_iterations:
57+
print("Maximum iterations exceeded!")
58+
converged=True
59+
60+
return theta
61+
62+
63+
64+
65+
66+
67+
68+
69+
# In[68]:
70+
71+
72+
if __name__=='__main__':
73+
iris=datasets.load_iris()
74+
X = iris.data[:, :2]
75+
y = (iris.target != 0) * 1
76+
77+
alpha=0.1
78+
theta=logistic_reg(alpha,X,y,max_iterations=70000)
79+
print(theta)
80+
def predict_prob(X):
81+
return sigmoid_function(np.dot(X,theta)) # predicting the value of probability from the logistic regression algorithm
82+
83+
84+
plt.figure(figsize=(10, 6))
85+
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0')
86+
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1')
87+
x1_min, x1_max = X[:,0].min(), X[:,0].max(),
88+
x2_min, x2_max = X[:,1].min(), X[:,1].max(),
89+
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
90+
grid = np.c_[xx1.ravel(), xx2.ravel()]
91+
probs = predict_prob(grid).reshape(xx1.shape)
92+
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black');
93+
94+
plt.legend();
95+
96+
97+

0 commit comments

Comments
 (0)
Please sign in to comment.