forked from Bhavya06/Neural-Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNNEye.py
80 lines (63 loc) · 2.26 KB
/
DNNEye.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
from datetime import datetime
startTime = datetime.now()
l = []
def generateColumns(start, end):
for i in range(start, end+1):
l.extend([str(i)+'X', str(i)+'Y'])
return l
eyes = generateColumns(1, 12)
# reading in the csv as a dataframe
import pandas as pd
df = pd.read_csv('Eyes.csv')
# selecting the features and target
X = df[eyes]
y = df['truth_value']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.10, random_state = 42)
# Data Normalization
from sklearn.preprocessing import StandardScaler as SC
sc = SC()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
import numpy as np
X_train, y_train, X_test, y_test = np.array(X_train), np.array(y_train), np.array(X_test), np.array(y_test)
# importing the required layers from keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Activation
# layering up the cnn
model = Sequential()
model.add(Dense(4, input_dim = X_train.shape[1]))
model.add(Activation('relu'))
model.add(Dense(4))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# model compilation
opt = 'adam'
model.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = ['accuracy'])
# training
model.fit(X_train, y_train, batch_size = 2, epochs = 50, validation_data = (X_test, y_test), verbose = 2)
# using the learned weights to predict the target
y_pred = model.predict(X_test)
# setting a confidence threshhold of 0.9
y_pred_labels = list(y_pred > 0.9)
for i in range(len(y_pred_labels)):
if int(y_pred_labels[i]) == 1 : y_pred_labels[i] = 1
else : y_pred_labels[i] = 0
# plotting a confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred_labels)
print("\n")
print("Confusion Matrix : ")
print(cm)
print("\n")
# creating a dataframe to show results
df_results = pd.DataFrame()
df_results['Actual label'] = y_test
df_results['Predicted value'] = y_pred
df_results['Predicted label'] = y_pred_labels
df_results.to_csv(r'Results.csv')
# printing execution time of script
print("\n")
print("Execution time in seconds = ", datetime.now() - startTime)