1.

How do you count the frequency of a given positive value appearing in the NumPy array?

Answer»

We can make use of the bincount() function to compute the number of times a given value is there in the array. This function accepts only positive integers and boolean expressions as the arguments.

import numpy as np
arr = np.array([1, 2, 1, 3, 5, 0, 0, 0, 2, 3])
result = np.bincount(arr)
print(result)

The result is:

[3 2 2 2 0 1]

It has to be noted here that each element represents the count of the corresponding index value present in the original array. This is demonstrated in the below image:




Discussion

No Comment Found