Skip to content

Commit 3fa8f7b

Browse files
authored
Update logistic_regression.py
1 parent 2638d57 commit 3fa8f7b

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed
+64-61
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,101 @@
1-
#!/usr/bin/env python
2-
# coding: utf-8
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
33

4-
# # Logistic Regression from scratch
4+
## Logistic Regression from scratch
55

66
# In[62]:
77

8+
# In[63]:
9+
10+
# importing all the required libraries
811

912
''' Implementing logistic regression for classification problem
1013
Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac'''
1114

12-
13-
# In[63]:
14-
15-
16-
#importing all the required libraries
1715
import numpy as np
1816
import matplotlib.pyplot as plt
19-
get_ipython().run_line_magic('matplotlib', 'inline')
17+
18+
# get_ipython().run_line_magic('matplotlib', 'inline')
19+
2020
from sklearn import datasets
2121

2222

2323
# In[67]:
2424

25+
# sigmoid function or logistic function is used as a hypothesis function in classification problems
2526

26-
#sigmoid function or logistic function is used as a hypothesis function in classification problems
2727
def sigmoid_function(z):
28-
return 1/(1+np.exp(-z))
28+
return 1 / (1 + np.exp(-z))
29+
2930

31+
def cost_function(h, y):
32+
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
3033

31-
def cost_function(h,y):
32-
return (-y*np.log(h)-(1-y)*np.log(1-h)).mean()
3334

3435
# here alpha is the learning rate, X is the feature 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-
36+
37+
def logistic_reg(
38+
alpha,
39+
X,
40+
y,
41+
max_iterations=70000,
42+
):
43+
converged = False
44+
iterations = 0
45+
theta = np.zeros(X.shape[1])
46+
4147
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-
J=cost_function(h,y)
50-
51-
52-
53-
iterations+=1 #update iterations
54-
55-
56-
if iterations== max_iterations:
57-
print("Maximum iterations exceeded!")
58-
print("Minimal cost function J=",J)
59-
converged=True
60-
61-
return theta
48+
z = np.dot(X, theta)
49+
h = sigmoid_function(z)
50+
gradient = np.dot(X.T, h - y) / y.size
51+
theta = theta - alpha * gradient
6252

53+
z = np.dot(X, theta)
54+
h = sigmoid_function(z)
55+
J = cost_function(h, y)
6356

57+
iterations += 1 # update iterations
6458

65-
66-
67-
59+
if iterations == max_iterations:
60+
print ('Maximum iterations exceeded!')
61+
print ('Minimal cost function J=', J)
62+
converged = True
6863

64+
return theta
6965

70-
# In[68]:
7166

67+
# In[68]:
7268

73-
if __name__=='__main__':
74-
iris=datasets.load_iris()
69+
if __name__ == '__main__':
70+
iris = datasets.load_iris()
7571
X = iris.data[:, :2]
7672
y = (iris.target != 0) * 1
77-
78-
alpha=0.1
79-
theta=logistic_reg(alpha,X,y,max_iterations=70000)
80-
print(theta)
73+
74+
alpha = 0.1
75+
theta = logistic_reg(alpha, X, y, max_iterations=70000)
76+
print (theta)
77+
78+
8179
def predict_prob(X):
82-
return sigmoid_function(np.dot(X,theta)) # predicting the value of probability from the logistic regression algorithm
83-
84-
80+
return sigmoid_function(np.dot(X, theta)) # predicting the value of probability from the logistic regression algorithm
81+
82+
8583
plt.figure(figsize=(10, 6))
8684
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0')
8785
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1')
88-
x1_min, x1_max = X[:,0].min(), X[:,0].max(),
89-
x2_min, x2_max = X[:,1].min(), X[:,1].max(),
90-
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
86+
(x1_min, x1_max) = (X[:, 0].min(), X[:, 0].max())
87+
(x2_min, x2_max) = (X[:, 1].min(), X[:, 1].max())
88+
(xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max),
89+
np.linspace(x2_min, x2_max))
9190
grid = np.c_[xx1.ravel(), xx2.ravel()]
9291
probs = predict_prob(grid).reshape(xx1.shape)
93-
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black');
94-
95-
plt.legend();
96-
97-
98-
92+
plt.contour(
93+
xx1,
94+
xx2,
95+
probs,
96+
[0.5],
97+
linewidths=1,
98+
colors='black',
99+
)
100+
101+
plt.legend()

0 commit comments

Comments
 (0)