Search code examples
pythonnumpymultidimensional-arraycounter

TypeError: unhashable type: 'numpy.ndarray', Counter rows


I'm trying to view the frequency of the elements in a 2d array as in the code:

a = np.array([22,33,22,55])
b = np.array([66,77,66,99])

x = np.column_stack((a,b))

print(collections.Counter(x))

Expected result: ({(22, 66): 2, (33, 77): 1, (55, 99): 1})

But I get:

 File "/Users/Documents/dos.py", line 8, in <module>
     print(collections.Counter(x))
 File "/opt/anaconda3/lib/python3.8/collections/__init__.py", line 552, in __init__
     self.update(iterable, **kwds)
 File "/opt/anaconda3/lib/python3.8/collections/__init__.py", line 637, in update
     _count_elements(self, iterable)
TypeError: unhashable type: 'numpy.ndarray'

Solution

  • In [146]: a = np.array([22,33,22,55])
         ...: b = np.array([66,77,66,99])
         ...: 
         ...: x = np.column_stack((a,b))
         ...: 
    In [147]: x
    Out[147]: 
    array([[22, 66],
           [33, 77],
           [22, 66],
           [55, 99]])
    In [148]: from collections import Counter
    

    Create a list of tuples: (tuples are hashable)

    In [149]: xl = [tuple(row) for row in x]
    In [150]: xl
    Out[150]: [(22, 66), (33, 77), (22, 66), (55, 99)]
    

    Now Counter works:

    In [151]: Counter(xl)
    Out[151]: Counter({(22, 66): 2, (33, 77): 1, (55, 99): 1})
    

    numpys own unique also works

    In [154]: np.unique(x, axis=0, return_counts=True)
    Out[154]: 
    (array([[22, 66],
            [33, 77],
            [55, 99]]),
     array([2, 1, 1]))