xxxxxxxxxx
from sklearn.metrics import confusion_matrix
# Ground truth values
y_true = [2, 0, 2, 2, 0, 1]
# Predicted values
y_pred = [0, 0, 2, 2, 0, 2]
# Compute the confusion matrix
cm = confusion_matrix(y_true, y_pred)
print(cm)
xxxxxxxxxx
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
sns.heatmap(conf_mat, square=True, annot=True, cmap='Blues', fmt='d', cbar=False)
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
By definition, entry i,j in a confusion matrix is the number of
observations actually in group i, but predicted to be in group j.
Scikit-Learn provides a confusion_matrix function:
from sklearn.metrics import confusion_matrix
y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
confusion_matrix(y_actu, y_pred)
# Output
# array([[3, 0, 0],
# [0, 1, 2],
# [2, 1, 3]], dtype=int64)
xxxxxxxxxx
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred_test.round()))
print(classification_report(y_test, y_pred_test.round()))
# Output:
[[99450 250]
[ 4165 11192]]
precision recall f1-score support
0 0.96 1.00 0.98 99700
1 0.98 0.73 0.84 15357
accuracy 0.96 115057
macro avg 0.97 0.86 0.91 115057
weighted avg 0.96 0.96 0.96 115057
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
df_confusion = pd.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)