-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistik.py
81 lines (54 loc) · 1.91 KB
/
logistik.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#import required modules
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
#Sigmoid method
def sigmoid(self, x, weight):
z = np.dot(x, weight)
return 1 / (1 + np.exp(-z))
#method to calculate the Loss
def loss(self, h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
#Method for calculating the gradients
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)
#Updating the weights
self.weight -= lr * dW
return print('fitted successfully to data')
#Method to predict the class label.
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
# In[2]:
from sklearn.datasets import load_breast_cancer
#Loading the data
data = load_breast_cancer()
#Preparing the data
x = data.data
y = data.target
#creating the class Object
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]))