Concepts / Classifier Uncertainty Estimates

Classifier Uncertainty Estimates

In the binary classification case, decision_function returns an array whose shape is $(n_{\text{samples}},)$.

  • Programming

A Second Question to Ask a Fitted Model

A fitted classifier offers more than a predict method. It also offers decision_function, which returns a number for every sample you pass in. Predict answers the question of which class a sample belongs to. Decision_function answers a more detailed question: how strongly, and in which direction, the model leans for each sample.

What do you think happens?

You pass a test set of 25 samples, each with 2 features, to decision_function of a binary classifier. What shape should the result have?

  • $(25, 2)$

  • $(25,)$

  • $(2,)$

  • $(25, 1)$

Reveal answer
Answer:

$(25,)$

A binary classifier collapses each sample's features into a single score, so the feature dimension disappears and you get one number per sample.

One Number per Sample

In the binary classification case, decision_function returns an array whose shape is $(n_{\text{samples}},)$. Each entry is a single floating-point number describing one sample. The source example makes this concrete: the test input has shape $(25, 2)$, meaning 25 samples with 2 features each, while the decision function output has shape $(25,)$. The feature dimension has disappeared because the classifier has collapsed each sample's features into one score.

sample 04.136sample 1-1.683sample 2-3.951sample 3-3.626sample 44.29sample 53.662
How do many samples in a matrix become a single column of scores, one value per row, with shape (n_samples,)?

The diagram shows the first six scores from the source run, one per sample, stacked as a single column. Each sample contributes exactly one number, regardless of how many features it started with. The matrix of inputs with many columns becomes a column vector of scores, and that is why the output shape has only one dimension.

Reading the Sign and the Magnitude

Each score measures how strongly the model believes a point belongs to the positive class, which in this example is class 1. A large positive score means the model is confident the point belongs to the positive class. A negative score means the model leans toward the negative class, which is the other class in the problem. The sign gives the direction of the lean, and the magnitude gives its strength.

one scorefor one samplescore above 0?compare with 0True, index 1leans positiveclasses_[1]positive class labelFalse, index 0leans negativeclasses_[0]negative class labelsign checkyesnoindex 1index 0
What happens to the predicted class as a sample's score moves from negative to positive?

The sign alone reproduces the predicted labels. If you compare each score with 0, you get a boolean array that is True for positive scores and False for negative scores. In the source example, that boolean array is True for the samples that predict returns as red and False for the samples that predict returns as blue. The thresholded decision function and the predictions therefore agree on which samples belong to the positive class. The source's rule is a comparison with 0; it does not describe a separate rule for a score of exactly 0, so this article does not assume one.

Worked example

Ranking six scores from the source run

The first six scores on the test set are 4.136, -1.683, -3.951, -3.626, 4.29, and 3.662. Which samples lean toward the positive class, and how do their strengths compare?

  1. 1
    Read the sign of each score

    4.136, 4.29, and 3.662 are positive, so these samples lean toward the positive class. -1.683, -3.951, and -3.626 are negative, so they lean toward the negative class.

  2. 2
    Compare magnitudes within the positive group

    4.29 is larger than 4.136, which is larger than 3.662, so the sample scored 4.29 leans toward the positive class more strongly.

  3. 3
    Compare magnitudes within the negative group

    -3.951 is more negative than -1.683, so the sample scored -3.951 leans toward the negative class more strongly than the sample scored -1.683.

Result

Scores are best read as a ranking of leanings: sign gives the direction, and the relative size gives the strength within this one model.

Recovering Labels with classes_

The boolean array tells you the decision for each sample, but it does not carry the original label names. It only says True or False. To get the labels that predict returns, you need one more piece of information: which label corresponds to False and which corresponds to True. The classes_ attribute provides that mapping.

Falsescore below 0classes_[0]negative class, index 0Truescore above 0classes_[1]positive class, index 1as index 0as index 1
How does a positive or negative score point to classes_[0] or classes_[1], and which label does predict return for each?

For binary classification, the negative class is always the first entry of classes_, and the positive class is always the second entry. The procedure in the source converts the True and False values into 1 and 0, then uses those integers as indices into classes_. Indexing with 0 selects the negative class, and indexing with 1 selects the positive class. When the rebuilt array is compared with the output of predict across all samples, the check reports True only if every element matches. In the source run the result is True, which confirms that the sign of the decision function, combined with classes_, reproduces predict exactly.

Worked example

Turning signs into predicted labels

Using the source's six scores, which label does predict return for the sample scored 4.136 and for the sample scored -1.683?

  1. 1
    Threshold the first score

    4.136 is above 0, so the boolean value is True.

  2. 2
    Convert True to an index

    True becomes 1, which selects the second entry of classes_.

  3. 3
    Look up the label

    The second entry is the positive class. In the source run, samples with True are the ones predict returns as red, so the label is red.

  4. 4
    Repeat for the second score

    -1.683 is below 0, so the boolean value is False, which becomes 0 and selects the first entry of classes_, the negative class. In the source run this is blue.

Result

The sample scored 4.136 is predicted red, and the sample scored -1.683 is predicted blue, matching the source's predict output.

Why Raw Scores Are Not a Confidence Scale

The numeric range of decision_function is not fixed. It can be arbitrary, and it depends on both the data and the model parameters. The source run reports a minimum of -7.69 and a maximum of 4.29 on the test set. A different dataset or different model settings could produce a different spread, so a score of 2 from one model does not necessarily mean the same thing as a score of 2 from another.

minimum score-7.69minimum scoredepends on data andparametersmaximum score4.29maximum scoredepends on data andparameters
Why can the same score mean different confidence across models or datasets, when the numeric range is not fixed?

Common Mistakes with decision_function

  • Common mistake

    Expecting one row of scores per feature

    What it looks like

    Passing 25 samples with 2 features and expecting an output of shape (25, 2)

    Why it fails

    The binary case collapses each sample's features into a single score, so the output has shape (n_samples,).

    Do this instead

    Expect a one-dimensional array with one floating-point value per sample.

  • Common mistake

    Reading a positive score as a 100 percent certainty

    What it looks like

    Concluding that a score of 4.29 means the model is certain, while a score of 4.136 is only moderately sure

    Why it fails

    The raw range is arbitrary and depends on data and parameters, so raw values are not a probability scale.

    Do this instead

    Use the sign for the decision and compare scores only as a relative ordering within one model.

  • Common mistake

    Reading labels straight from the sign

    What it looks like

    Seeing True and assuming it means a specific name such as red

    Why it fails

    The boolean array carries no label names; the mapping comes from classes_.

    Do this instead

    Convert True and False to 1 and 0, then index classes_ to get the label.

  • Common mistake

    Assuming the order of classes_ is arbitrary

    What it looks like

    Treating the first label as the positive class because it appears first alphabetically

    Why it fails

    For binary classification, the first entry of classes_ is always the negative class and the second is always the positive class.

    Do this instead

    Index classes_ with 0 for negative and 1 for positive, and check the order before interpreting a sign.

Checking Your Reading of the Scores

EASY

A binary classifier returns the score -2.5 for one sample and 1.8 for another. Without running any code, state which sample leans toward the positive class, and explain which entry of classes_ each sample maps to.

Hints
  • Look at the sign of each score before anything else.
  • The negative class is always at index 0 of classes_, and the positive class is always at index 1.

Key Takeaways for Reading Decision Scores

decision_function gives one floating-point score per sample in the binary case, so the output shape is $(n_{\text{samples}},)$. The sign of each score gives the direction of the model's lean, and its magnitude gives the strength. The classes_ attribute, with index 0 for the negative class and index 1 for the positive class, turns those signs back into the labels that predict returns. Because the numeric range is arbitrary and depends on data and parameters, read the sign for the decision and the relative ordering within one model, not the raw value, as a confidence level.

At a glance

  • decision_function returns one floating-point score per sample in the binary case, so its shape is (n_samples,).
  • A positive score leans toward the positive class and a negative score leans toward the negative class; the magnitude shows how strong the lean is.
  • The sign of the score reproduces predict, and classes_ maps True and False to labels, with index 0 as the negative class and index 1 as the positive class.
  • The numeric range of decision_function is arbitrary and depends on data and model parameters, so raw values are not a probability scale.

Related concepts

More concepts starting with C