使用Scikit Learn绘制部分依赖图时的ValueError

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注