Christel Junco
Christel Junco

Reputation: 133

TypeError: unhashable type: 'numpy.ndarray' in plt.scatter

I don't get the following error

TypeError: unhashable type: '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.]

Upvotes: 2

Views: 11486

Answers (2)

Tom Hale
Tom Hale

Reputation: 46745

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.

Upvotes: 1

DYZ
DYZ

Reputation: 57033

Pred is a numpy array. It cannot be used as an index in self.colors[Pred]. You should use self.colors[int(Pred[0])].

Upvotes: 1

Related Questions