我想将部分依赖性图的y轴标签从“部分依赖性”更改为“故障概率”。
此帖子与更改Python sklearn部分依赖性图中的x轴标签类似,但该解决方案不起作用,并且显然y轴是在函数中硬编码的(当前partial_dependence.py
源代码的第740行)。
回答:
这些图共享y轴,因此在轴上调用set_ylabel
可能无法设置正确的轴。
以下是解决方法:
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("故障概率")
完整代码:
from sklearn.model_selection import train_test_splitfrom sklearn.ensemble import GradientBoostingRegressorfrom sklearn.inspection import plot_partial_dependencefrom sklearn.datasets import fetch_california_housingimport matplotlib.pyplot as pltcal_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_namesclf = 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("故障概率")fig.suptitle( "加州住房数据集中房屋价值对非位置特征的部分依赖性\n")plt.show()
结果: