-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathneural_network.py
More file actions
46 lines (38 loc) · 1.5 KB
/
neural_network.py
File metadata and controls
46 lines (38 loc) · 1.5 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
from sklearn import metrics
from sklearn.neural_network import MLPClassifier
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import datetime
from mlxtend.plotting import plot_decision_regions
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
def predict_nn(x,y,z,clf):
clf.fit(x, y)
predicted = clf.predict(z)
return predicted
if __name__ == "__main__":
startTime = datetime.datetime.now()
x = np.load('data/train_encoded_array.npy')
#x[np.where(x==0)] = 0.001
y = np.load('data/train_target_array.npy')
y = y.astype('int')
y = y.flatten()
z = np.load('data/test_encoded_array.npy')
#z[np.where(z==0)] = 0.001
t = np.load('data/test_target_array.npy')
t = t.astype('int')
t = t.flatten()
learningRate =[0.1, 0.01, 0.001]
for lr in learningRate:
clf = MLPClassifier(solver='sgd', hidden_layer_sizes=(4, ), random_state=1, batch_size='auto', learning_rate='constant', learning_rate_init=lr)
clf.fit(x, y)
predicted = predict_nn(x,y,z,clf)
print("For learning rate: ", lr)
accuracy = metrics.accuracy_score(t, predicted, normalize=False)
confusion_matrix = metrics.confusion_matrix(t, predicted)
#print(np.shape(predicted))
print("Accuracy Score: ",accuracy)
print("Confusion Matrix:\n",confusion_matrix)
endTime = datetime.datetime.now()- startTime
print("Total time taken to train: ",endTime)