【问题标题】:TypeError: unhashable type: 'numpy.ndarray' in plt.scatterTypeError:不可散列的类型:plt.scatter 中的“numpy.ndarray”
【发布时间】:2018-07-29 08:08:44
【问题描述】:

我没有收到以下错误

TypeError: unhashable type: 'numpy.ndarray'

我尝试了所有可能的解决方案,但我无法弄清楚。

这是我的数组:

instances=np.array([[0,10],
                            [1,3],
                            [3,4],
                            [3,5],
                            [5,5],
                            [5,6],
                            [6,-5],
                            [5,8]])

我这里有一个循环:

for p in instances:                
    Pred=clf.predict([p])

    print(p[0])    
    print(Pred)

    plt.scatter(p[0], p[1], s=200, marker='*', c=self.colors[Pred])
return Pred

输出是这样的:

0    
[0.]

【问题讨论】:

  • 老实说,我无法弄清楚您的问题是什么。请编辑澄清。
  • 你不能在函数外使用return Pred

标签: python python-3.x numpy matplotlib svm


【解决方案1】:

Pred 是一个 numpy 数组。它不能用作self.colors[Pred] 中的索引。你应该使用self.colors[int(Pred[0])]

【讨论】:

    【解决方案2】:

    您没有发布错误的上下文,但我假设它类似于 this question 中的内容。

    这样,然后改变:

        plt.scatter(p[0], p[1], s=200, marker='*', c=self.colors[Pred])
    

    到:

        plt.scatter(p[0], p[1], s=200, marker='*', c=self.colors[Pred].ravel().tolist())
    

    这会将数组 y 扁平化为一维,然后将其转换为列表,to_rgba 很乐意将其消化为可以散列的东西。

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2022-01-08
      • 2019-03-22
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多