Implementation:DistrictDataLabs Yellowbrick ModelVisualizer
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Visualization, Data_Science |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for the Visualizer API Pattern (fit-score-show lifecycle) provided by the Yellowbrick library.
Description
The ModelVisualizer and ScoreVisualizer classes in yellowbrick.base implement the core Yellowbrick API pattern that bridges scikit-learn estimators with visual diagnostics. Together they form the middle and leaf tiers of the Yellowbrick class hierarchy:
ModelVisualizer (L286-391) extends Visualizer and mixes in Wrapper. It wraps a scikit-learn estimator and provides transparent attribute delegation, so that any attribute or method not found on the visualizer is forwarded to the wrapped estimator. Its __init__ accepts an estimator, stores it, extracts the model name via get_model_name(), and initializes both parent classes. Its fit() method uses check_fitted() to determine whether the wrapped estimator needs training: if the is_fitted parameter is "auto" (the default), it inspects the estimator for fitted attributes; if the estimator is not yet fitted, fit() trains it on the provided data before returning self.
ScoreVisualizer (L399-449) extends ModelVisualizer and adds an abstract score(X, y, **kwargs) method. Concrete subclasses implement this method to compute a model performance metric (such as accuracy, R2, or silhouette score), produce the visualization via draw(), and return the numeric score. This class enables the full fit-score-show pattern used by classification reports, residual plots, and other model evaluation visualizers.
The two classes together ensure that the complete Yellowbrick lifecycle -- instantiate -> fit -> score -> show -- works consistently across all model-wrapping visualizers.
Usage
ModelVisualizer is used as a base class for visualizers that need to wrap and interact with a scikit-learn estimator but do not compute a score (e.g., feature importance visualizers, learning curve visualizers). ScoreVisualizer is used as a base class for visualizers that evaluate model performance and present the results visually (e.g., ClassificationReport, ResidualsPlot, SilhouetteVisualizer). End users instantiate concrete subclasses, not these base classes directly.
Code Reference
Source Location
- Repository: yellowbrick
- File: yellowbrick/base.py
- Lines: 286-391 (ModelVisualizer), 399-449 (ScoreVisualizer)
Signature
class ModelVisualizer(Visualizer, Wrapper):
def __init__(self, estimator, ax=None, fig=None, is_fitted="auto", **kwargs):
self.estimator = estimator
self.is_fitted = is_fitted
self.name = get_model_name(self.estimator)
Wrapper.__init__(self, self.estimator)
Visualizer.__init__(self, ax=ax, fig=fig, **kwargs)
def fit(self, X, y=None, **kwargs):
if not check_fitted(self.estimator, is_fitted_by=self.is_fitted):
self.estimator.fit(X, y, **kwargs)
return self
class ScoreVisualizer(ModelVisualizer):
def score(self, X, y, **kwargs):
raise NotImplementedError(
"ScoreVisualizer subclasses should implement score"
)
Import
from yellowbrick.base import ModelVisualizer
from yellowbrick.base import ScoreVisualizer
I/O Contract
Inputs
ModelVisualizer.__init__
| Name | Type | Required | Description |
|---|---|---|---|
| estimator | scikit-learn estimator | Yes | A scikit-learn estimator (regressor, classifier, or clusterer) to wrap. If not fitted, it is fitted when the visualizer is fitted. |
| ax | matplotlib.axes.Axes | No | The axis to plot the figure on. If None, the current axes will be used or generated. |
| fig | matplotlib.figure.Figure | No | The figure to plot on. If None, the current figure will be used or generated. |
| is_fitted | bool or str | No | Whether the wrapped estimator is already fitted. If False, the estimator will be fit during visualizer.fit(). If "auto" (default), a helper checks for fitted attributes. |
| kwargs | dict | No | Additional keyword arguments passed to the Visualizer base class (size, color, title). |
ModelVisualizer.fit
| Name | Type | Required | Description |
|---|---|---|---|
| X | ndarray or DataFrame of shape (n, m) | Yes | A matrix of n instances with m features. |
| y | ndarray or Series of length n | No | An array or series of target or class values. |
| kwargs | dict | No | Keyword arguments passed to the estimator's fit method and/or the drawing functionality. |
ScoreVisualizer.score
| Name | Type | Required | Description |
|---|---|---|---|
| X | ndarray or DataFrame of shape (n, m) | Yes | A matrix of n test instances with m features. |
| y | ndarray or Series of length n | Yes | An array or series of true target or class values for scoring. |
| kwargs | dict | No | Additional keyword arguments passed to the scoring implementation. |
Outputs
ModelVisualizer.fit
| Name | Type | Description |
|---|---|---|
| self | ModelVisualizer | Returns self to support scikit-learn pipeline chaining. |
ScoreVisualizer.score
| Name | Type | Description |
|---|---|---|
| score | float or array-like | The score of the underlying model. Model-specific: accuracy for classifiers, R2 for regressors, etc. |
Usage Examples
Basic Usage: Fit-Score-Show Pattern
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from yellowbrick.classifier import ClassificationReport
from yellowbrick.datasets import load_mushroom
# Load data
X, y = load_mushroom()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Instantiate: create a ScoreVisualizer wrapping a classifier
viz = ClassificationReport(GradientBoostingClassifier())
# Fit: trains the wrapped estimator and learns from data
viz.fit(X_train, y_train)
# Score: evaluates the model on test data and draws the visualization
viz.score(X_test, y_test)
# Show: finalizes and renders the plot
viz.show()
Using a Pre-Fitted Model
from sklearn.linear_model import LinearRegression
from yellowbrick.regressor import ResidualsPlot
from yellowbrick.datasets import load_bikeshare
from sklearn.model_selection import train_test_split
# Load data and train the model separately
X, y = load_bikeshare()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)
# Pass the pre-fitted model; is_fitted="auto" detects it is already trained
viz = ResidualsPlot(model)
viz.fit(X_train, y_train) # Does NOT re-fit the model
viz.score(X_test, y_test)
viz.show()
Accessing Wrapped Estimator Attributes
from sklearn.linear_model import Ridge
from yellowbrick.regressor import ResidualsPlot
from yellowbrick.datasets import load_concrete
from sklearn.model_selection import train_test_split
X, y = load_concrete()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
viz = ResidualsPlot(Ridge(alpha=1.0))
viz.fit(X_train, y_train)
# Access wrapped estimator attributes directly through the visualizer
print(viz.coef_) # Delegated to the wrapped Ridge estimator
print(viz.intercept_) # Delegated to the wrapped Ridge estimator
print(viz.name) # "Ridge" - extracted by get_model_name()
viz.score(X_test, y_test)
viz.show()
Class Hierarchy
The complete inheritance chain for the Visualizer API pattern is:
sklearn.base.BaseEstimator
|
v
Visualizer(BaseEstimator) # L35-279 in base.py
| # Defines: __init__, fit, draw, finalize, show
|
v
ModelVisualizer(Visualizer, Wrapper) # L286-391 in base.py
| # Adds: estimator wrapping, delegated fit
|
v
ScoreVisualizer(ModelVisualizer) # L399-449 in base.py
# Adds: abstract score() method