1import matplotlib.pyplot as plt
2from sklearn.metrics import confusion_matrix, plot_confusion_matrix
3
4clf = # define your classifier (Decision Tree, Random Forest etc.)
5clf.fit(X, y) # fit your classifier
6
7# make predictions with your classifier
8y_pred = clf.predict(X)
9
10# optional: get true negative (tn), false positive (fp)
11# false negative (fn) and true positive (tp) from confusion matrix
12M = confusion_matrix(y, y_pred)
13tn, fp, fn, tp = M.ravel()
14
15# plotting the confusion matrix
16plot_confusion_matrix(clf, X, y)
17plt.show()
1import pandas as pd
2y_true = pd.Series([2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2])
3y_pred = pd.Series([0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2])
4
5pd.crosstab(y_true, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
6