3 Classification Fundamentals

Learn how classification models use labeled examples and features to predict categories, and how to evaluate their predictions with a test set and performance measures.

and its classes

is a type of supervised machine learning: a model learns from examples with known categories and then predicts categories for new inputs. The categories are called classes. A task with two possible classes is , while a task with more than two classes is .

Examples, labels, and

A pairs an input with its known answer, or . are the measurable properties used to describe the input. For an email spam classifier, might include whether the message contains a link, the number of words in its subject, and how often certain terms appear; the is spam or not spam.

During training, a learning algorithm uses many labeled examples to fit a model. The model can then use from an unseen email to predict its . The is the target to predict, not a feature to supply to the model. Training data should represent the task well, and the used should be available when predictions are made.

Evaluating predictions

To estimate how well a classifier works on new examples, keep some labeled data separate from training as a . Compare each prediction on this held-out data with its known .

A summarizes results by comparing predicted and actual classes:

  • True positive (TP): predicted positive and actually positive.

  • False positive (FP): predicted positive but actually negative.

  • False negative (FN): predicted negative but actually positive.

  • True negative (TN): predicted negative and actually negative.

For example, consider a of 100100 emails. Of 2020 actual spam messages, the classifier correctly identifies 1212 and misses 88. Of 8080 non-spam messages, it incorrectly flags 33 as spam and correctly leaves 7777 unflagged. The results are TP=12\mathrm{TP}=12, FN=8\mathrm{FN}=8, FP=3\mathrm{FP}=3, and TN=77\mathrm{TN}=77.

, , and

measures the fraction of all predictions that are correct:

Accuracy=TP+TNTP+TN+FP+FN\mathrm{Accuracy}=\frac{\mathrm{TP}+\mathrm{TN}}{\mathrm{TP}+\mathrm{TN}+\mathrm{FP}+\mathrm{FN}}

For the email example, is 12+77100=89%\frac{12+77}{100}=89\%.

measures the fraction of positive predictions that are correct:

Precision=TPTP+FP\mathrm{Precision}=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}

In the example, is 1215=80%\frac{12}{15}=80\%. measures the fraction of actual positive examples that are found:

Recall=TPTP+FN\mathrm{Recall}=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}

In the example, is 1220=60%\frac{12}{20}=60\%.

alone can be misleading when one class is much more common than another. For instance, a model that predicts “not spam” for every email in a dataset that is mostly non-spam may have high but fail to identify spam. matters when false alarms are costly, while matters when missing positive cases is costly. Choose measures that fit the task, and inspect errors by class rather than relying on one score.