Decision Thresholds and decision_function
A precision-recall curve plots the precision and recall produced by every threshold a classifier could use, so it shows the whole trade-off instead of one operating point.
Guessing What a Random Scorer Achieves
Imagine a classifier whose scores are assigned completely at random, with no information about the labels at all. Now imagine a dataset in which 30% of the samples are positive. Before reading on, make a guess: what would the summary number for this meaningless classifier look like?
What do you think happens?
What value would a summary score of a random scorer reach on a dataset where 30% of samples are positive?
Reveal answer
About 0.3, which is the positive fraction of the dataset.
Average precision is expected to reach the positive fraction for a random scorer. With 30% positives, that means a value near 0.3. Any real model should be judged against that baseline, not against zero.
That baseline is a useful starting point. A single summary number only means something when you know what a useless model would score. This article builds the tool that shows the full picture: the precision-recall curve, which describes every threshold a classifier could use rather than one fixed decision.
From Scores to Decisions
A classifier that outputs a score for each sample is turned into a yes-or-no decision by picking a threshold. Samples whose score is at or above the threshold are called positive, and the rest are called negative. Move the threshold and you change how many samples get called positive. That change in turn changes precision and recall, so the two measures trade against each other as the threshold moves. The same classifier can look very different depending on where the threshold sits.
In scikit-learn, the decision_function method produces these continuous scores for models such as an SVM. The default threshold of decision_function is zero. The predict method uses that default, so it commits to one particular trade-off without showing you the alternatives.
The key point is that a single threshold gives you a single pair of precision and recall. The curve is what you get when you repeat this pipeline for every threshold the classifier could use, rather than stopping at the default.
What the Curve Plots
A precision-recall curve plots the precision and recall produced by every threshold a classifier could use, so it shows the whole trade-off instead of one operating point.
The function that computes the curve lives in the sklearn.metrics module. It takes two inputs: the ground-truth labels and a continuous certainty score for the positive class. That score can come from decision_function or from predict_proba. Passing scores rather than final predicted labels is what lets the function explore many thresholds.
The call returns three sequences that line up by index. The thresholds are every distinct value that appears in the decision function, in sorted order. The precision and recall entries at position i are the values you would get by using the threshold at position i. Plotting precision on one axis and recall on the other turns those pairs into a curve.
Each point on the curve belongs to exactly one threshold. A marked point on the curve corresponds to threshold zero, the default of decision_function. Every other point is a trade-off you could have chosen instead. Seeing the default among all the alternatives is the main reason to draw the whole curve.
Reading Precision and Recall Off the Curve
Once the curve is drawn, you can read trade-offs directly from it without rerunning the model at a new threshold. The source example uses an SVC trained with gamma set to 0.05. On that curve, a recall of 0.4 can be reached at a precision of about 0.75. You read the pair straight off the plot.
Finding the precision available at a given recall
Using the SVC curve described above, what precision can be achieved if you need a recall of 0.4?
- 1Locate the recall
Find 0.4 on the recall axis of the curve.
- 2Follow to the curve
Move vertically from that recall position until you touch the curve. That touched point belongs to one specific threshold.
- 3Read the precision
Read the precision at that point on the vertical axis.
A precision of about 0.75 is available at a recall of 0.4.
The shape of the curve also shows where precision is cheap or expensive. In the same example, precision can reach about 0.5 while recall stays very high, so the left part of the curve is relatively flat. Past a precision of about 0.5, each further gain in precision costs a large amount of recall. Those steep stretches are where asking for more precision is a costly choice.
The curve also describes the overall pattern. Good curves stay near the upper-right corner, where precision and recall are both high at the same threshold. The source describes the curve as beginning at the top-left corner, where the threshold is so low that every sample is called positive. As the threshold rises, the curve moves toward higher precision and lower recall. At very high thresholds, the few samples called positive are almost all correct, so precision is high while recall has dropped. A better model keeps recall high for as long as possible while precision climbs.
Operating Points and Business Requirements
A specific threshold or requirement placed on a classifier is called its operating point. A requirement such as 90% recall is one example. In many projects the right threshold comes from a business requirement, such as missing no more than 10% of the positive cases. Choosing an operating point is useful in business settings because it lets a team make a performance promise to customers or to other groups inside the organization.
Once a target like that is fixed, a threshold can usually be found to meet it. The harder question is whether the model can still keep precision reasonable at that threshold. A model that labels every sample as positive reaches 100% recall, but it is useless. Judging a model well therefore means looking at how precision and recall move together, not at either one alone.
An operating point is still just one point on the curve. The curve is what tells you whether that point is realistic. If the curve drops sharply at 90% recall, the operating point is expensive to hold, and the team may need to revisit the requirement or improve the model.
Comparing Two Classifiers Across Thresholds
Different classifiers can work well in different parts of the curve, which is why comparing their curves is more informative than comparing a single number. The source example compares an SVM with a random forest trained on the same dataset. The random forest has no decision_function, only predict_proba, so the probability of class 1 is passed to the precision-recall function as the certainty score.
| Region of the curve | Stronger classifier | Why it matters |
|---|---|---|
| Extremes: very high recall or very high precision | Random forest | Stronger when a requirement sits at either extreme |
| Around precision of roughly 0.7 | SVM | The better choice in this middle region |
Where each classifier wins, based on the laid-over precision-recall curves
Ranking the two by F1 score alone says the SVM is clearly ahead. That ranking is misleading as a complete picture. The F1 score captures only one point on the curve, the one set by the default threshold. Whatever the curves do at other thresholds is invisible to it, and so is the difference at the extremes where the random forest wins.
Summarizing the Curve with Average Precision
Comparing curves by eye gives detail, but it is a manual process. For automatic model comparison, you want a single number that summarizes the curve without committing to one threshold or operating point. One such summary is the area under the precision-recall curve, which is called average precision. The average_precision_score function computes it.
Notice the argument. Average precision needs every threshold to be considered, so it takes the continuous scores from decision_function or predict_proba. Passing the output of predict would collapse those scores into hard labels and remove the threshold information the summary depends on.
Average precision summarizes the curve as its area and ranges from 0 to 1. Since it is the area under a curve that runs from 0 to 1, its value always falls between 0, the worst case, and 1, the best case. A random scorer is expected to reach the positive fraction, which is the baseline you saw in the opening question.
In the SVM and random forest example, averaged over all thresholds, the two classifiers come out nearly tied, with the random forest slightly ahead. This reverses the F1 ranking from the previous section. That is exactly why the summary and the single-threshold score should be read together.
Mistakes That Distort the Curve
Common mistake Passing predicted labels instead of continuous scores
What it looks likeCalling the curve or average precision function with the output of predict.
Why it failsHard labels collapse the scores into yes or no, so the function cannot explore many thresholds.
Do this insteadPass the continuous scores from decision_function, or the probability of class 1 from predict_proba.
Common mistake Judging a model by one threshold alone
What it looks likeRanking two models only by F1 score at the default threshold.
Why it failsF1 captures one point on the curve and ignores what happens at every other threshold.
Do this insteadRead the full precision-recall curves and use average precision as a summary across all thresholds.
Common mistake Treating high recall as proof of a good model
What it looks likeA model that labels every sample as positive reaches 100% recall.
Why it failsRecall alone ignores precision, and such a model is useless.
Do this insteadLook at how precision and recall move together along the curve.
Common mistake Assuming the operating point is free to hold
What it looks likePromising 90% recall without checking the curve at that point.
Why it failsIf the curve drops sharply there, the promised recall may cost too much precision.
Do this insteadCheck the curve at the operating point, and revisit the requirement or improve the model if it is expensive.
Practice Reading a Curve
Using the SVC curve from this article, suppose a project must reach a recall of 0.4. Read the precision available at that recall. Then explain why a team that asks for precision above about 0.5 should expect to give up a large amount of recall.
Hints
- Start at recall 0.4 on the horizontal axis and move to the curve.
- Recall the source statement that past a precision of about 0.5, each gain in precision costs a large amount of recall.
- A steep stretch of the curve means a small gain in precision is expensive.
Key Takeaways from the Curve
- 1A precision-recall curve plots the precision and recall produced by every threshold a classifier could use, so each point corresponds to one threshold.
- 2The curve shows the trade-off between precision and recall; reading it lets you see how much recall is lost when precision is pushed higher.
- 3Compare classifiers by their full curves, because a single-threshold score such as F1 can reverse the conclusion.
- 4Average precision is the area under the curve, takes continuous scores as input, and ranges from 0 to 1, with a random scorer expected to reach the positive fraction.
At a glance
- A precision-recall curve plots the precision and recall produced by every threshold a classifier could use, so each point corresponds to one threshold.
- Reading the curve reveals the precision available at a given recall, and the recall lost when precision is pushed higher, especially in steep stretches.
- Comparing two classifiers across their full curves can reverse a conclusion drawn from a single score such as F1.
- Average precision summarizes the curve as its area, takes continuous scores as input, and ranges from 0 to 1.