-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.py
More file actions
69 lines (54 loc) · 2.49 KB
/
Copy pathnn.py
File metadata and controls
69 lines (54 loc) · 2.49 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
60
61
62
63
64
65
66
67
68
69
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
class NeauralNetwork:
def __init__(self):
self.w1 = 1
self.w2 = 0
self.bias = 0
def fit(self, X, y, epochs, loss_threshold):
self.w1, self.w2, self.bias = self.gradient_descent(X['age'], X['affordibility'], y, epochs, loss_threshold)
def predict(self, X_test):
weighted_sum = self.w1 * X_test['age'] + self.w2 * X_test['affordibility'] + self.bias
return self.sigmoid_numpy(weighted_sum)
def log_loss(self, y_true, y_predicted):
epsilon = 1e-15
y_predicted_new = [max(i,epsilon) for i in y_predicted]
y_predicted_new = [min(i, 1-epsilon) for i in y_predicted_new]
y_predicted_new = np.array(y_predicted_new)
return -np.mean(y_true * np.log(y_predicted_new) + (1 - y_true) * np.log(1 - y_predicted_new))
def sigmoid_numpy(self, x):
return 1 / (1 + np.exp(-x))
def gradient_descent(self, age, affordibility, y_true, epochs, loss_threshold):
# w1, w2, bias
w1 = w2 = 1
bias = 0
rate = 0.5
n = len(age)
for i in range(epochs):
weighted_sum = w1 * age + w2 * affordibility + bias
y_predicted = self.sigmoid_numpy(weighted_sum)
loss = self.log_loss(y_true, y_predicted)
w1d = (1/n) * np.dot(np.transpose(age), (y_predicted - y_true))
w2d = (1/n) * np.dot(np.transpose(affordibility), (y_predicted - y_true))
bd = np.mean(y_predicted - y_true)
w1 = w1 - rate * w1d
w2 = w2 - rate * w2d
bias = bias - rate * bd
# reduce prints to every 100th epoch
if i % 100 == 0:
print (f'Epoch:{i}, w1:{w1}, w2:{w2}, bias:{bias}, loss:{loss}')
if loss <= loss_threshold:
break
return w1, w2, bias
df = pd.read_csv("insurance_data.csv")
df.head()
X_train, X_test, y_train, y_test = train_test_split(df[['age','affordibility']],df.bought_insurance, test_size=0.2, random_state=25)
# we do this to bring both age and affordability on the same scale between 0 and 1
# if you do scaling the ML model tends to work better
X_train_scaled = X_train.copy()
X_train_scaled['age'] = X_train_scaled['age'] / 100
X_test_scaled = X_test.copy()
X_test_scaled['age'] = X_test_scaled['age'] / 100
neauralNetwork = NeauralNetwork()
neauralNetwork.fit(X_train_scaled, y_train, epochs=1000, loss_threshold=0.4631)