Accuracy
- Machine Learning course
- Beginner
- Lesson 62/112
- 5 min
Accuracy is the ratio of correct predictions to total predictions.
Accuracy
Accuracy is the share of correct predictions among all predictions. The score method of classifiers returns it:
from sklearn.metrics import accuracy_score
y_true = [1, 0, 1, 1, 0]
y_pred = [1, 0, 0, 1, 0]
print(accuracy_score(y_true, y_pred)) # 0.8
print((np.array(y_true) == np.array(y_pred)).mean()) # the sameIt is the easiest metric to understand and gives a good summary when the classes are balanced.
The trap with imbalanced classes
In data where 1% of transactions are fraud, a model saying "everything is normal" gets 99% accuracy but catches no fraud at all:
y_true = np.array([0] * 99 + [1])
y_pred = np.zeros(100, dtype=int)
print(accuracy_score(y_true, y_pred)) # 0.99, yet uselessThat's why with imbalanced problems the confusion matrix, precision and recall are examined alongside accuracy.
Practice Compare with a baseline
Build a baseline that always predicts the most frequent class with DummyClassifier(strategy="most_frequent"). Print the test accuracy of both it and a LogisticRegression(max_iter=5000) model with round(..., 3) on separate lines.
Starter code
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
# buraya yazSample solution
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
temel = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
m = LogisticRegression(max_iter=5000).fit(X_train, y_train)
print(round(temel.score(X_test, y_test), 3))
print(round(m.score(X_test, y_test), 3))Expected output
0.629 0.937
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If 170 of 200 predictions are correct, what is the accuracy?
- 0.70
- 1.18
- 0.85 Correct answer
- 0.17
Explanation: 170 / 200 = 0.85.
2 Compute the accuracy with the metric function.
acc = ____(y_test, y_pred)
acc = accuracy_score(y_test, y_pred)
Explanation: accuracy_score(y_true, y_pred): truth first, then prediction.
3 Match the situations.
| Term | Meaning |
|---|---|
Balanced classes |
Accuracy is meaningful |
99% one class |
Accuracy is misleading |
m.score(X, y) |
Accuracy for a classifier |
y_true == y_pred |
Correct-prediction mask |