【问题标题】:ValueError When Plotting With Partial Dependence Using Scikit Learn使用 Scikit Learn 绘制部分依赖时的 ValueError
【发布时间】:2020-06-07 20:56:07
【问题描述】:

我尝试按照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

是我做错了什么还是教程不再有效?我试图绘制对我的随机森林模型的部分依赖,但得到了同样的错误。

感谢任何帮助

更新:所有错误日志

【问题讨论】:

  • 能否提供更详细的错误日志。使用 Python3,您的示例对我来说运行良好。
  • @Fourier 抱歉信息不足。我已经更新了
  • 我正在运行 sklearn 的 0.22.1,它运行没有任何问题。 X = pd.DataFrame(boston.data, columns=boston.feature_names).as_matrix() 时我可以重现你的错误
  • @Fourier 我更新到 sklearn 0.22.1 然后它可以工作了。请写下你的答案,这样我就可以选择它作为答案

标签: python machine-learning scikit-learn


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-03-27
    • 2014-04-05
    • 2017-04-17
    • 2020-03-28
    • 1970-01-01
    • 2017-06-12
    • 2018-01-24
    • 2021-11-19
    • 2017-08-25
    相关资源
    最近更新 更多