Implementation:Scikit learn Scikit learn PredictionErrorDisplay
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Visualization |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for visualizing prediction errors of regression models provided by scikit-learn.
Description
The PredictionErrorDisplay class provides visualization of regression model prediction errors through scatter plots. It supports two display modes: "actual vs predicted" plots where data points are compared against a diagonal line representing perfect predictions, and "residuals vs predicted" plots where prediction residuals are plotted against predicted values with a horizontal reference line at zero. Optional error lines can be drawn from each point to the reference line.
Usage
Use this display class when qualitatively assessing the behavior of a regression model, diagnosing systematic prediction errors, checking for heteroscedasticity, or evaluating model performance on held-out data.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/metrics/_plot/regression.py
Signature
class PredictionErrorDisplay:
def __init__(self, *, y_true, y_pred)
def plot(self, ax=None, *, kind="residual_vs_predicted", scatter_kwargs=None,
line_kwargs=None, with_errors=False)
@classmethod
def from_estimator(cls, estimator, X, y, *, kind="residual_vs_predicted",
scatter_kwargs=None, line_kwargs=None, ax=None,
with_errors=False, random_state=None, subsample=1_000)
@classmethod
def from_predictions(cls, y_true, y_pred, *, kind="residual_vs_predicted",
scatter_kwargs=None, line_kwargs=None, ax=None,
with_errors=False, random_state=None, subsample=1_000)
Import
from sklearn.metrics import PredictionErrorDisplay
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| y_true | ndarray of shape (n_samples,) | Yes | True target values |
| y_pred | ndarray of shape (n_samples,) | Yes | Predicted target values |
| estimator | estimator instance | Yes (from_estimator) | Fitted regressor |
| X | array-like of shape (n_samples, n_features) | Yes (from_estimator) | Input data for predictions |
| kind | str | No | Type of plot: residual_vs_predicted or actual_vs_predicted (default residual_vs_predicted) |
| with_errors | bool | No | Whether to draw error lines from points to reference line (default False) |
| subsample | int | No | Number of samples to display for large datasets (default 1000) |
| random_state | int, RandomState or None | No | Random state for subsampling |
Outputs
| Name | Type | Description |
|---|---|---|
| display | PredictionErrorDisplay | Display object with line_, errors_lines_, scatter_, ax_, and figure_ attributes |
Usage Examples
Basic Usage
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.metrics import PredictionErrorDisplay
X, y = load_diabetes(return_X_y=True)
ridge = Ridge().fit(X, y)
# Actual vs Predicted plot
PredictionErrorDisplay.from_estimator(ridge, X, y, kind="actual_vs_predicted")
plt.show()
# Residuals vs Predicted plot
PredictionErrorDisplay.from_estimator(ridge, X, y, kind="residual_vs_predicted")
plt.show()
# From pre-computed predictions
y_pred = ridge.predict(X)
display = PredictionErrorDisplay(y_true=y, y_pred=y_pred)
display.plot(kind="actual_vs_predicted")
plt.show()