Python 使用Scikit Learn绘制部分相关时的值错误

Python 使用Scikit Learn绘制部分相关时的值错误,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我试着从一开始就学习这个例子 print(\uuuuu doc\uuuuuuu) 作为pd进口熊猫 将matplotlib.pyplot作为plt导入 从sklearn.dataset导入加载 从sklearn.neural_网络导入MLPREGESSOR 从sklearn.preprocessing导入StandardScaler 从sklearn.pipeline导入make_pipeline 从sklearn.tree导入决策树 从sklearn.inspection导入绘图\部分\依赖

我试着从一开始就学习这个例子

print(\uuuuu doc\uuuuuuu)
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
从sklearn.dataset导入加载
从sklearn.neural_网络导入MLPREGESSOR
从sklearn.preprocessing导入StandardScaler
从sklearn.pipeline导入make_pipeline
从sklearn.tree导入决策树
从sklearn.inspection导入绘图\部分\依赖
波士顿=加载波士顿()
X=pd.DataFrame(boston.data,columns=boston.feature\u name)
y=boston.target
树=决策树累加器()
mlp=制造管道(StandardScaler(),
MLPREGESSOR(隐藏层大小=(100100),
tol=1e-2,最大值=500,随机状态=0)
树拟合(X,y)
mlp.配合(X,y)
图,ax=plt.子批次(图尺寸=(12,6))
ax.集合标题(“决策树”)
tree_disp=绘图部分依赖(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 list

During 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
为IPython交互环境自动创建的模块
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
~\Anaconda3\lib\site packages\sklearn\inspection\partial\u dependence.py in convert\u功能(fx)
523尝试:
-->524 fx=要素名称。索引(fx)
525除值错误外:
ValueError:“LSTAT”不在列表中
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在里面
23图,ax=plt.子批次(图尺寸=(12,6))
24 ax.集合标题(“决策树”)
--->25 tree_disp=绘图部分依赖(tree,X,[“LSTAT”,“RM”])
~\Anaconda3\lib\site packages\sklearn\inspection\partial\u dependence.py in plot\u partial\u dependence(估计器、X、特征、特征名称、目标、响应方法、n\u cols、网格分辨率、百分位数、方法、n\u作业、详细信息、图、线功率、轮廓功率)
533 fxs=(fxs,)
534尝试:
-->535 fxs=[在fxs中为fx转换功能(fx)]
536除类型错误外:
537 raise VALUE ERROR('功能中的每个条目必须是int,'
~\Anaconda3\lib\site packages\sklearn\inspection\partial\u dependence.py in(.0)
533 fxs=(fxs,)
534尝试:
-->535 fxs=[在fxs中为fx转换功能(fx)]
536除类型错误外:
537 raise VALUE ERROR('功能中的每个条目必须是int,'
~\Anaconda3\lib\site packages\sklearn\inspection\partial\u dependence.py in convert\u功能(fx)
524 fx=要素名称。索引(fx)
525除值错误外:
-->526提升值错误('Feature%s不在Feature_名称'%fx'中)
527返回整数(外汇)
528
ValueError:要素名称中没有要素LSTAT
是我做错了什么,还是教程不起作用了?我曾试图在随机森林模型上绘制部分依赖关系,但得到了相同的错误

任何帮助都将不胜感激


更新:所有错误日志

sklearn可能有问题。请更新到最新版本(0.22.1)。您的代码在此版本下运行正常

需要注意的是:将
ax
添加到
plot\u partial\u dependency
的函数调用中,以分配ax对象:

tree_disp = plot_partial_dependence(tree, X, ["LSTAT", "RM"], ax=ax)

你能提供一个更详细的错误日志吗?你的例子在我使用Python3时运行得很好。@Fourier为缺少信息感到抱歉。我已经更新了itI,我正在运行0.22.1的
sklearn
,它运行没有任何问题。当
X=pd.DataFrame(boston.data,columns=boston.feature\u names)时,我可以重现你的错误。作为\u matrix()
@Fourier我更新为sklearn 0.22.1,然后它就可以工作了。请写下你的答案,这样我就可以选择它作为答案了