Skip to content

Commit e1a5f07

Browse files
ArturoAmorQArturoAmorQ
authored andcommitted
MNT Add info about the estimators html diagram (INRIA#844)
1 parent ff94dea commit e1a5f07

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

python_scripts/03_categorical_pipeline_visualization.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,41 @@
9797
("classifier", LogisticRegression()),
9898
]
9999
)
100+
model
100101

101102
# %% [markdown]
102-
# Let's visualize it!
103+
# Let's fit it!
103104

104105
# %%
105-
model
106+
model.fit(data, target)
107+
108+
# %% [markdown]
109+
# Notice that the diagram changes color once the estimator is fit.
110+
#
111+
# So far we used `Pipeline` and `ColumnTransformer`, which allows us to custom
112+
# the names of the steps in the pipeline. An alternative is to use
113+
# `make_column_transformer` and `make_pipeline`, they do not require, and do not
114+
# permit, naming the estimators. Instead, their names are set to the lowercase
115+
# of their types automatically.
116+
117+
# %%
118+
from sklearn.compose import make_column_transformer
119+
from sklearn.pipeline import make_pipeline
120+
121+
numeric_transformer = make_pipeline(
122+
SimpleImputer(strategy="median"), StandardScaler()
123+
)
124+
categorical_transformer = OneHotEncoder(handle_unknown="ignore")
125+
126+
preprocessor = make_column_transformer(
127+
(numeric_transformer, numeric_features),
128+
(categorical_transformer, categorical_features),
129+
)
130+
model = make_pipeline(preprocessor, LogisticRegression())
131+
model.fit(data, target)
106132

107133
# %% [markdown]
108-
# ## Finally we score the model
134+
# ## Finally we can score the model using cross-validation:
109135

110136
# %%
111137
from sklearn.model_selection import cross_validate

0 commit comments

Comments
 (0)