Python/machine_learning/logistic_regression.py

103 lines
2.8 KiB
Python
Raw Normal View History

2018-10-24 19:20:28 +00:00
#!/usr/bin/python
# Logistic Regression from scratch
# In[62]:
2018-10-24 19:20:28 +00:00
# In[63]:
# importing all the required libraries
"""
Implementing logistic regression for classification problem
Helpful resources:
Coursera ML course
https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac
"""
import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets
2018-10-24 19:20:28 +00:00
# get_ipython().run_line_magic('matplotlib', 'inline')
# In[67]:
# sigmoid function or logistic function is used as a hypothesis function in
# classification problems
2019-10-05 05:14:13 +00:00
def sigmoid_function(z):
"""
Also known as Logistic Function.
1
f(x) = -------
1 + eˣ
The sigmoid function approaches a value of 1 as its input 'x' becomes
increasing positive. Opposite for negative values.
Reference: https://en.wikipedia.org/wiki/Sigmoid_function
@param z: input to the function
@returns: returns value in the range 0 to 1
"""
2018-10-24 19:20:28 +00:00
return 1 / (1 + np.exp(-z))
2018-10-24 19:20:28 +00:00
def cost_function(h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
2019-10-05 05:14:13 +00:00
def log_likelihood(x, y, weights):
scores = np.dot(x, weights)
return np.sum(y * scores - np.log(1 + np.exp(scores)))
2019-10-05 05:14:13 +00:00
2018-10-16 19:22:32 +00:00
# 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):
theta = np.zeros(x.shape[1])
2018-10-24 19:20:28 +00:00
for iterations in range(max_iterations):
z = np.dot(x, theta)
2018-10-24 19:20:28 +00:00
h = sigmoid_function(z)
gradient = np.dot(x.T, h - y) / y.size
theta = theta - alpha * gradient # updating the weights
z = np.dot(x, theta)
2018-10-24 19:20:28 +00:00
h = sigmoid_function(z)
j = cost_function(h, y)
if iterations % 100 == 0:
print(f"loss: {j} \t") # printing the loss after every 100 iterations
2018-10-24 19:20:28 +00:00
return theta
2019-10-05 05:14:13 +00:00
2018-10-24 19:20:28 +00:00
# In[68]:
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
2018-10-24 19:20:28 +00:00
iris = datasets.load_iris()
x = iris.data[:, :2]
y = (iris.target != 0) * 1
2018-10-24 19:20:28 +00:00
alpha = 0.1
theta = logistic_reg(alpha, x, y, max_iterations=70000)
2019-10-05 05:14:13 +00:00
print("theta: ", theta) # printing the theta i.e our weights vector
2018-10-24 19:20:28 +00:00
def predict_prob(x):
2019-10-05 05:14:13 +00:00
return sigmoid_function(
np.dot(x, theta)
2019-10-05 05:14:13 +00:00
) # predicting the value of probability from the logistic regression algorithm
2018-10-24 19:20:28 +00:00
plt.figure(figsize=(10, 6))
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")
(x1_min, x1_max) = (x[:, 0].min(), x[:, 0].max())
(x2_min, x2_max) = (x[:, 1].min(), x[:, 1].max())
2019-10-05 05:14:13 +00:00
(xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
grid = np.c_[xx1.ravel(), xx2.ravel()]
probs = predict_prob(grid).reshape(xx1.shape)
2019-10-05 05:14:13 +00:00
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors="black")
2018-10-24 19:20:28 +00:00
plt.legend()
plt.show()