Issue
I would like to change the ylabel from a partial dependence plot from "partial dependence" to "failure probability".
This post is similar to change x labels in a python sklearn partial dependence plot but the solution did not work and apparently the y_axis is hard coded in the function (line 740 of the current partial_dependence.py
source code).
Solution
The plots share the y-axis, so calling set_ylabel
on an axis may not set the correct one.
Here's how it could be solved:
fig, ax = plt.subplots(1, 1)
pdp = plot_partial_dependence(
clf, X_train, features, feature_names=names, n_jobs=3, ax=ax, grid_resolution=50
)
pdp.axes_[0][0].set_ylabel("Failure Probability")
Full code:
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.inspection import plot_partial_dependence
from sklearn.datasets import fetch_california_housing
import matplotlib.pyplot as plt
cal_housing = fetch_california_housing()
X_train, X_test, y_train, y_test = train_test_split(
cal_housing.data, cal_housing.target, test_size=0.2, random_state=1
)
names = cal_housing.feature_names
clf = GradientBoostingRegressor(
n_estimators=100, max_depth=4, learning_rate=0.1, loss="huber", random_state=1
)
clf.fit(X_train, y_train)
features = [0, 5, 1]
fig, ax = plt.subplots(1, 1)
pdp = plot_partial_dependence(
clf, X_train, features, feature_names=names, n_jobs=3, ax=ax, grid_resolution=50
)
pdp.axes_[0][0].set_ylabel("Failure Probability")
fig.suptitle(
"Partial dependence of house value on nonlocation features\n"
"for the California housing dataset"
)
plt.show()
Result:
Answered By - Alexander L. Hayes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.