-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
64 lines (55 loc) · 1.83 KB
/
evaluation.py
File metadata and controls
64 lines (55 loc) · 1.83 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
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
def evaluate_model(model, X_test, y_test, verbose=2):
"""
Evaluate the model on the test data.
"""
return model.evaluate(X_test, y_test, verbose=verbose)
def plot_history(history):
"""
Plot training history of the model - Accuracy and Loss
"""
# Plot accuracy
plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.title("Model Accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# Plot loss
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.title("Model Loss")
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
def predict_classes(model, X_test, classes):
"""
Make predictions over the testing set and convert integer predictions to class labels.
"""
y_pred = model.predict(X_test)
y_pred = np.argmax(y_pred, axis=1)
y_pred_class = [classes[i] for i in y_pred]
return y_pred_class
def plot_confusion_matrix(y_true, y_pred, labels):
"""
Plots the confusion matrix.
"""
matrix = confusion_matrix(y_true, y_pred)
fig, ax = plt.subplots(figsize=(12, 8.5))
plt.imshow(matrix)
ax.set_xticks(range(len(labels)))
ax.set_xticklabels(labels, rotation=25)
ax.set_yticks(range(len(labels)))
ax.set_yticklabels(labels)
for i, true_label in enumerate(matrix):
for j, predicted_label in enumerate(true_label):
ax.text(j, i, matrix[i, j], ha="center", va="center", color="w")
plt.tick_params(axis=u'both', which=u'both', length=0)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()