How to count occurrences of a digit in numpy array with Python

1 Answer

0 votes
import numpy as np

arr = np.array([0, 1, 1, 2, 2, 3, 4, 5, 1, 1, 1, 3, 3, 2, 0, 0])

count_one = np.count_nonzero(arr == 1)

print(f"Total ccurrences of 1 = {count_one}")



'''
run:

Total ccurrences of 1 = 5

'''

 



answered Feb 17, 2024 by avibootz
...