%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
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)
mlp_disp = plot_partial_dependence(mlp, X_test, ["LSTAT", "RM"],
feature_names=boston.feature_names,
n_cols=3, line_kw={"c": "red"})
hgbr_disp = plot_partial_dependence(hgbr, X_test, ["LSTAT", "RM"],
feature_names=boston.feature_names,
n_cols=3, line_kw={"c": "blue"})
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 = 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_