python - 使用 Scikit Learn 进行部分依赖绘图时出现 ValueError

标签 python machine-learning scikit-learn

我尝试遵循 Scikit-Learn site 中的示例

print(__doc__)

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.tree import DecisionTreeRegressor
from sklearn.inspection import plot_partial_dependence

boston = load_boston()
X = pd.DataFrame(boston.data, columns=boston.feature_names)
y = boston.target

tree = 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 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

我做错了什么或者教程不再起作用了吗?我尝试绘制对随机森林模型的部分依赖关系,但得到了相同的错误。

感谢任何帮助

更新:所有错误日志

最佳答案

sklearn 可能出现问题。请更新到最新版本(0.22.1)。您的代码在此版本中可以完美运行。

一个小旁注:将 ax 添加到 plot_partial_dependence 的函数调用中以分配 ax 对象:

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

关于python - 使用 Scikit Learn 进行部分依赖绘图时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60372345/

相关文章:

python - Plone 4 : Passing arguments to view class (BrowserView)

python - 在 NumPy 中获取 ndarray 的索引和值

python - 如何在 sklearn Python 中绘制 SVM 决策边界?

python - 将txt文件解析成字典写入csv文件

python - 防止 Django 模板中的换行符分割名称列表

python - graphlab create sframe 如何获取 SArray 中位数

machine-learning - MNIST隐藏层直观理解

search - Find-S/候选消除算法的训练样例最少数量?

python - 用于交叉列列表的一致 ColumnTransformer

python - sklearn中的预定义Split函数