Skip to content

Commit f7b7129

Browse files
author
SebastienMelo
committed
changed additional mentions of ColumnTransformer
1 parent 5e3451c commit f7b7129

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

python_scripts/03_categorical_pipeline_column_transformer.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@
7474
# categories.
7575
# * **numerical scaling** numerical features which will be standardized.
7676
#
77-
# Now, we create our `ColumnTransfomer` by specifying three values: the
78-
# preprocessor name, the transformer, and the columns. First, let's create the
79-
# preprocessors for the numerical and categorical parts.
77+
# Now, we create our `ColumnTransfomer` using the helper function
78+
# `make_column_transformer`. We specify two values: the transformer, and the
79+
# columns. First, let's create the preprocessors for the numerical and
80+
# categorical parts.
8081

8182
# %%
8283
from sklearn.preprocessing import OneHotEncoder, StandardScaler
@@ -89,13 +90,11 @@
8990
# their respective columns.
9091

9192
# %%
92-
from sklearn.compose import ColumnTransformer
93+
from sklearn.compose import make_column_transformer
9394

94-
preprocessor = ColumnTransformer(
95-
[
96-
("one-hot-encoder", categorical_preprocessor, categorical_columns),
97-
("standard_scaler", numerical_preprocessor, numerical_columns),
98-
]
95+
preprocessor = make_column_transformer(
96+
(categorical_preprocessor, categorical_columns),
97+
(numerical_preprocessor, numerical_columns),
9998
)
10099

101100
# %% [markdown]
@@ -234,8 +233,8 @@
234233
handle_unknown="use_encoded_value", unknown_value=-1
235234
)
236235

237-
preprocessor = ColumnTransformer(
238-
[("categorical", categorical_preprocessor, categorical_columns)],
236+
preprocessor = make_column_transformer(
237+
(categorical_preprocessor, categorical_columns),
239238
remainder="passthrough",
240239
)
241240

python_scripts/parameter_tuning_sol_02.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@
3131
)
3232

3333
# %%
34-
from sklearn.compose import ColumnTransformer
34+
from sklearn.compose import make_column_transformer
3535
from sklearn.compose import make_column_selector as selector
3636
from sklearn.preprocessing import OrdinalEncoder
3737

3838
categorical_preprocessor = OrdinalEncoder(
3939
handle_unknown="use_encoded_value", unknown_value=-1
4040
)
41-
preprocessor = ColumnTransformer(
42-
[
43-
(
44-
"cat_preprocessor",
45-
categorical_preprocessor,
46-
selector(dtype_include=object),
47-
)
48-
],
41+
preprocessor = make_column_transformer(
42+
(categorical_preprocessor, selector(dtype_include=object)),
4943
remainder="passthrough",
5044
)
5145

@@ -121,3 +115,5 @@
121115
test_score = model.score(data_test, target_test)
122116

123117
print(f"Test score after the parameter tuning: {test_score:.3f}")
118+
119+
# %%

0 commit comments

Comments
 (0)