Implementation:Scikit learn Scikit learn PrecisionRecallDisplay
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Visualization |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for visualizing precision-recall curves of binary classifiers provided by scikit-learn.
Description
The PrecisionRecallDisplay class provides visualization of the precision-recall tradeoff for binary classification models. It plots precision against recall at various decision thresholds, optionally displaying the average precision score and a chance-level line based on the positive class prevalence. The class extends _BinaryClassifierCurveDisplayMixin and supports creation from an estimator, from predictions, or from pre-computed precision and recall arrays.
Usage
Use this display class when evaluating binary classifiers on imbalanced datasets where the ROC curve may give an overly optimistic picture, when you need to visualize the precision-recall tradeoff, or when comparing classifiers with respect to their ability to identify positive instances.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/metrics/_plot/precision_recall_curve.py
Signature
class PrecisionRecallDisplay(_BinaryClassifierCurveDisplayMixin):
def __init__(self, precision, recall, *, average_precision=None, name=None,
pos_label=None, prevalence_pos_label=None, estimator_name=None)
def plot(self, ax=None, *, name=None, plot_chance_level=False,
chance_level_kw=None, despine=False, **kwargs)
@classmethod
def from_estimator(cls, estimator, X, y, *, sample_weight=None, pos_label=None,
response_method="auto", name=None, ax=None,
plot_chance_level=False, chance_level_kw=None, despine=False,
**kwargs)
@classmethod
def from_predictions(cls, y_true, y_pred, *, sample_weight=None, pos_label=None,
name=None, ax=None, plot_chance_level=False,
chance_level_kw=None, despine=False, **kwargs)
Import
from sklearn.metrics import PrecisionRecallDisplay
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| precision | ndarray | Yes | Precision values for the curve |
| recall | ndarray | Yes | Recall values for the curve |
| average_precision | float | No | Average precision score shown in the legend |
| name | str | No | Name for labeling the curve in the legend |
| pos_label | int, float, bool or str | No | The label of the positive class |
| prevalence_pos_label | float | No | Prevalence of positive class for the chance level line |
| plot_chance_level | bool | No | Whether to plot the chance level line (default False) |
| despine | bool | No | Whether to remove top and right spines from the plot (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| display | PrecisionRecallDisplay | Display object with line_, chance_level_, ax_, and figure_ attributes |
Usage Examples
Basic Usage
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import PrecisionRecallDisplay
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = LogisticRegression(random_state=0).fit(X_train, y_train)
# Create precision-recall curve from estimator
PrecisionRecallDisplay.from_estimator(clf, X_test, y_test, plot_chance_level=True)
plt.show()
# Or from predictions
y_score = clf.predict_proba(X_test)[:, 1]
PrecisionRecallDisplay.from_predictions(y_test, y_score, name="LogisticRegression")
plt.show()