Satish BajagainContents

Confusion matrix

A confusion matrix shows how reference labels and predicted labels pair up. It reveals errors that a single accuracy score can hide.

Reference rows / predicted columns
ClassAB
A82
B19
Illustrative counts, not project results. Rows are reference classes; columns are predictions. Diagonal cells agree.

Intuition: count each kind of outcome

Choose an explicit class order and put each example in one cell. On this site, rows are reference classes and columns are predictions. The diagonal counts agreements. Off-diagonal cells show the direction of a confusion; swapping row and column conventions reverses that interpretation.

From counts to metrics

Let CijC_{ij} count examples with reference class ii and prediction jj. Then

N=∑ijCij,accuracy=∑iCiiN.N=\sum_{ij}C_{ij},\qquad \mathrm{accuracy}=\frac{\sum_iC_{ii}}N.

For class kk, treat it as positive and all other classes as negative:

recallk=Ckk∑jCkj,precisionk=Ckk∑iCik.\mathrm{recall}_k=\frac{C_{kk}}{\sum_jC_{kj}},\qquad \mathrm{precision}_k=\frac{C_{kk}}{\sum_iC_{ik}}.

Recall asks what fraction of reference examples of that class were recovered. Precision asks what fraction of predictions of that class were correct. If a denominator is zero, the metric needs an explicit undefined-value convention.

Read the illustrative example

The table has 20 examples and 17 diagonal agreements, so accuracy is 0.85. For class A, recall is 8/10=0.88/10=0.8, while precision is 8/9≈0.8898/9\approx0.889. There are two A examples predicted as B, and one B example predicted as A. Those error counts need not be symmetric.

F1 combines precision and recall as 2PR/(P+R)2PR/(P+R) when defined. Macro averaging gives each class equal weight; support-weighted averaging emphasizes more common classes. Neither removes the need to inspect per-class counts.

What this table does not establish

Counts do not explain whether the evaluation split was valid or the reference labels reliable. A matrix also loses temporal order: swapping two epochs leaves it unchanged. For sleep staging, a hypnogram and subject-level uncertainty can reveal structure that an aggregate matrix cannot.

Where you will see this

The sleep-staging page withholds evaluation exports until the research is released. Cohen's kappa uses the same count table to compare agreement with a marginal-frequency baseline.

References