Multi-Class Measurements

from sklearn.metrics import classification_report

print(classification_report(y_valid, pipe_tree.predict(X_valid),
        target_names=["non-fraud", "fraud"]))
              precision    recall  f1-score   support

   non-fraud       1.00      1.00      1.00     59708
       fraud       0.69      0.75      0.72       102

    accuracy                           1.00     59810
   macro avg       0.85      0.87      0.86     59810
weighted avg       1.00      1.00      1.00     59810
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score

digits = load_digits()
digits.images[-1]
array([[ 0.,  0., 10., 14.,  8.,  1.,  0.,  0.],
       [ 0.,  2., 16., 14.,  6.,  1.,  0.,  0.],
       [ 0.,  0., 15., 15.,  8., 15.,  0.,  0.],
       [ 0.,  0.,  5., 16., 16., 10.,  0.,  0.],
       [ 0.,  0., 12., 15., 15., 12.,  0.,  0.],
       [ 0.,  4., 16.,  6.,  4., 16.,  6.,  0.],
       [ 0.,  8., 16., 10.,  8., 16.,  8.,  0.],
       [ 0.,  1.,  8., 12., 14., 12.,  1.,  0.]])
from sklearn.neighbors import KNeighborsClassifier

X_train_digits, X_test_digits, y_train_digits, y_test_digits = train_test_split(
    digits['data'] / 16., digits['target'], random_state=0)
    
knn = KNeighborsClassifier().fit(X_train_digits, y_train_digits)
pred = knn.predict(X_test_digits)
print("Accuracy: ", round(accuracy_score(y_test_digits, pred), 4))
Accuracy:  0.98

Confusion matrix for multi-class

ConfusionMatrixDisplay.from_estimator(knn, X_test_digits, y_test_digits, cmap='gray_r');
plt.show()
print(classification_report(y_test_digits, pred, digits=4))
              precision    recall  f1-score   support

           0     1.0000    1.0000    1.0000        37
           1     0.9767    0.9767    0.9767        43
           2     1.0000    0.9773    0.9885        44
           3     0.9574    1.0000    0.9783        45
           4     1.0000    0.9737    0.9867        38
           5     0.9592    0.9792    0.9691        48
           6     0.9811    1.0000    0.9905        52
           7     0.9600    1.0000    0.9796        48
           8     1.0000    0.9167    0.9565        48
           9     0.9787    0.9787    0.9787        47

    accuracy                         0.9800       450
   macro avg     0.9813    0.9802    0.9805       450
weighted avg     0.9805    0.9800    0.9799       450

Macro average vs weighted average

print(classification_report(y_test_digits, pred, digits=4))
              precision    recall  f1-score   support

           0     1.0000    1.0000    1.0000        37
           1     0.9767    0.9767    0.9767        43
           2     1.0000    0.9773    0.9885        44
           3     0.9574    1.0000    0.9783        45
           4     1.0000    0.9737    0.9867        38
           5     0.9592    0.9792    0.9691        48
           6     0.9811    1.0000    0.9905        52
           7     0.9600    1.0000    0.9796        48
           8     1.0000    0.9167    0.9565        48
           9     0.9787    0.9787    0.9787        47

    accuracy                         0.9800       450
   macro avg     0.9813    0.9802    0.9805       450
weighted avg     0.9805    0.9800    0.9799       450


Macro average: Give equal importance to all classes.

Weighted average: Weighted by the number of samples in each class and divide by the total number of samples.

Let’s apply what we learned!