Training and Testing Classifiers in scikit-learn
Classifiers can provide uncertainty estimates in addition to the bare predicted label. These are numeric signals that go beyond the label and describe how strongly the model favors each option.
Two Test Points With the Same Label
Most first work with a classifier asks one question: for a given test point, which class does the model predict? That answer is useful, but it hides something important. Often you also want to know how sure the classifier is that the predicted class is the right one.
Imagine two test points that both come back labeled 'red'. For the first, the classifier is almost completely certain. For the second, it is barely leaning toward red. Both return the same label, yet they should not be treated the same way. The label alone cannot tell you which situation you are in.
Following the Chain From Data to Fitted Model
The source builds its discussion around a small synthetic two-dimensional dataset and a gradient boosting classifier. Read the steps in order before worrying about uncertainty. The data is generated, the class labels are renamed to words so the output is easier to read, the data is split into training and test portions, and the model is fitted on the training part. Only after that does the model exist as an object you can ask questions of.
- 1Generate a synthetic two-dimensional dataset with features X and class labels y
- 2Rename the numeric class labels to words such as red and blue so the output is readable
- 3Split the data into a training portion and a test portion
- 4Create a GradientBoostingClassifier and call fit on the training portion only, producing the trained object gbrt
- 5Query the trained gbrt with test points using one of its uncertainty methods
The source's code stops after fitting. The point is that gbrt is now a trained classifier, and every method you call in the next steps belongs to that one object. Keep the name gbrt in mind. If an output later looks wrong, the place to look is the step where the wrong object or the wrong input entered this chain, not only the final print statement.
Why a Predicted Class Is Not Enough
A predicted class is a single decision. It collapses everything the model learned about one input into one word. Two inputs can land on the same side of the boundary while sitting in very different positions relative to it. A label that says red with near-total confidence and a label that says red while barely leaning that way carry the same surface information, but a careful application would act on them differently.
Classifiers can provide uncertainty estimates in addition to the bare predicted label. These are numeric signals that go beyond the label and describe how strongly the model favors each option.
The reason to expose uncertainty is that the cost of a wrong answer is not always the same. A model used for decisions with real consequences should make its confidence available, because a wrong answer can be cheap in one setting and expensive in another. The source keeps this discussion general and returns to it in more detail later in the course, so this article only introduces the idea.
Two Methods That Expose Uncertainty
scikit-learn offers two methods for obtaining uncertainty estimates from classifiers: decision_function and predict_proba. Their outputs are not the same kind of thing, which is why you should predict what each one returns before you read the result.
GradientBoostingClassifier has both methods, which makes it a convenient model for comparing them. Calling both on the same fitted object with the same test points means any difference you see comes from the method rather than from the data.
Running the Example and Checking the Shapes
The following script follows the source's workflow: generate two-dimensional data, rename the labels, split, fit gbrt, and then call both methods on the same test points. The script is a generated illustration of the steps described in the source; it does not reproduce the source's exact code.
make_blobscreates two-dimensional synthetic data with two centersnp.whererenames the numeric labels to wordstrain_test_splitkeeps the test portion separate from fit- fit is called only on the training portion
- Both uncertainty methods are called on the same fitted object gbrt with the same test points
What do you think happens?
Before you run the code, which call returns one value per test point with a sign, and which returns one column per class whose values sum to 1 for each row?
Reveal answer
decision_function returns the signed scores; predict_proba returns per-class probabilities
decision_function gives an unbounded signed value per test point, while predict_proba gives one probability per class for each point, and each row sums to 1.
Run the script and check the shapes first. The printed classes_ list shows the order of the columns in predict_proba. The shape of the decision_function output is one-dimensional for this binary problem, while the predict_proba output has two columns. Checking shapes is the quickest way to confirm which method you are looking at.
Moving the Cutoff on a Probability
A predicted label is produced by applying a rule to the model's output. Once you have a per-class probability, you can apply a different cutoff of your own. Raising the cutoff for the red class means fewer points get called red, and each point that crosses the line changes its predicted class.
- The column for red is looked up by name, not assumed to be first
- predict applies the model's own default decision rule
- The 0.8 cutoff is a choice you make, and it can be changed
Read the diagram as a list of test points in order, each with its probability of red. Moving the cutoff does not change any probability. It only changes which points cross the line and therefore which predicted class they receive. That is the mechanism by which the trade-off between kinds of mistakes can be adjusted.
Mistakes That Carry Different Costs
A mistake can go in two directions. A false positive means the model predicts the positive class for an input that does not belong to it. A false negative means the model predicts the other class for an input that does belong to the positive class. The two are not interchangeable: which one is more costly depends on the application, and the same mistake can be cheap in one setting and expensive in another.
| Setting | Costly mistake | Why it matters |
|---|---|---|
| Routine recommendation | False positive | A wrong suggestion is cheap to ignore |
| Serious screening decision | False negative | A missed case can carry a high cost |
| Automated action with no review | Either direction | Confidence determines whether a human should check the case |
Illustrative settings. The right balance depends on the real consequences in each application.
Common mistake Treating the predicted label as if it carried confidence
What it looks likeTwo points both labeled red are handled identically even though one is a near tie
Why it failsThe label collapses the model's confidence into one word
Do this insteadLook at the uncertainty output for the points where the decision matters most
Common mistake Assuming every classifier has decision_function and predict_proba
What it looks likeCalling predict_proba on a model that only exposes decision_function
Why it failsMost classifiers have at least one method, but not all have both
Do this insteadCheck which method the specific classifier provides before writing code that depends on it
Common mistake Reading decision_function values as probabilities
What it looks likeSaying a score of 2.0 means a 200 percent chance
Why it failsdecision_functionvalues are unbounded signed scores, not probabilitiesDo this insteadRead the sign for the side and the magnitude only for relative distance from the boundary
Common mistake Fitting on the test data
What it looks likePassing the full dataset to fit and then evaluating on the same points
Why it failsThe test points are then no longer independent of training
Do this insteadKeep the test portion separate and pass only the training portion to fit
Practice Reading the Two Outputs
Using the gbrt example, predict the output shape of decision_function and of predict_proba for the first five test points before you run the code. Then run the code and check the shapes. Explain in one sentence what the sign of a decision_function value tells you, and why a predict_proba row always sums to one.
Hints
- Think about whether a score needs one value or one value per class
- The classes_ attribute tells you the column order for
predict_proba - A probability row covers every class, so its values must add up to one
Key Takeaways
- A predicted class is one decision, and it can hide how confident the classifier is about that decision
- scikit-learn exposes uncertainty through
decision_function, which returns unbounded signed scores, andpredict_proba, which returns per-class probabilities that sum to one for each point - Not every classifier has both methods, so check which ones a given model provides and confirm shapes before interpreting outputs
- Mistakes can have very different consequences, so the cutoff on a score or probability should reflect the real cost of false positives and false negatives, and test data must stay separate from fit