Concepts / Confusion Matrix and Classification Reports

Confusion Matrix and Classification Reports

A reported probability of 1.0 from such a model does not mean the prediction is reliable.

  • Programming

The Hidden Cutoff Behind Every Label

When a classifier labels a sample as positive or negative, it feels as though the model simply answers the question. Underneath, the model usually computes a score for each sample and then compares that score against a fixed cutoff. That cutoff is the decision threshold. Because it is chosen once and then applied silently, it is easy to forget that it exists at all, and easy to assume it is the only correct place to draw the line.

This article starts from that hidden assumption and tests it. The confusion matrix and the classification report describe the predictions a model made. But those predictions have already discarded information about how confident the model was. Recovering that information, and seeing what happens when the cutoff moves, is the subject of this lesson.

What do you think happens?

A model predicts class 1 only when its score is above 0. Is 0 the honest midpoint that makes the model fair, or is it just a convention?

  • It is the honest midpoint, so any other cutoff is manipulation

  • It is a convention the library uses when no other instruction is given

Reveal answer
Answer:

It is a convention the library uses when no other instruction is given.

For binary classification the default is 0 for a decision_function output and 0.5 for a predict_proba output. Those are where the library draws the line when you give it no instruction. Whether that line serves your application depends on what errors you can tolerate, and the default has no opinion about that.

From a Score to a Predicted Class

Think of the decision_function output as a score along a line. Every sample has one score. The cutoff splits that line into two regions, and any sample scoring above the cutoff is assigned to class 1. Everything at or below the cutoff falls into the class 0 region. The diagram below traces five samples through this process.

Sample 1score 1.3 gives class 1Sample 2score 0.4 gives class 1Sample 3score -0.2 gives class 0Sample 4score -0.8 gives class 0Sample 5score -1.5 gives class 0
Where does each sample's score fall relative to the cutoff at 0, and which side of the cutoff sends it to class 1 versus class 0?

The scores in this diagram are illustrative values chosen to show the rule, not results from the source. The rule itself is the point: the predicted class is decided entirely by which side of the cutoff a score lands on.

Models with a predict_proba method follow the same logic on a different scale. Their outputs fall from 0 to 1 and can be read as probabilities. The default threshold of 0.5 means a sample is labeled positive when the model is more than 50 percent sure it belongs to the positive class. Raising the threshold requires the model to be more confident before it makes a positive decision, which makes negative decisions easier to reach.

Setting Up an Imbalanced Problem

Consider an imbalanced problem with many negative samples and few positive ones. The classifier here is a kernel SVM, and its default cutoff for class 1 is a decision_function value greater than 0. Suppose your application values catching positive cases more than avoiding false alarms, as in a cancer screening setting. You want to change the cutoff so that more samples are flagged as class 1. Before you look at any results, make a prediction about what happens to the two measures you care about.

Worked example

Reading the report at the default cutoff

The test set contains 113 samples: 104 from class 0 and 9 from class 1. At the default cutoff, the classification report shows class 0 precision 0.97 and recall 0.89, and class 1 precision 0.35 and recall 0.67. What does this tell you about where the classifier's effort goes?

  1. 1
    Count the class sizes

    Class 0 has 104 samples and class 1 has only 9. The test set is heavily imbalanced toward class 0.

  2. 2
    Read the class 0 row

    Class 0 precision is 0.97 and recall is 0.89. Those are strong numbers because the classifier has many class 0 examples to learn from.

  3. 3
    Read the class 1 row

    Class 1 precision is 0.35 and recall is 0.67. The rare class receives much weaker results.

  4. 4
    Connect size to effort

    Because class 0 is so much larger, the classifier spends most of its effort getting class 0 right. Class 1 is where the threshold change is meant to help.

Result

At the default cutoff, class 1 is the weak spot: 67 percent of true class 1 samples are found, but only 35 percent of class 1 predictions are correct.

Tracing the Confusion Matrix Cells

A confusion matrix sorts every test sample into one of four cells by comparing its true class with its predicted class. A true positive is a class 1 sample predicted as class 1. A false positive is a class 0 sample predicted as class 1. A false negative is a class 1 sample predicted as class 0. A true negative is a class 0 sample predicted as class 0. Applying a threshold is the step that fills these cells, because the predicted class is what the threshold produces.

The reported figures constrain the counts. Class 1 recall of 0.67 on 9 positive samples means about 6 are found and 3 are missed. Class 1 precision of 0.35 with 6 true positives implies roughly 11 false positives. The table below shows one set of counts consistent with the reported report at the default cutoff. It is a reconstruction for illustration, not a count printed in the source.

Predicted class 0Predicted class 1
True class 0 (104 samples)93 true negatives11 false positives
True class 1 (9 samples)3 false negatives6 true positives

One set of counts consistent with the default-cutoff report (illustrative reconstruction)

Rule

Precision divides true positives by all predicted positives, so every false alarm lowers it. Recall divides true positives by all actual positives, so every missed positive lowers it. Both depend on the same confusion matrix cells, which is why changing the threshold moves them together through the cell counts.

Moving the Cutoff and Watching the Trade-Off

Now lower the cutoff for class 1 from 0 to -0.8 and evaluate on the same test set. The class 1 rows should be read first, since they are the ones the change was meant to improve. Then check whether the overall averages moved in the same direction, because a single class can hide what happens to the other.

Class 1 recall0.67Class 1 recall1.00Class 1 precision0.35Class 1 precision0.32risesfalls
When the cutoff shifts left, how do class 1 recall and precision change on the same test set?

In the source example, lowering the cutoff from 0 to -0.8 raised class 1 recall from 0.67 to 1.00 and lowered class 1 precision from 0.35 to 0.32. Recall went up because every true class 1 sample is now caught. Precision went down because the extra positive predictions include more false alarms. This is the general pattern: lowering the threshold for the positive class tends to raise recall and lower precision, and raising it tends to do the opposite.

The reason is geometric. Moving the cutoff leftward enlarges the class 1 region of the score line, so more samples land in it. The source describes this as classifying a larger region of space as class 1, which is visible in the heat map of the decision function.

The same move can be traced through the cells. Under the illustrative reconstruction, a cutoff of -0.8 that captures all 9 class 1 samples with a precision of 0.32 implies about 28 predicted positives, so about 19 are false positives. That leaves roughly 85 true negatives out of 104 class 0 samples. These counts are a consistent illustration of the reported figures, and the direction of change is what matters: false negatives fell from 3 to 0, while false positives rose.

What do you think happens?

If you raise the class 1 threshold above the default instead of lowering it, what should happen to class 1 recall and precision?

  • Recall rises and precision falls

  • Recall falls and precision tends to rise

  • Both rise together

Reveal answer
Answer:

Recall falls and precision tends to rise

Lowering the positive-class threshold tends to raise recall and lower precision, and raising it tends to do the opposite. Fewer positive predictions means fewer false alarms but also fewer true positives caught.

Choosing the Cutoff Without Touching the Test Set

Once the cutoff is understood as a tunable choice, the next question is how to tune it. It is tempting to scan several thresholds on the test set and keep the one that looks best. That is a mistake. Setting a decision threshold on the test set is likely to yield overly optimistic results, in the same way that tuning any other parameter on the test set would.

Training datafit the modelValidation datacompare candidatethresholdsChosen thresholdfits your prioritiesTest dataevaluate oncemodel is fixedpick oneapply once
Through which data splits is the threshold selected, and at what stage is the test set kept untouched?

Use a validation set or cross-validation to compare candidate thresholds. Choose the one that fits your priorities on that held-out data, then evaluate once on the test set. The test set stays untouched during selection, so its final report remains an honest estimate. The source gives no rule of thumb for the exact threshold value, because the range of the decision_function can be arbitrary. The threshold has to be found empirically for the model and data at hand.

Mistakes Learners Make With Thresholds

  • Common mistake

    Treating the default cutoff as the correct one

    What it looks like

    Keeping the 0 cutoff for decision_function because it looks neutral

    Why it fails

    The default is a convention the library uses when given no other instruction. It does not account for the errors your application can tolerate.

    Do this instead

    Decide which error matters more for your problem, then choose the cutoff that serves it.

  • Common mistake

    Tuning the threshold on the test set

    What it looks like

    Trying several cutoffs on the test set and keeping the one with the best report

    Why it fails

    Test-set tuning gives overly optimistic results, and the test set no longer provides an honest final estimate.

    Do this instead

    Compare candidate thresholds on a validation set or through cross-validation, then evaluate once on the test set.

  • Common mistake

    Reading only the overall average

    What it looks like

    Noticing that the average looks stable after moving the cutoff

    Why it fails

    A single class can hide what happens to the other. The class 1 rows may have changed a lot even when the averages barely move.

    Do this instead

    Read the class 1 rows first, then check the averages.

  • Common mistake

    Assuming a probability of 1.0 is certain

    What it looks like

    Trusting a full-depth DecisionTree's probability of 1.0 as a reliable answer

    Why it fails

    Not all models provide realistic uncertainty, and a full-depth tree is always 100 percent sure of its decisions even when often wrong.

    Do this instead

    Check calibration and evaluate the model on held-out data rather than relying on the reported probability alone.

Trace a Threshold Change Yourself

MEDIUM

A test set has 20 samples of class 1 and 80 of class 0. At the default cutoff, class 1 recall is 0.50 and class 1 precision is 0.60. You lower the class 1 cutoff and class 1 recall rises to 0.80. Predict what should happen to class 1 precision, and explain which confusion matrix cells changed to produce that result.

Hints
  • Think about what a higher recall means for the number of false negatives.
  • Precision divides true positives by all predicted positives, so count how many new positive predictions were added and how many of them were correct.
  • Lowering the cutoff enlarges the class 1 region, so new predictions may include both real positives and new false alarms.
MEDIUM

Choose a threshold for a model you are working with. First, list two candidate cutoffs. Then write down which one you would pick if false alarms were expensive and which one you would pick if missed positives were expensive. Finally, state which data split you would use to compare them and confirm that the test set is not used until the final evaluation.

Hints
  • Your priority determines whether precision or recall matters more.
  • Validation data or cross-validation folds are where candidate thresholds get compared.

Carrying the Threshold Idea Forward

  1. 1
    A predicted class comes from comparing a decision_function score or a predict_proba output against a threshold, with defaults of 0 and 0.5.
  2. 2
    Lowering the positive-class threshold tends to raise recall and lower precision, and raising it tends to do the opposite.
  3. 3
    The threshold should be chosen on a validation set or through cross-validation, and the test set should be used only for the final evaluation.
  4. 4
    Probabilities and reported confidence do not guarantee reliability, so check results on held-out data.

Key Takeaways

  • A predicted class comes from comparing a decision_function score or a predict_proba output against a threshold, with defaults of 0 and 0.5.
  • Lowering the positive-class threshold tends to raise recall and lower precision, and raising it tends to do the opposite.
  • In the source example, lowering the class 1 cutoff from 0 to -0.8 raised recall from 0.67 to 1.00 and lowered precision from 0.35 to 0.32.
  • Choose the threshold on a validation set or through cross-validation, never on the test set, and evaluate once on the test set.

Related concepts

More concepts starting with C