-1

How do I extract unique values from an ndarray?

import numpy as np

arr = np.array([[1, 2, 2], [2, 2, 3]], np.int32)

set(arr)
Traceback (most recent call last):
  File "/home/me/PycharmProjects/project/example.py", line 6, in <module>
    print(set(arr))
TypeError: unhashable type: 'numpy.ndarray'
DanielBell99
  • 896
  • 5
  • 25
  • 57
  • https://stackoverflow.com/a/15637512/7955271 suggests `pandas.unique()` is faster FYI – Vin Nov 29 '22 at 15:29

1 Answers1

1
import numpy as np

arr = np.array([[1, 2, 2], [2, 2, 3]], np.int32)

print(arr)
print(np.unique(arr))
[[1 2 2]
 [2 2 3]]

[1 2 3]

Documentation

DanielBell99
  • 896
  • 5
  • 25
  • 57