def test_partial_dependence_display_wrong_len_kind( pyplot, clf_diabetes, diabetes, ): """Check that we raise an error when `kind` is a list with a wrong length. This case can only be triggered using the `PartialDependenceDisplay.from_estimator` method. """ disp = PartialDependenceDisplay.from_estimator( clf_diabetes, diabetes.data, features=[0, 2], grid_resolution=20, kind="average", # len(kind) != len(features) ) # alter `kind` to be a list with a length different from length of `features` disp.kind = ["average"] err_msg = ( r"When `kind` is provided as a list of strings, it should contain as many" r" elements as `features`. `kind` contains 1 element\(s\) and `features`" r" contains 2 element\(s\)." ) with pytest.raises(ValueError, match=err_msg): disp.plot()
from sklearn.inspection import partial_dependence from sklearn.inspection import PartialDependenceDisplay print("Computing partial dependence plots...") tic = time() features = ["MedInc", "AveOccup", "HouseAge", "AveRooms"] display = PartialDependenceDisplay.from_estimator( est, X_train, features, kind="both", subsample=50, n_jobs=3, grid_resolution=20, random_state=0, ice_lines_kw={ "color": "tab:blue", "alpha": 0.2, "linewidth": 0.5 }, pd_line_kw={ "color": "tab:orange", "linestyle": "--" }, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( "Partial dependence of house value on non-location features\n" "for the California housing dataset, with MLPRegressor") display.figure_.subplots_adjust(hspace=0.3)
from sklearn.inspection import PartialDependenceDisplay common_params = { "subsample": 50, "n_jobs": 2, "grid_resolution": 20, "centered": True, "random_state": 0, } print("Computing partial dependence plots...") tic = time() display = PartialDependenceDisplay.from_estimator( est, X_train, features=["MedInc", "AveOccup", "HouseAge", "AveRooms"], kind="both", **common_params, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( "Partial dependence of house value on non-location features\n" "for the California housing dataset, with MLPRegressor") display.figure_.subplots_adjust(hspace=0.3) # %% # Gradient boosting # ................. # # Let's now fit a :class:`~sklearn.ensemble.HistGradientBoostingRegressor` and # compute the partial dependence on the same features.
tree.fit(X, y) mlp.fit(X, y) # %% # Plotting partial dependence for two features # ============================================ # # We plot partial dependence curves for features "age" and "bmi" (body mass # index) for the decision tree. With two features, # :func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` expects to plot # two curves. Here the plot function place a grid of two plots using the space # defined by `ax` . fig, ax = plt.subplots(figsize=(12, 6)) ax.set_title("Decision Tree") tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age", "bmi"], ax=ax) # %% # The partial dependence curves can be plotted for the multi-layer perceptron. # In this case, `line_kw` is passed to # :func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` to change the # color of the curve. fig, ax = plt.subplots(figsize=(12, 6)) ax.set_title("Multi-layer Perceptron") mlp_disp = PartialDependenceDisplay.from_estimator(mlp, X, ["age", "bmi"], ax=ax, line_kw={"color": "red"}) # %%
X = np.c_[f_0, f_1] noise = rng.normal(loc=0.0, scale=0.01, size=n_samples) y = 5 * f_0 + np.sin(10 * np.pi * f_0) - 5 * f_1 - np.cos( 10 * np.pi * f_1) + noise fig, ax = plt.subplots() # Without any constraint gbdt = HistGradientBoostingRegressor() gbdt.fit(X, y) disp = PartialDependenceDisplay.from_estimator( gbdt, X, features=[0, 1], line_kw={ "linewidth": 4, "label": "unconstrained", "color": "tab:blue" }, ax=ax, ) # With positive and negative constraints gbdt = HistGradientBoostingRegressor(monotonic_cst=[1, -1]) gbdt.fit(X, y) PartialDependenceDisplay.from_estimator( gbdt, X, features=[0, 1], feature_names=(