-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmatriz_confusion.py
34 lines (28 loc) · 1.07 KB
/
matriz_confusion.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
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
def graficar_matriz_de_confusion(y_ref, y_pred, clases, cmap=plt.cm.Blues):
titulo = 'Matriz de confusión'
cm = confusion_matrix(y_ref, y_pred)
# Generar figura
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
xticklabels=clases, yticklabels=clases,
title=titulo,
ylabel='Categoría real',
xlabel='Predicción')
# Rotar y alinear ticks
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Rellenar gráfica con los valores de la matriz de confusión
fmt = 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
plt.show()