-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogistic_Regression_Training_And_Prediction.py
105 lines (75 loc) · 2.37 KB
/
Logistic_Regression_Training_And_Prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
Created_by : Anand Tiwari
Created_at : 15/08/2018
Description: This algorithm describe the working or implimentation of logistic regression.
We have used XOR problem to impliment this algorithm. We have used sigmoid (also known as Logistic)
as a activation function and Gradient Descent for training the model.
'''
import numpy as np
import matplotlib.pyplot as plt
class LogisticRegression(object):
# initializing the hyperparameter while creatng object
def __init__(self, reg = 0.01, learning_rate = 0.1, max_iters = 5000):
self.reg = reg
self.learning_rate = learning_rate
self.max_iters = max_iters
# Sigmoid function
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
# Cost or Error function
def cross_entropy(self, T, y):
return -(T * np.log(y) + (1 - T) * np.log(1 - y)).mean()
# Classification of accurate prediction
def classification(self, y, prediction):
return np.mean(y == prediction)
# fit function
def fit(self, X, Y):
N, D = X.shape
# randomly initialize weights
self.w = np.random.randn(D)
self.error = []
# Gradient Descent
for i in range(self.max_iters):
YPred = self.sigmoid(X.dot(self.w))
e = self.cross_entropy(Y, YPred)
self.error.append(e)
self.w -= self.learning_rate * (X.T.dot(YPred - Y) + self.reg * self.w)
# predict function
def predict(self, X):
return self.sigmoid(X.dot(self.w))
# score function
def score(self, X, Y):
YPred = self.predict(X)
return self.classification(Y, np.round(YPred))
# weight function to return the optimal weights
def weights(self):
return self.w
# plot function to plot the error
def plot(self):
plt.plot(self.error, c='b', label = 'Train Error')
plt.legend()
plt.show()
if __name__ == '__main__':
N = 4
D = 2
# The XOR inputs
X = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
# Target
Y = np.array([0, 1, 1, 0])
# Feature Engineering
ones = np.array([[1] * N]).T
xy = np.matrix(X[:, 0] * X[:, 1]).T
Xb = np.array(np.concatenate((ones, xy, X), axis = 1))
# Creating model
# I have used XOR. You can use any problem you want
# Be careful with the input you provide
lr = LogisticRegression()
lr.fit(Xb, Y)
print("classification Accuracy is : ", lr.score(Xb, Y))
print("Optimal Weights are : ", lr.weights())
lr.plot()