.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/miscellaneous/plot_pipeline_display.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_miscellaneous_plot_pipeline_display.py: ================================================================= Displaying Pipelines ================================================================= The default configuration for displaying a pipeline is `'text'` where `set_config(display='text')`. To visualize the diagram in Jupyter Notebook, use `set_config(display='diagram')` and then output the pipeline object. To see more detailed steps in the visualization of the pipeline, click on the steps in the pipeline. .. GENERATED FROM PYTHON SOURCE LINES 15-21 Displaying a Pipeline with a Preprocessing Step and Classifier ############################################################################### This section constructs a :class:`~sklearn.pipeline.Pipeline` with a preprocessing step, :class:`~sklearn.preprocessing.StandardScaler`, and classifier, :class:`~sklearn.linear_model.LogisticRegression`, and displays its visual representation. .. GENERATED FROM PYTHON SOURCE LINES 21-33 .. code-block:: default from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn import set_config steps = [ ("preprocessing", StandardScaler()), ("classifier", LogisticRegression()), ] pipe = Pipeline(steps) .. GENERATED FROM PYTHON SOURCE LINES 34-35 To view the text pipeline, the default is `display='text'`. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default set_config(display="text") pipe .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Pipeline(steps=[('preprocessing', StandardScaler()), ('classifier', LogisticRegression())]) .. GENERATED FROM PYTHON SOURCE LINES 39-40 To visualize the diagram, change `display='diagram'`. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: default set_config(display="diagram") pipe # click on the diagram below to see the details of each step .. raw:: html
Pipeline(steps=[('preprocessing', StandardScaler()),
                    ('classifier', LogisticRegression())])
Please rerun this cell to show the HTML repr or trust the notebook.


.. GENERATED FROM PYTHON SOURCE LINES 44-51 Displaying a Pipeline Chaining Multiple Preprocessing Steps & Classifier ############################################################################### This section constructs a :class:`~sklearn.pipeline.Pipeline` with multiple preprocessing steps, :class:`~sklearn.preprocessing.PolynomialFeatures` and :class:`~sklearn.preprocessing.StandardScaler`, and a classifer step, :class:`~sklearn.linear_model.LogisticRegression`, and displays its visual representation. .. GENERATED FROM PYTHON SOURCE LINES 51-64 .. code-block:: default from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler, PolynomialFeatures from sklearn.linear_model import LogisticRegression from sklearn import set_config steps = [ ("standard_scaler", StandardScaler()), ("polynomial", PolynomialFeatures(degree=3)), ("classifier", LogisticRegression(C=2.0)), ] pipe = Pipeline(steps) .. GENERATED FROM PYTHON SOURCE LINES 65-66 To visualize the diagram, change to display='diagram' .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default set_config(display="diagram") pipe # click on the diagram below to see the details of each step .. raw:: html
Pipeline(steps=[('standard_scaler', StandardScaler()),
                    ('polynomial', PolynomialFeatures(degree=3)),
                    ('classifier', LogisticRegression(C=2.0))])
Please rerun this cell to show the HTML repr or trust the notebook.


.. GENERATED FROM PYTHON SOURCE LINES 70-76 Displaying a Pipeline and Dimensionality Reduction and Classifier ############################################################################### This section constructs a :class:`~sklearn.pipeline.Pipeline` with a dimensionality reduction step, :class:`~sklearn.decomposition.PCA`, a classifier, :class:`~sklearn.svm.SVC`, and displays its visual representation. .. GENERATED FROM PYTHON SOURCE LINES 76-85 .. code-block:: default from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.decomposition import PCA from sklearn import set_config steps = [("reduce_dim", PCA(n_components=4)), ("classifier", SVC(kernel="linear"))] pipe = Pipeline(steps) .. GENERATED FROM PYTHON SOURCE LINES 86-87 To visualize the diagram, change to `display='diagram'`. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default set_config(display="diagram") pipe # click on the diagram below to see the details of each step .. raw:: html
Pipeline(steps=[('reduce_dim', PCA(n_components=4)),
                    ('classifier', SVC(kernel='linear'))])
Please rerun this cell to show the HTML repr or trust the notebook.


.. GENERATED FROM PYTHON SOURCE LINES 91-97 Displaying a Complex Pipeline Chaining a Column Transformer ############################################################################### This section constructs a complex :class:`~sklearn.pipeline.Pipeline` with a :class:`~sklearn.compose.ColumnTransformer` and a classifier, :class:`~sklearn.linear_model.LogisticRegression`, and displays its visual representation. .. GENERATED FROM PYTHON SOURCE LINES 97-133 .. code-block:: default import numpy as np from sklearn.pipeline import make_pipeline from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder, StandardScaler from sklearn.linear_model import LogisticRegression from sklearn import set_config numeric_preprocessor = Pipeline( steps=[ ("imputation_mean", SimpleImputer(missing_values=np.nan, strategy="mean")), ("scaler", StandardScaler()), ] ) categorical_preprocessor = Pipeline( steps=[ ( "imputation_constant", SimpleImputer(fill_value="missing", strategy="constant"), ), ("onehot", OneHotEncoder(handle_unknown="ignore")), ] ) preprocessor = ColumnTransformer( [ ("categorical", categorical_preprocessor, ["state", "gender"]), ("numerical", numeric_preprocessor, ["age", "weight"]), ] ) pipe = make_pipeline(preprocessor, LogisticRegression(max_iter=500)) .. GENERATED FROM PYTHON SOURCE LINES 134-135 To visualize the diagram, change to `display='diagram'` .. GENERATED FROM PYTHON SOURCE LINES 135-138 .. code-block:: default set_config(display="diagram") pipe # click on the diagram below to see the details of each step .. raw:: html
Pipeline(steps=[('columntransformer',
                     ColumnTransformer(transformers=[('categorical',
                                                      Pipeline(steps=[('imputation_constant',
                                                                       SimpleImputer(fill_value='missing',
                                                                                     strategy='constant')),
                                                                      ('onehot',
                                                                       OneHotEncoder(handle_unknown='ignore'))]),
                                                      ['state', 'gender']),
                                                     ('numerical',
                                                      Pipeline(steps=[('imputation_mean',
                                                                       SimpleImputer()),
                                                                      ('scaler',
                                                                       StandardScaler())]),
                                                      ['age', 'weight'])])),
                    ('logisticregression', LogisticRegression(max_iter=500))])
Please rerun this cell to show the HTML repr or trust the notebook.


.. GENERATED FROM PYTHON SOURCE LINES 139-145 Displaying a Grid Search over a Pipeline with a Classifier ############################################################################### This section constructs a :class:`~sklearn.model_selection.GridSearchCV` over a :class:`~sklearn.pipeline.Pipeline` with :class:`~sklearn.ensemble.RandomForestClassifier` and displays its visual representation. .. GENERATED FROM PYTHON SOURCE LINES 145-193 .. code-block:: default import numpy as np from sklearn.pipeline import make_pipeline from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder, StandardScaler from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn import set_config numeric_preprocessor = Pipeline( steps=[ ("imputation_mean", SimpleImputer(missing_values=np.nan, strategy="mean")), ("scaler", StandardScaler()), ] ) categorical_preprocessor = Pipeline( steps=[ ( "imputation_constant", SimpleImputer(fill_value="missing", strategy="constant"), ), ("onehot", OneHotEncoder(handle_unknown="ignore")), ] ) preprocessor = ColumnTransformer( [ ("categorical", categorical_preprocessor, ["state", "gender"]), ("numerical", numeric_preprocessor, ["age", "weight"]), ] ) pipe = Pipeline( steps=[("preprocessor", preprocessor), ("classifier", RandomForestClassifier())] ) param_grid = { "classifier__n_estimators": [200, 500], "classifier__max_features": ["auto", "sqrt", "log2"], "classifier__max_depth": [4, 5, 6, 7, 8], "classifier__criterion": ["gini", "entropy"], } grid_search = GridSearchCV(pipe, param_grid=param_grid, n_jobs=1) .. GENERATED FROM PYTHON SOURCE LINES 194-195 To visualize the diagram, change to `display='diagram'`. .. GENERATED FROM PYTHON SOURCE LINES 195-197 .. code-block:: default set_config(display="diagram") grid_search # click on the diagram below to see the details of each step .. raw:: html
GridSearchCV(estimator=Pipeline(steps=[('preprocessor',
                                            ColumnTransformer(transformers=[('categorical',
                                                                             Pipeline(steps=[('imputation_constant',
                                                                                              SimpleImputer(fill_value='missing',
                                                                                                            strategy='constant')),
                                                                                             ('onehot',
                                                                                              OneHotEncoder(handle_unknown='ignore'))]),
                                                                             ['state',
                                                                              'gender']),
                                                                            ('numerical',
                                                                             Pipeline(steps=[('imputation_mean',
                                                                                              SimpleImputer()),
                                                                                             ('scaler',
                                                                                              StandardScaler())]),
                                                                             ['age',
                                                                              'weight'])])),
                                           ('classifier',
                                            RandomForestClassifier())]),
                 n_jobs=1,
                 param_grid={'classifier__criterion': ['gini', 'entropy'],
                             'classifier__max_depth': [4, 5, 6, 7, 8],
                             'classifier__max_features': ['auto', 'sqrt', 'log2'],
                             'classifier__n_estimators': [200, 500]})
Please rerun this cell to show the HTML repr or trust the notebook.


.. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.073 seconds) .. _sphx_glr_download_auto_examples_miscellaneous_plot_pipeline_display.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.0.X?urlpath=lab/tree/notebooks/auto_examples/miscellaneous/plot_pipeline_display.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline_display.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline_display.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_