-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
59 lines (42 loc) · 1.63 KB
/
evaluate.py
File metadata and controls
59 lines (42 loc) · 1.63 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
import sys
import numpy as np
import matplotlib.pyplot as plt
from nn_from_scratch.dataset import Dataset
from nn_from_scratch.evaluation import Evaluation
from nn_from_scratch.network import Network
from nn_from_scratch.repository import Repository
def main():
sys.stdout.write("Loading the test dataset. This may take a minute...")
sys.stdout.flush()
dataset = Dataset(split="test")
dataset.extract()
X, Y = dataset.get_features_and_labels()
sys.stdout.write("\r")
nn = Network()
repository = Repository()
repository.load(nn, "resources/weights_and_biases.json")
evaluation = Evaluation(nn)
predicted_probabilities, _ = nn.forward(X, training=True)
Y_pred = np.argmax(predicted_probabilities, axis=0)
accuracy = evaluation.accuracy(Y, Y_pred)
confusion_matrix = evaluation.confusion_matrix(Y, Y_pred)
print(f"The mean accuracy of the model in the test set is: {accuracy}\n")
print("The confusion matrix of the model is:\n")
print(confusion_matrix)
print("\n")
print("The binary confusion matrix of the model is:\n")
print(evaluation.binary_confusion_matrix(Y, Y_pred))
plot(confusion_matrix)
def plot(confusion_matrix: np.ndarray):
fig, ax = plt.subplots(1, 1)
image = ax.imshow(confusion_matrix, interpolation=None)
colorbar = fig.colorbar(image, ax=ax, orientation="horizontal", fraction=0.1)
colorbar.ax.set_xlabel("Sample count")
ax.set_xticks(range(0, 10))
ax.set_yticks(range(0, 10))
plt.xlabel("Predicted values")
plt.ylabel("Actual values")
plt.title("Confusion matrix")
plt.show()
if __name__ == "__main__":
main()