TypeError: unhashable type: 'numpy.ndarray

I keep getting TypeError: unhashable type: 'numpy.ndarray' when I run this code. I'm unable to pinpoint the issue. I am using Python version 3.5.4.



My code:

import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
%matplotlib inline

np.random.seed(1)

X, Y = load_planar_dataset()

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

The stack trace error:

TypeError                                 Traceback (most recent call last)
c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
    173     try:
--> 174         rgba = _colors_full_map.cache[c, alpha]
    175     except (KeyError, TypeError):  # Not in cache, or unhashable.

TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4231             try:  # Then is 'c' acceptable as PathCollection facecolors?
-> 4232                 colors = mcolors.to_rgba_array(c)
   4233                 n_elem = colors.shape[0]

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
    274     for i, cc in enumerate(c):
--> 275         result[i] = to_rgba(cc, alpha)
    276     return result

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
    175     except (KeyError, TypeError):  # Not in cache, or unhashable.
--> 176         rgba = _to_rgba_no_colorcycle(c, alpha)
    177         try:

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
    230     if len(c) not in [3, 4]:
--> 231         raise ValueError("RGBA sequence should have length 3 or 4")
    232     if len(c) == 3 and alpha is None:

ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-12-8d7e20d0c82c> in <module>
      1 # Visualize the data:
----> 2 plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
   2860         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
   2861         verts=verts, edgecolors=edgecolors, **({"data": data} if data
-> 2862         is not None else {}), **kwargs)
   2863     sci(__ret)
   2864     return __ret

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
   1811 
   1812         inner.__doc__ = _add_data_doc(inner.__doc__,

c:\users\aditya shah\appdata\local\programs\python\python35\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4243                         "acceptable for use with 'x' with size {xs}, "
   4244                         "'y' with size {ys}."
-> 4245                         .format(nc=n_elem, xs=x.size, ys=y.size)
   4246                     )
   4247                 # Both the mapping *and* the RGBA conversion failed: pretty

ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 400, 'y' with size 400.

 

If you are posting a general question, always make sure you give reference to packages or modules used which are not standard (or common) Python module so that your error can be reproduced by others.



The error "unhashable type" means somewhere in the code Python called the internal hash function on a wrong type of object. In your case, you are probably passing the "c" argument with a wrong shape. It is 2-d, you want a 1-d list. Matplotlib assumed it is an RGB array and tried to use it as a dictonary keys (and dict keys must be hashable). So change below

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

to

plt.scatter(X[0, :], X[1, :], c=Y[0,:], s=40, cmap=plt.cm.Spectral);

and that should work.