from sklearn.metrics import confusion_matrix y_true = [2, 0, 2, 2, 0, 1] y_pred = [0, 0, 2, 2, 0, 2] matrix = confusion_matrix(y_true, y_pred) matrix.diagonal()/matrix.sum(axis=1)