Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:DistrictDataLabs Yellowbrick PredictionError Visualizer

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Regression, Visualization
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for visualizing prediction errors by plotting actual versus predicted values, provided by the Yellowbrick library.

Description

The PredictionError visualizer plots the actual target values on the horizontal axis against the predicted values on the vertical axis for a fitted regression model. This produces a scatter plot where a perfect model would place all points along the 45-degree identity line y^=y.

The visualizer supports two reference lines. The identity line (y=x) provides a visual benchmark for perfect predictions. The best-fit line is a linear regression through the actual-vs-predicted scatter, showing the empirical relationship between the two. When the best-fit line deviates from the identity line, it indicates systematic bias in the model's predictions.

The shared_limits parameter forces both axes to share the same range, creating a square plot where the identity line is a true 45-degree diagonal. This makes it easier to visually judge whether the model is over- or under-predicting. Scatter point transparency is controlled via the alpha parameter to handle overlapping points in dense datasets.

The visualizer extends RegressionScoreVisualizer and displays the R2 score in the legend. Its primary entry point is the score() method, which generates predictions and draws the plot.

Usage

Use PredictionError when you need to:

  • Evaluate whether a regression model produces well-calibrated predictions
  • Visually diagnose systematic over- or under-prediction bias
  • Detect heteroscedasticity across different ranges of the target variable
  • Present model performance in an intuitive actual-vs-predicted format

Code Reference

Source Location

  • Repository: yellowbrick
  • File: yellowbrick/regressor/prediction_error.py
  • Class: Lines 36-285
  • Quick Method: Lines 293-417

Signature

class PredictionError(RegressionScoreVisualizer):
    def __init__(
        self,
        estimator,
        ax=None,
        shared_limits=True,
        bestfit=True,
        identity=True,
        alpha=0.75,
        is_fitted="auto",
        **kwargs
    )

Import

from yellowbrick.regressor import PredictionError

I/O Contract

Inputs

Name Type Required Description
estimator Scikit-Learn regressor Yes A regression estimator instance to wrap. Must be a regressor or a YellowbrickTypeError is raised.
ax matplotlib Axes No The axes to plot on. If None, the current axes are used or created.
shared_limits bool No If True, the X and Y axes share the same range, creating a square plot with a true 45-degree identity line. Default: True.
bestfit bool No If True, draws a linear best-fit line through the scatter. Default: True.
identity bool No If True, draws the 45-degree identity line y^=y. Default: True.
alpha float No Transparency for scatter points (0=transparent, 1=opaque). Default: 0.75.
is_fitted bool or str No Whether the estimator is already fitted. 'auto' checks automatically. Default: 'auto'.
point_color color (via kwargs) No Color for scatter points. Passed as a keyword argument. Default: None (uses matplotlib default).
line_color color (via kwargs) No Color for the best-fit and identity lines. Passed as a keyword argument. Default: dark grey.

Outputs

Name Type Description
score_ float The R2 score of the regression model on the scored data.
ax matplotlib Axes The axes containing the prediction error scatter plot with best-fit and identity lines.

Usage Examples

Basic Usage

from sklearn.linear_model import Lasso
from sklearn.model_selection import train_test_split
from yellowbrick.regressor import PredictionError
from yellowbrick.datasets import load_concrete

# Load dataset
X, y = load_concrete()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and fit the visualizer
viz = PredictionError(Lasso())
viz.fit(X_train, y_train)
viz.score(X_test, y_test)
viz.show()

Quick Method

from sklearn.linear_model import Lasso
from sklearn.model_selection import train_test_split
from yellowbrick.regressor import prediction_error
from yellowbrick.datasets import load_concrete

X, y = load_concrete()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

viz = prediction_error(Lasso(), X_train, y_train, X_test, y_test)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment