|
97 | 97 | ("classifier", LogisticRegression()), |
98 | 98 | ] |
99 | 99 | ) |
| 100 | +model |
100 | 101 |
|
101 | 102 | # %% [markdown] |
102 | | -# Let's visualize it! |
| 103 | +# Let's fit it! |
103 | 104 |
|
104 | 105 | # %% |
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) |
106 | 132 |
|
107 | 133 | # %% [markdown] |
108 | | -# ## Finally we score the model |
| 134 | +# ## Finally we can score the model using cross-validation: |
109 | 135 |
|
110 | 136 | # %% |
111 | 137 | from sklearn.model_selection import cross_validate |
|
0 commit comments