|
93 | 93 | hist_no_interact, X, y, cv=5, n_jobs=2, train_sizes=np.linspace(0.1, 1, 5)
|
94 | 94 | )
|
95 | 95 |
|
| 96 | +# %% |
| 97 | +# :class:`~inspection.PartialDependenceDisplay` exposes a new parameter |
| 98 | +# `categorical_features` to display partial dependence for categorical features |
| 99 | +# using bar plots and heatmaps. |
| 100 | +from sklearn.datasets import fetch_openml |
| 101 | + |
| 102 | +X, y = fetch_openml( |
| 103 | + "titanic", version=1, as_frame=True, return_X_y=True, parser="pandas" |
| 104 | +) |
| 105 | +X = X.select_dtypes(["number", "category"]).drop(columns=["body"]) |
| 106 | + |
| 107 | +# %% |
| 108 | +from sklearn.preprocessing import OrdinalEncoder |
| 109 | +from sklearn.pipeline import make_pipeline |
| 110 | + |
| 111 | +categorical_features = ["pclass", "sex", "embarked"] |
| 112 | +model = make_pipeline( |
| 113 | + ColumnTransformer( |
| 114 | + transformers=[("cat", OrdinalEncoder(), categorical_features)], |
| 115 | + remainder="passthrough", |
| 116 | + ), |
| 117 | + HistGradientBoostingRegressor(random_state=0), |
| 118 | +).fit(X, y) |
| 119 | + |
| 120 | +# %% |
| 121 | +from sklearn.inspection import PartialDependenceDisplay |
| 122 | + |
| 123 | +fig, ax = plt.subplots(figsize=(14, 4), constrained_layout=True) |
| 124 | +_ = PartialDependenceDisplay.from_estimator( |
| 125 | + model, |
| 126 | + X, |
| 127 | + features=["age", "sex", ("pclass", "sex")], |
| 128 | + categorical_features=categorical_features, |
| 129 | + ax=ax, |
| 130 | +) |
| 131 | + |
96 | 132 | # %%
|
97 | 133 | # Faster parser in :func:`~datasets.fetch_openml`
|
98 | 134 | # -----------------------------------------------
|
99 | 135 | # :func:`~datasets.fetch_openml` now supports a new `"pandas"` parser that is
|
100 | 136 | # more memory and CPU efficient. In v1.4, the default will change to
|
101 | 137 | # `parser="auto"` which will automatically use the `"pandas"` parser for dense
|
102 | 138 | # data and `"liac-arff"` for sparse data.
|
103 |
| -from sklearn.datasets import fetch_openml |
104 |
| - |
105 | 139 | X, y = fetch_openml(
|
106 | 140 | "titanic", version=1, as_frame=True, return_X_y=True, parser="pandas"
|
107 | 141 | )
|
|
0 commit comments