#!/usr/bin/env python # coding: utf-8 # In[7]: get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('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 # 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) # ## 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"}) # 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_