I have a 2-dimensional numpy array of following format:
now how to print the frequency of unique elements in this 2d numpy array, so that it returns count([1. 0.]) = 1 and count([0. 1.]) = 1
? I know how to do this using loops, but is there any better pythonic way to do this.
Advertisement
Answer
You can use numpy.unique()
, for axis=0, and pass return_counts=True
, It will return a tuple with unique values, and the counts for these values.
np.unique(arr, return_counts=True, axis=0)
OUTPUT:
(array([[0, 1], [1, 0]]), array([1, 1], dtype=int64))