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 FeatureImportances Visualizer

From Leeroopedia


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

Overview

Concrete tool for visualizing feature importance rankings as horizontal bar charts, provided by the Yellowbrick library.

Description

The FeatureImportances visualizer displays a horizontal bar chart of features ranked by their importance as determined by a fitted scikit-learn estimator. It works with any model that exposes either a feature_importances_ attribute (tree-based models) or a coef_ attribute (linear models) after fitting. The visualizer searches for these attributes in order, using whichever is found first.

The class extends ModelVisualizer from the Yellowbrick base module. When fit(X, y) is called, the visualizer first fits the underlying estimator (unless it is already fitted, controlled by the is_fitted parameter), then extracts the feature importances. For multi-class classifiers with multi-dimensional coef_ arrays of shape (n_classes, n_features), the visualizer either computes the mean across classes (when stack=False) or produces a stacked bar chart showing per-class contributions (when stack=True). Importances can be displayed as absolute values via the absolute parameter, and normalized relative to the strongest feature via the relative parameter. The topn parameter allows displaying only the top N (positive integer) or bottom N (negative integer) features.

Feature labels are automatically extracted from DataFrame column names when a DataFrame is passed, or can be explicitly provided via the labels parameter.

Usage

Use this visualizer for feature engineering and model interpretation. It is appropriate when you need to identify which features are most influential, perform feature selection, or communicate model behavior to non-technical stakeholders.

Code Reference

Source Location

  • Repository: yellowbrick
  • File: yellowbrick/model_selection/importances.py
  • Class Lines: L38-406 (class), L124-138 (__init__), L154-267 (fit)
  • Quick Method Lines: L413-531

Signature

class FeatureImportances(ModelVisualizer):
    def __init__(
        self,
        estimator,
        ax=None,
        labels=None,
        relative=True,
        absolute=False,
        xlabel=None,
        stack=False,
        colors=None,
        colormap=None,
        is_fitted="auto",
        topn=None,
        **kwargs
    ):

Import

from yellowbrick.model_selection import FeatureImportances

I/O Contract

Inputs

Name Type Required Description
estimator scikit-learn estimator Yes A model that exposes coef_ or feature_importances_ after fitting.
ax matplotlib.Axes No The axes object to plot on. Default: None (current axes).
labels list No Feature names. If None, uses DataFrame column names or integer indices.
relative bool No Show features as percentage of strongest. Default: True.
absolute bool No Take absolute value of coefficients. Default: False.
xlabel str No Custom label for x-axis. Default: None (auto-determined).
stack bool No If True and multi-class, show stacked bar chart. Default: False.
colors list of strings No Colors for each bar when stack=False.
colormap string or matplotlib cmap No Colormap for classes when stack=True.
is_fitted bool or str No Whether the estimator is already fitted. Default: "auto".
topn int No Show only top N (positive) or bottom N (negative) features. Default: None (all).

The fit(X, y) method accepts:

Name Type Required Description
X ndarray or DataFrame, shape (n, m) Yes Feature matrix of n instances with m features.
y ndarray or Series, length n No Target or class values.

Outputs

Name Type Description
features_ np.array Feature labels ranked according to their importance.
feature_importances_ np.array Numeric importance values computed by the model (optionally relative and/or absolute).
classes_ np.array Class labels for classifiers. None for regressors.

Usage Examples

Basic Usage

from sklearn.ensemble import GradientBoostingClassifier
from yellowbrick.model_selection import FeatureImportances

# Create and fit the visualizer
viz = FeatureImportances(GradientBoostingClassifier(), relative=True, topn=10)
viz.fit(X_train, y_train)
viz.show()

Quick Method

from yellowbrick.model_selection import feature_importances
from sklearn.ensemble import RandomForestClassifier

feature_importances(RandomForestClassifier(), X_train, y_train, topn=5)

Related Pages

Implements Principle

Requires Environment

Page Connections

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