[英]TypeError: unhashable type: 'numpy.ndarray' in plt.scatter
I don't get the following error 我没有以下错误
TypeError: unhashable type: 'numpy.ndarray'
TypeError:无法散列的类型:'numpy.ndarray'
I tried all the possible solution, but I can't figure it out. 我尝试了所有可能的解决方案,但无法弄清楚。
this is my array: 这是我的数组:
instances=np.array([[0,10],
[1,3],
[3,4],
[3,5],
[5,5],
[5,6],
[6,-5],
[5,8]])
and I have a loop here: 我在这里有一个循环:
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
the output is this: 输出是这样的:
0
[0.]
Pred
is a numpy array. Pred
是一个numpy数组。 It cannot be used as an index in self.colors[Pred]
. 它不能用作
self.colors[Pred]
的索引。 You should use self.colors[int(Pred[0])]
. 您应该使用
self.colors[int(Pred[0])]
。
You didn't post the context of your error, but I'm assuming it's something like the one in this question . 您没有发布错误的上下文,但是我假设它类似于此问题中的内容 。
It so, then change: 这样,然后更改:
plt.scatter(p[0], p[1], s=200, marker='*', c=self.colors[Pred])
to: 至:
plt.scatter(p[0], p[1], s=200, marker='*', c=self.colors[Pred].ravel().tolist())
This flattens the array y
to be one-dimensional, and then turns it into a list, which to_rgba
is happy to digest as something it can hash. 这将数组
y
平为一维,然后将其转换为列表, to_rgba
乐于消化它可以散列的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.