0

I have a list of ndarrays and I want to find the number of unique ndarrays in it.

For example:

l = [ np.array([1,2,3]), np.array([2,3,4]), np.array([1,2,3]) ]
l
=> [array([1, 2, 3]), array([2, 3, 4]), np.array([1,2,3])]

np.unique(l) expected outcome:
=> [array([1, 2, 3]), array([2, 3, 4])]

When I try to use np.unique() I get TypeError: unhashable type: 'numpy.ndarray'

Can anyone suggest an efficient way to get this? (I can use map, calc some hash value from the arrays and run up.unique on that, but I'm wondering if there is a better way.)

Thanks!

Zach Moshe
  • 2,782
  • 4
  • 24
  • 40
  • possible duplicate of [Efficiently count the number of occurrences of unique subarrays in NumPy?](http://stackoverflow.com/questions/30879446/efficiently-count-the-number-of-occurrences-of-unique-subarrays-in-numpy) – styvane Jun 23 '15 at 21:51
  • 1
    first of all, your arrays in `l` are in **1d**, can you show what `ndarray` you actually tried to get unique out of it and what is your expected outcome? – Anzel Jun 23 '15 at 21:52

1 Answers1

0

You can cast the arrays to tuples:

 len(set([tuple(v) for v in l]))
AndreaL
  • 150
  • 8
  • works. thanks! can anyone share about efficiency of this for large l and/or large subarrays (what's the cost of tuple(v)?) ? – Zach Moshe Jun 24 '15 at 06:31