【问题标题】:Unhashable type "slice" when trying to plot Random Forest尝试绘制随机森林时不可散列的类型“切片”
【发布时间】:2018-10-12 12:41:23
【问题描述】:

我正在尝试绘制我的随机森林,但它给了我以下错误:TypeError: unhashable type: 'slice' 除了显示空图。该模型本身运行良好,给了我可接受的精度和召回分数。

from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators=500, random_state=0) 
forest.fit(train_x, train_y) 

import matplotlib as plt
import matplotlib.pyplot as plt
import mglearn

fig, axes = plt.subplots(2, 3, figsize=(20, 10))
for i, (ax, tree) in enumerate(zip(axes.ravel(), forest.estimators_)):
    ax.set_title("Tree {}".format(i))
    mglearn.plots.plot_tree_partition(train_x, train_y, tree, ax=ax)

mglearn.plots.plot_2d_separator(forest, train_x, fill=True, ax=axes[-1, -1],
                                alpha=.4)
axes[-1, -1].set_title("Random Forest")
mglearn.discrete_scatter(train_x[:, 0], train_x[:, 1], train_y)

查看下面的详细错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-67-26f28abf17a0> in <module>()
      2 for i, (ax, tree) in enumerate(zip(axes.ravel(), forest.estimators_)):
      3     ax.set_title("Tree {}".format(i))
----> 4     mglearn.plots.plot_tree_partition(train_x, train_y, tree, ax=ax)
      5 
      6 mglearn.plots.plot_2d_separator(forest, train_x, fill=True, ax=axes[-1, -1],

~\AppData\Local\Continuum\anaconda3\lib\site-packages\mglearn\plot_interactive_tree.py in plot_tree_partition(X, y, tree, ax)
     65     eps = X.std() / 2.
     66 
---> 67     x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
     68     y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
     69     xx = np.linspace(x_min, x_max, 1000)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2137             return self._getitem_multilevel(key)
   2138         else:
-> 2139             return self._getitem_column(key)
   2140 
   2141     def _getitem_column(self, key):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2144         # get column
   2145         if self.columns.is_unique:
-> 2146             return self._get_item_cache(key)
   2147 
   2148         # duplicate columns & possible reduce dimensionality

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1838         """Return the cached item, item represents a label indexer."""
   1839         cache = self._item_cache
-> 1840         res = cache.get(item)
   1841         if res is None:
   1842             values = self._data.get(item)

TypeError: unhashable type: 'slice'

【问题讨论】:

  • 由于您在此处从 mglearn 或 pandas 收到错误消息,因此您需要确保您的问题已被相应标记。

标签: python pandas matplotlib scikit-learn random-forest


【解决方案1】:

错误来自pandas,因为函数不支持

尝试在您的plot_2d_separator 中用train_x.values 替换您的数据框train_x

希望这会有所帮助,
干杯

【讨论】:

  • 我改了,现在错误不一样了:KeyError: 'figure.constrained_layout.use'
  • 您能提供一下您正在使用的数据集及其形状吗?
  • 还有你得到的完整错误,所以我可以在本地重现它,因为我在玩具数据集上尝试了你的代码,它可以工作:)
  • 嗨,马纳尔!我在问题中添加了 train_y 和 train_x 的图片。它们基本上在熊猫数据框中。 :) 我在 Jupyter notebook 中运行它。
  • 嗯,我只是重新运行它并得到一个新错误:IndexError:数组索引过多......但第一个情节显示了一些东西......其他人是空的。我想这是一些进步
猜你喜欢
  • 2017-01-12
  • 2020-10-08
  • 2019-09-05
  • 2013-11-27
  • 2017-03-15
  • 2021-07-01
  • 2018-07-10
  • 1970-01-01
  • 2015-09-23
相关资源
最近更新 更多