In [7]:
%load_ext autoreload
%autoreload 2

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.size'] = 12
plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['lines.linewidth'] = 2.5
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
In [6]:
from sklearn.datasets import load_boston
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.tree import DecisionTreeRegressor
from sklearn.inspection import plot_partial_dependence


##############################################################################
# Train models on the boston housing price dataset
# ================================================
#
# First, we load the boston housing price dataset and split the the dataset
# into a training and test set. Then, we train a histogram gradient boosting
# decision tree and a multi-layer perceptron on the training set.

from sklearn.preprocessing import scale
boston = load_boston()
X, y = boston.data, boston.target
X_train, X_test, y_train, y_test = train_test_split(scale(X), y, test_size=0.1,
                                                    random_state=0)

hgbr = DecisionTreeRegressor()
mlp = MLPRegressor(hidden_layer_sizes=(100, 100),
                    tol=1e-2, max_iter=500, random_state=0)
hgbr.fit(X_train, y_train)
mlp.fit(X_train, y_train)
Out[6]:
MLPRegressor(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,
             beta_2=0.999, early_stopping=False, epsilon=1e-08,
             hidden_layer_sizes=(100, 100), learning_rate='constant',
             learning_rate_init=0.001, max_fun=15000, max_iter=500,
             momentum=0.9, n_iter_no_change=10, nesterovs_momentum=True,
             power_t=0.5, random_state=0, shuffle=True, solver='adam', tol=0.01,
             validation_fraction=0.1, verbose=False, warm_start=False)

Bounds are weird

In [8]:
mlp_disp = plot_partial_dependence(mlp, X_test, ["LSTAT", "RM"],
                                   feature_names=boston.feature_names,
                                   n_cols=3, line_kw={"c": "red"})
In [18]:
hgbr_disp = plot_partial_dependence(hgbr, X_test, ["LSTAT", "RM"],
                                    feature_names=boston.feature_names,
                                    n_cols=3, line_kw={"c": "blue"})
In [20]:
plot_partial_dependence(hgbr, X_test, ["LSTAT", "RM"],
                        feature_names=boston.feature_names, ax=mlp_disp.axes_,
                        n_cols=3, line_kw={"c": "blue"})
Out[20]:
<sklearn.inspection._plot.partial_dependence.PartialDependenceDisplay at 0x1a1edc62d0>
In [14]:
h = plot_partial_dependence(hgbr, X_test, ["LSTAT", "RM"],
                            feature_names=boston.feature_names, ax=mlp_disp.axes_,
                            n_cols=3, line_kw={"c": "blue"})
h.figure_
Out[14]: