Evaluation Metrics
Accuracy Score
Compute accuracy: fraction of predictions that match the true labels. Loop over index, count matches where y_true[i]==y_pred[i], divide by n. Library: sklearn.metrics.accuracy_score(y_true, y_pred). RESULT: accuracy (rounded).
Learning path
Prerequisite: Confusion Matrix Counts.
By hand
y_true=[1,0,1,1,0,0,1,0], y_pred=[1,1,0,1,0,0,0,0]. Matches at i=0,3,4,5,7 → correct=5, n=8, accuracy=5/8=0.625.
naive.py
Replay: real traced execution (multi-file project)
y_true = [1, 0, 1, 1, 0, 0, 1, 0]
y_pred = [1, 1, 0, 1, 0, 0, 0, 0]
n = len(y_true)
correct = 0
for i in range(n):
if y_true[i] == y_pred[i]:
correct = correct + 1
accuracy = correct / n
print('RESULT:', round(accuracy, 4))
y_true ← [1, 0, 1, 1, 0, 0, 1, 0]
1y_true = [1, 0, 1, 1, 0, 0, 1, 0]2y_pred = [1, 1, 0, 1, 0, 0, 0, 0]values this step[1, 0, 1, 1, 0, 0, 1, 0]y_truey_pred ← [1, 1, 0, 1, 0, 0, 0, 0]
1y_true = [1, 0, 1, 1, 0, 0, 1, 0]2y_pred = [1, 1, 0, 1, 0, 0, 0, 0]3n = len(y_true)values this step[1, 1, 0, 1, 0, 0, 0, 0]y_predn ← 8
2y_pred = [1, 1, 0, 1, 0, 0, 0, 0]3n = len(y_true)4correct = 0values this step8ncorrect ← 0
3n = len(y_true)4correct = 05for i in range(n):values this step0correcti ← 0
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step0iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1correct ← 1
6 if y_true[i] == y_pred[i]:7 correct = correct + 18accuracy = correct / nvalues this step0 → 1correcti ← 1
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step0 → 1iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1i ← 2
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step1 → 2iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1i ← 3
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step2 → 3iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1correct ← 2
6 if y_true[i] == y_pred[i]:7 correct = correct + 18accuracy = correct / nvalues this step1 → 2correcti ← 4
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step3 → 4iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1correct ← 3
6 if y_true[i] == y_pred[i]:7 correct = correct + 18accuracy = correct / nvalues this step2 → 3correcti ← 5
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step4 → 5iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1correct ← 4
6 if y_true[i] == y_pred[i]:7 correct = correct + 18accuracy = correct / nvalues this step3 → 4correcti ← 6
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step5 → 6iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1i ← 7
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:values this step6 → 7iif y_true[i] == y_pred[i]:
5for i in range(n):6 if y_true[i] == y_pred[i]:7 correct = correct + 1correct ← 5
6 if y_true[i] == y_pred[i]:7 correct = correct + 18accuracy = correct / nvalues this step4 → 5correctfor i in range(n):
4correct = 05for i in range(n):6 if y_true[i] == y_pred[i]:accuracy ← 0.625
7 correct = correct + 18accuracy = correct / n9print('RESULT:', round(accuracy, 4))values this step0.625accuracystdout ← RESULT: 0.625
8accuracy = correct / n9print('RESULT:', round(accuracy, 4))values this stepRESULT: 0.625stdout
With scikit-learn
accuracy_score(y_true, y_pred) returns the fraction of matching pairs as a
float.
library.py
from sklearn.metrics import accuracy_score
from dalib.display import set_display
set_display()
y_true = [1, 0, 1, 1, 0, 0, 1, 0]
y_pred = [1, 1, 0, 1, 0, 0, 0, 0]
print('correct:', sum(t == p for t, p in zip(y_true, y_pred)))
acc = accuracy_score(y_true, y_pred)
print('RESULT:', round(float(acc), 4))
correct: 5
RESULT: 0.625
Implementation notes
- Accuracy = (TP+TN)/n = (2+3)/8 = 0.625; see Confusion Matrix Counts for the component counts.
- On balanced data accuracy is intuitive, but on imbalanced data a classifier predicting only the majority class can still score high — motivating precision, recall, and F1 as complementary metrics.