Classification and Prediction · Data Mining

Accuracy and Error Measures

Watch lectures

Accuracy and Error Measures

1. Classifier Accuracy, Error Rate and the Confusion Matrix

For a test set of nn tuples (binary classes: positive P, negative N):

Accuracy=TP+TNTP+TN+FP+FN,Error rate=1−Accuracy=FP+FNn\text{Accuracy} = \frac{TP+TN}{TP+TN+FP+FN},\qquad \text{Error rate} = 1-\text{Accuracy} = \frac{FP+FN}{n}

Confusion / contingency matrix

Predicted Positive Predicted Negative Total
Actual Positive TP (true positive) FN (false negative) P
Actual Negative FP (false positive) TN (true negative) N
Term Definition
Sensitivity / Recall / True Positive Rate TPP=TPTP+FN\dfrac{TP}{P}=\dfrac{TP}{TP+FN}
Specificity / True Negative Rate TNN=TNTN+FP\dfrac{TN}{N}=\dfrac{TN}{TN+FP}
Precision TPTP+FP\dfrac{TP}{TP+FP}
F1 / F-score 2×precision×recallprecision+recall\dfrac{2 \times precision \times recall}{precision + recall}
False Positive Rate FPN=1−\dfrac{FP}{N}=1- specificity
False Negative Rate FNP=1−\dfrac{FN}{P}=1- sensitivity

Worked example — 200 test tuples, 120 positive, 80 negative. Classifier predicts 105 positive of which 95 are correct, and 95 negative of which 70 are correct:

TP=95, FP=10, FN=25, TN=70TP=95,\ FP=10,\ FN=25,\ TN=70
Accuracy=95+70200=82.5%,Error=17.5%,Sensitivity=95120=79.2%,Specificity=7080=87.5%\text{Accuracy}=\frac{95+70}{200}=82.5\%,\quad \text{Error}=17.5\%,\quad \text{Sensitivity}=\frac{95}{120}=79.2\%,\quad \text{Specificity}=\frac{70}{80}=87.5\%
Precision=95105=90.5%,F1=2(0.905)(0.792)0.905+0.792=0.845\text{Precision}=\frac{95}{105}=90.5\%,\qquad F_1=\frac{2(0.905)(0.792)}{0.905+0.792}=0.845

2. Which Measure to Use?

Accuracy alone is misleading with imbalanced classes. For 1000 tuples with 990 negative and 10 positive, a "always negative" classifier has 99% accuracy but 0% recall and 0% F1.

  • Rare disease / fraud detection → maximise recall, accept lower precision.
  • Spam filtering / credit approval → emphasise precision.
  • Balanced view → F1 or AUC.
  • Cost-sensitive → weighted error:
Ecost=∑ijC(i,j)⋅count(predicted i, actual j),Cost per tuple=EcostnE_{\text{cost}}=\sum_{ij} C(i,j)\cdot \text{count}(\text{predicted } i,\ \text{actual } j),\qquad \text{Cost per tuple}=\frac{E_{\text{cost}}}{n}

3. ROC Curves and AUC

Plot TPR (sensitivity) against FPR (1 − specificity) while varying the classification threshold; a perfect classifier is the point (0,1) and a random guess is the diagonal.

AUC=∫01TPR d(FPR),0.5=random, 1.0=perfect\text{AUC} = \int_{0}^{1}\text{TPR}\,d(\text{FPR}),\qquad 0.5 = \text{random},\ 1.0 = \text{perfect}
 TPR
 1.0 |---------___------  model B (better, larger AUC)
     |      __/
     |    _/  ... model A (moderate)
     |  _/   /
 0.5 | /   /  ---- random classifier (diagonal)
     |/   /
 0.0 +-------------------- FPR
     0.0        0.5      1.0

Other tools: precision-recall curve, lift chart (cumulative gain), cost curve.

4. Measures for Numeric Predictors

MAE=1n∑i∣yi−y^i∣,MSE=1n∑i(yi−y^i)2,RMSE=MSE\text{MAE}=\frac{1}{n}\sum_{i}|y_i-\hat y_i|,\qquad \text{MSE}=\frac{1}{n}\sum_i (y_i-\hat y_i)^2,\qquad \text{RMSE}=\sqrt{\text{MSE}}
Relative error=∣yi−y^i∣∣yi∣,RMSErel=RMSEyˉ,R2=1−∑(yi−y^i)2∑(yi−yˉ)2\text{Relative error}=\frac{|y_i-\hat y_i|}{|y_i|},\qquad \text{RMSE}_{rel}=\frac{\text{RMSE}}{\bar y},\qquad R^{2}=1-\frac{\sum(y_i-\hat y_i)^2}{\sum (y_i-\bar y)^2}

MSE is sensitive to outliers (squared); MAE is more robust. RMSE has the same unit as the target, so it is more interpretable.

5. Estimators of Accuracy — Sampling and Confidence Intervals

Accuracy measured on a finite test set is only an estimate. If the model makes xx correct predictions out of nn independent trials, the error follows a binomial distribution whose standard deviation is

stddev (error)=acc (1−acc)n\text{stddev (error)} = \sqrt{\frac{acc\,(1-acc)}{n}}

100(1−α)100(1-\alpha)% confidence interval for accuracy:

acc±z(1−α/2)acc(1−acc)n(z≈1.645 for 90%, 1.96 for 95%, 2.58 for 99%)acc \pm z_{(1-\alpha/2)}\sqrt{\frac{acc(1-acc)}{n}}\qquad (z \approx 1.645\ \text{for }90\%,\ 1.96\ \text{for }95\%,\ 2.58\ \text{for }99\%)

Numerical example — accuracy 0.85 on 200 tuples: σ=0.85×0.15/200=0.0252\sigma=\sqrt{0.85\times0.15/200}=0.0252; the 95% CI is 0.85±1.96(0.0252)=0.85±0.0490.85 \pm 1.96(0.0252) = 0.85\pm0.049, i.e. [80.1%, 89.9%]. Larger test sets give tighter intervals (σ∝1/n\sigma \propto 1/\sqrt{n}).

Comparing two classifiers honestly is harder than it looks — the tests are not independent (same training/test data), so use paired methods:

Method Idea
McNemar's test Compares the two classifiers on a 2×22\times2 table of disagreement counts f01,f10f_{01}, f_{10}; χ2=(∣f01−f10∣−1)2f01+f10\chi^2=\frac{(|f_{01}-f_{10}|-1)^2}{f_{01}+f_{10}} with 1 d.o.f., or an exact binomial test
kk-fold cross-validated paired tt-test Compare the per-fold differences of error rates
Sign test / Wilcoxon signed-rank Non-parametric comparisons

6. Occam's Razor, Bias–Variance and Model Cost

Between two models with equal test error, prefer the simpler one (fewer parameters/rules/leaves) — it is more likely to generalise.

E[error]=Bias2+Variance+noise,Underfit⇒high bias,Overfit⇒high variance\mathbb{E}[\text{error}]=\text{Bias}^2+\text{Variance}+\text{noise},\qquad \text{Underfit} \Rightarrow \text{high bias},\quad \text{Overfit} \Rightarrow \text{high variance}

Expected error, or a pessimistic estimate used for pruning a tree (with f=error at nodef=\text{error at node}, NN = tuples at the node, kk = number of leaves):

e=f+k2N,or Laplace-corrected f+1N+ke=\frac{f+\frac{k}{2}}{N},\qquad \text{or Laplace-corrected } \frac{f+1}{N+k}

7. Other Useful Measures

Measure Formula Note
Lift P(y∣X)P(y)\frac{P(y|X)}{P(y)} >1 means the rule improves on the base rate
Kappa (Cohen) acc−Prand1−Prand\frac{acc-P_{rand}}{1-P_{rand}} Agreement corrected for chance
Geometric mean sensitivity×specificity\sqrt{sensitivity \times specificity} Robust for imbalance
Matthews correlation (MCC) TP⋅TN−FP⋅FN(TP+FP)(TP+FN)(TN+FP)(TN+FN)\frac{TP\cdot TN - FP\cdot FN}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}} Balanced single number (−1..+1)
Brier score 1n∑(pi−yi)2\frac{1}{n}\sum (p_i-y_i)^2 Calibration of predicted probabilities
Log loss −1n∑[yiln⁡pi+(1−yi)ln⁡(1−pi)]-\frac{1}{n}\sum[y_i\ln p_i + (1-y_i)\ln(1-p_i)] Penalises confident wrong probabilities

8. Exam Points

  • Build a confusion matrix from given data and compute accuracy, error, sensitivity, specificity, precision, F1 — 10 marks (very frequent numerical).
  • Explain ROC and AUC with a sketch — 5 marks.
  • Compute a confidence interval for accuracy and interpret it — 5 marks.
  • Explain MSE, RMSE, MAE and R2R^2 for predictors — 5 marks.
  • Why is accuracy misleading for imbalanced data? — 3 marks.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Data Mining modules