liveBook · Manning

Accuracy

As explained in Statistics Every Programmer Needs

Model accuracy is the proportion of predictions that a model assigns correctly to the target outcomes.

Accuracy is the share of predictions that match their actual class labels. For binary classification, accuracy = (true positives + true negatives) / total predictions; multiply by 100 to express it as a percentage. Each observation must have a prediction. section 5.2.4

Table 5.1 Confusion matrix for raisin classification. A confusion matrix stores classification metrics from a regression with actual and predicted binary outcomes.
Predicted
Kecimen (0)
Besni (1)
Actual
Kecimen (0)
tn
fp
Besni (1)
fn
tp

The confusion matrix places correct predictions on the true-positive and true-negative cells. Its other cells count false positives and false negatives. section 5.2.4

Raisin classification

A logistic regression correctly identifies 397 observations of class 0 and 384 of class 1. With 900 observations, accuracy is (397 + 384) / 900 × 100 = 86.78%. The remaining 119 predictions give a misclassification rate of 13.22%. section 5.2.4

For a test set, pass actual and predicted labels to `metrics.accuracy_score(y_test, y_pred)`. Multiply its result by 100 for a percentage. A decision tree predicts 61% of fourth-down conversion outcomes correctly; a random forest reaches 62% on the same test data. The small difference is useful for comparison, but accuracy alone does not show which class each model handles better. section 6.3.4 section 6.4.3

>>> from sklearn.metrics import confusion_matrix,
>>> conf_matrix = confusion_matrix(y_test, y_pred)
>>> print(conf_matrix)
[[71 67]
 [36 91]]

The decision tree’s confusion matrix shows 71 correct class-0 predictions and 91 correct class-1 predictions, alongside 67 false positives and 36 false negatives. Inspecting these counts reveals errors hidden by the overall 61% accuracy. section 6.3.4

Judge whether an accuracy rate is useful in light of the errors it conceals. Missing a serious medical diagnosis and incorrectly flagging a healthy person have different consequences; sensitivity, specificity, and the confusion matrix help distinguish such mistakes. ROC AUC offers a separate assessment of discrimination and need not match accuracy. section 5.2.4

How Statistics Every Programmer Needs covers it

How deep it goes
Defines a confusion-matrix formula, calculates a percentage manually, demonstrates scikit-learn scoring, and examines errors by class.
Examples
Calculates 86.78% accuracy for raisin classes and compares 61% decision-tree accuracy with 62% random-forest accuracy on fourth-down outcomes.
What it assumes
Assumes paired actual and predicted binary labels; defines the confusion-matrix counts used in the calculation.
What you can do after
Apply it via scikit-learn `metrics.accuracy_score` to test labels and predictions, then inspect a confusion matrix for class disparities.
How it connects
Connects accuracy to confusion matrices, misclassification rate, sensitivity, specificity, ROC AUC, logistic regression, decision trees, and random forests.

Also explained in

Compare how 32 books explain accuracy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.