-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogisticRegression4.py
More file actions
59 lines (40 loc) · 1.55 KB
/
logisticRegression4.py
File metadata and controls
59 lines (40 loc) · 1.55 KB
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
from sklearn.datasets import load_breast_cancer
import numpy as np
class LogisticRegression:
def __init__(self,x,y):
self.intercept = np.ones((x.shape[0], 1))
self.x = np.concatenate((self.intercept, x), axis=1)
self.weight = np.zeros(self.x.shape[1])
self.y = y
def sigmoid(self, x, weight):
z = np.dot(x, weight)
return 1 / (1 + np.exp(-z))
def loss(self, h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
def gradient_descent(self, X, h, y):
return np.dot(X.T, (h - y)) / y.shape[0]
def fit(self, lr , iterations):
for i in range(iterations):
sigma = self.sigmoid(self.x, self.weight)
loss = self.loss(sigma,self.y)
dW = self.gradient_descent(self.x , sigma, self.y)
self.weight -= lr * dW
return print('fitted successfully to data')
def predict(self, x_new , treshold):
x_new = np.concatenate((self.intercept, x_new), axis=1)
result = self.sigmoid(x_new, self.weight)
result = result >= treshold
y_pred = np.zeros(result.shape[0])
for i in range(len(y_pred)):
if result[i] == True:
y_pred[i] = 1
else:
continue
return y_pred
data = load_breast_cancer()
x = data.data
y = data.target
regressor = LogisticRegression(x,y)
regressor.fit(0.1 , 5000)
y_pred = regressor.predict(x,0.5)
print('accuracy -> {}'.format(sum(y_pred == y) / y.shape[0]))