-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiris.py
64 lines (51 loc) · 2.03 KB
/
iris.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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from neural import NeuralNetworkClassifier
#dati che verrano utilizzati
iris_dataset = datasets.load_iris()
#print(iris_dataset["DESCR"])
#caratteristiche, dati in input
X = iris_dataset.data
#output, cioe' quello che il modello dovrebbe predire
Y = iris_dataset.target
#divido i dati, un parte li uso per addestrare, l'altra per
#testare se il modello ha imparato bene
X_train, X_test, Y_train, Y_test = train_test_split(X, Y)
X_train, X_test = X_train.T, X_test.T
#=============================================================
# Parameter of computation and train of the network
#=============================================================
N = len(X_train[0,:])
n_epoch = 400 + 1
lr_rate = 0.01
NN = NeuralNetworkClassifier([50, 50], n_epoch, f_act='relu')
result = NN.train(X_train, Y_train, alpha=lr_rate, verbose=True)
A = NN.predict(X_train[:, :N-N//4 ])
NN.confmat(Y_train[:N-N//4], A, plot=True, title='Confusion matrix for train data', k=0)
#plt.savefig("conf_mat_fit_train.pdf")
L_t = result['train_Loss']
L_v = result['valid_Loss']
#=============================================================
# Plot Loss
#=============================================================
plt.figure(1)
plt.plot(np.linspace(1, n_epoch, n_epoch), L_t, 'b', label='train Loss')
plt.plot(np.linspace(1, n_epoch, n_epoch), L_v, 'r', label='validation loss')
plt.title('Binay cross entropy', fontsize=15)
plt.xlabel('epoch', fontsize=15)
plt.ylabel('Loss', fontsize=15)
plt.legend(loc='best')
#plt.savefig("Loss_fit.pdf")
plt.grid()
#=============================================================
# Test of the network
#=============================================================
A = NN.predict(X_test)
M = NN.confmat(Y_test, A, plot=True, title='Confusion matrix for test data', k=2)
#plt.savefig("conf_mat_fit_test.pdf")
acc = NN.accuracy(A, Y_test)
print(f"Accuracy on test set = {acc:.5f}")
plt.tight_layout()
plt.show()