我尝试按照Scikit-Learn网站的示例进行操作
print(__doc__)import pandas as pdimport matplotlib.pyplot as pltfrom sklearn.datasets import load_bostonfrom sklearn.neural_network import MLPRegressorfrom sklearn.preprocessing import StandardScalerfrom sklearn.pipeline import make_pipelinefrom sklearn.tree import DecisionTreeRegressorfrom sklearn.inspection import plot_partial_dependenceboston = load_boston()X = pd.DataFrame(boston.data, columns=boston.feature_names)y = boston.targettree = DecisionTreeRegressor()mlp = make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(100, 100), tol=1e-2, max_iter=500, random_state=0))tree.fit(X, y)mlp.fit(X, y)fig, ax = plt.subplots(figsize=(12, 6))ax.set_title("Decision Tree")tree_disp = plot_partial_dependence(tree, X, ["LSTAT", "RM"])
但我得到了一个错误
Automatically created module for IPython interactive environment---------------------------------------------------------------------------ValueError Traceback (most recent call last)~\Anaconda3\lib\site-packages\sklearn\inspection\partial_dependence.py in convert_feature(fx) 523 try:--> 524 fx = feature_names.index(fx) 525 except ValueError:ValueError: 'LSTAT' is not in listDuring handling of the above exception, another exception occurred:ValueError Traceback (most recent call last)<ipython-input-8-2bdead960e12> in <module> 23 fig, ax = plt.subplots(figsize=(12, 6)) 24 ax.set_title("Decision Tree")---> 25 tree_disp = plot_partial_dependence(tree, X, ["LSTAT", "RM"])~\Anaconda3\lib\site-packages\sklearn\inspection\partial_dependence.py in plot_partial_dependence(estimator, X, features, feature_names, target, response_method, n_cols, grid_resolution, percentiles, method, n_jobs, verbose, fig, line_kw, contour_kw) 533 fxs = (fxs,) 534 try:--> 535 fxs = [convert_feature(fx) for fx in fxs] 536 except TypeError: 537 raise ValueError('Each entry in features must be either an int, '~\Anaconda3\lib\site-packages\sklearn\inspection\partial_dependence.py in <listcomp>(.0) 533 fxs = (fxs,) 534 try:--> 535 fxs = [convert_feature(fx) for fx in fxs] 536 except TypeError: 537 raise ValueError('Each entry in features must be either an int, '~\Anaconda3\lib\site-packages\sklearn\inspection\partial_dependence.py in convert_feature(fx) 524 fx = feature_names.index(fx) 525 except ValueError:--> 526 raise ValueError('Feature %s not in feature_names' % fx) 527 return int(fx) 528 ValueError: Feature LSTAT not in feature_names
是我做错了什么,还是教程已经不再适用?我也尝试在我自己的随机森林模型上绘制部分依赖图,但得到了相同的错误。
任何帮助都会被感激
更新:所有错误日志
回答:
sklearn
可能出现了一些问题。请更新到最新版本(0.22.1)。使用这个版本,您的代码可以完美运行。
一个小提示:在调用plot_partial_dependence
函数时添加ax
参数以分配ax对象:
tree_disp = plot_partial_dependence(tree, X, ["LSTAT", "RM"], ax=ax)