我试着从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对象:相关问题 更多 >
编程相关推荐