xxxxxxxxxx
import seaborn as sns
from sklearn.metrics import confusion_matrix
# y_test : actual labels or target
# y_preds : predicted labels or target
sns.heatmap(confusion_matrix(y_test, y_preds),annot=True);
xxxxxxxxxx
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, plot_confusion_matrix
clf = # define your classifier (Decision Tree, Random Forest etc.)
clf.fit(X, y) # fit your classifier
# make predictions with your classifier
y_pred = clf.predict(X)
# optional: get true negative (tn), false positive (fp)
# false negative (fn) and true positive (tp) from confusion matrix
M = confusion_matrix(y, y_pred)
tn, fp, fn, tp = M.ravel()
# plotting the confusion matrix
plot_confusion_matrix(clf, X, y)
plt.show()
xxxxxxxxxx
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(test_Y, predictions_dt)
cm
# after creating the confusion matrix, for better understaning plot the cm.
import seaborn as sn
plt.figure(figsize = (10,8))
# were 'cmap' is used to set the accent colour
sn.heatmap(cm, annot=True, cmap= 'flare', fmt='d', cbar=True)
plt.xlabel('Predicted_Label')
plt.ylabel('Truth_Label')
plt.title('Confusion Matrix - Decision Tree')
xxxxxxxxxx
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import plot_confusion_matrix
clf = LogisticRegression()
clf.fit(X_train,y_train)
disp = plot_confusion_matrix(clf,X_test,y_test,cmap="Blues",values_format='.3g')
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
xxxxxxxxxx
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# Sample true and predicted labels
true_labels = np.array([0, 0, 1, 1, 2, 2])
predicted_labels = np.array([0, 0, 1, 1, 1, 2])
# Compute confusion matrix
cm = confusion_matrix(true_labels, predicted_labels)
# Define class labels
class_labels = ['Class 0', 'Class 1', 'Class 2']
# Plot confusion matrix
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(class_labels))
plt.xticks(tick_marks, class_labels, rotation=45)
plt.yticks(tick_marks, class_labels)
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
# Fill confusion matrix cells with values
thresh = cm.max() / 2.
for i, j in np.ndindex(cm.shape):
plt.text(j, i, format(cm[i, j], 'd'), horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.show()
xxxxxxxxxx
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
xxxxxxxxxx
from sklearn import metrics
metrics.ConfusionMatrixDisplay.from_predictions(true_y, predicted_y).plot()
xxxxxxxxxx
from sklearn.metrics import confusion_matrix
matrix_confusion = confusion_matrix(y_test, y_pred)
sns.heatmap(matrix_confusion, square=True, annot=True, cmap='Blues', fmt='d', cbar=False