Example 1: import sklearn.metrics from plot_confusion_matrix
from sklearn.metrics import plot_confusion_matrix
Example 2: confusion matrix python
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)
Example 3: confusion matrix with labels sklearn
import pandas as pd
y_true = pd.Series([2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2])
y_pred = pd.Series([0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2])
pd.crosstab(y_true, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
Example 4: sklearn plot confusion matrix
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, plot_confusion_matrix
clf =
clf.fit(X, y)
y_pred = clf.predict(X)
M = confusion_matrix(y, y_pred)
tn, fp, fn, tp = M.ravel()
plot_confusion_matrix(clf, X, y)
plt.show()
Example 5: confusion matrix
sudo pacman -S tk
Example 6: confusion matrix with labels sklearn
Predicted 0 1 2 All
True
0 3 0 0 3
1 0 1 2 3
2 2 1 3 6
All 5 2 5 12