Confusion Matrix
A Confusion Matrix will tell us how our model confuses specific categories of data. This may be more important than the overall accuracy of our model, if it cannot identify certain categories at all.
We can draw this matrix with the below code:
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import matplotlib.pyplot as plt
class estimator:
_estimator_type = ''
classes_=[]
def __init__(self, model, classes):
self.model = model
self._estimator_type = 'classifier'
self.classes_ = classes
def predict(self, X):
y_prob= self.model.predict(X, verbose=False)
y_pred = y_prob.argmax(axis=1)
return y_pred
def plot_confusion_matrix(y_test = [], predictions = [], labels = []):
classifier = estimator(model, labels)
predictions = classifier.predict(x_test)
fig, ax=plt.subplots(figsize=(20,20))
disp = ConfusionMatrixDisplay.from_predictions(y_true=y_test, y_pred=predictions,
display_labels=labels, normalize='true',
xticks_rotation='vertical', cmap='Blues',
colorbar=False, values_format='.2f', ax=ax)
plt.show()
plot_confusion_matrix(y_test=y_test, predictions=predictions, labels=labels)