Displaying estimators and complex pipelines#

This example illustrates different ways estimators and pipelines can be displayed.

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

from sklearn.compose import make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

Compact text representation#

Estimators will only show the parameters that have been set to non-default values when displayed as a string. This reduces the visual noise and makes it easier to spot what the differences are when comparing instances.

lr = LogisticRegression(penalty="l1")
print(lr)
LogisticRegression(penalty='l1')

Rich HTML representation#

In notebooks estimators and pipelines will use a rich HTML representation. This is particularly useful to summarise the structure of pipelines and other composite estimators, with interactivity to provide detail. Click on the example image below to expand Pipeline elements. See Visualizing Composite Estimators for how you can use this feature.

num_proc = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())

cat_proc = make_pipeline(
    SimpleImputer(strategy="constant", fill_value="missing"),
    OneHotEncoder(handle_unknown="ignore"),
)

preprocessor = make_column_transformer(
    (num_proc, ("feat1", "feat3")), (cat_proc, ("feat0", "feat2"))
)

clf = make_pipeline(preprocessor, LogisticRegression())
clf
Pipeline(steps=[('columntransformer',
                 ColumnTransformer(transformers=[('pipeline-1',
                                                  Pipeline(steps=[('simpleimputer',
                                                                   SimpleImputer(strategy='median')),
                                                                  ('standardscaler',
                                                                   StandardScaler())]),
                                                  ('feat1', 'feat3')),
                                                 ('pipeline-2',
                                                  Pipeline(steps=[('simpleimputer',
                                                                   SimpleImputer(fill_value='missing',
                                                                                 strategy='constant')),
                                                                  ('onehotencoder',
                                                                   OneHotEncoder(handle_unknown='ignore'))]),
                                                  ('feat0', 'feat2'))])),
                ('logisticregression', LogisticRegression())])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Total running time of the script: (0 minutes 0.027 seconds)

Related examples

Displaying Pipelines

Displaying Pipelines

Combine predictors using stacking

Combine predictors using stacking

Release Highlights for scikit-learn 0.23

Release Highlights for scikit-learn 0.23

Column Transformer with Mixed Types

Column Transformer with Mixed Types

Gallery generated by Sphinx-Gallery