2024-04-29 09:07:40 发布
网友
我想将部分依赖图中的ylabel从“部分依赖”更改为“失效概率”
这篇文章类似于change x labels in a python sklearn partial dependence plot,但解决方案不起作用,显然y_轴是在函数(line 740 of the current ^{} source code)中硬编码的
这些图共享y轴,因此在轴上调用set_ylabel可能无法设置正确的轴
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("Failure Probability")
完整代码:
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()
结果:
这些图共享y轴,因此在轴上调用
set_ylabel
可能无法设置正确的轴以下是解决问题的方法:
完整代码:
结果:
相关问题 更多 >
编程相关推荐