Home > Mobile >  Can't understand why numpy's count_nonzero() seems not to be applicable to an ndarray
Can't understand why numpy's count_nonzero() seems not to be applicable to an ndarray

Time:01-04

I'm trying to use numpy's count_nonzero(), but I keep getting an error message.

import numpy as np
arr = np.full( (3, 3), 1, dtype='int')
print(arr.sum())                  # Produces 9 as expected
print(arr.count_nonzero(arr==1))  # Generates an error message

9
AttributeError: 'numpy.ndarray' object has no attribute 'count_nonzero'

This seems so simple, I can't understand what I'm missing. Any suggestions appreciated. Thanks.

CodePudding user response:

count_nonzero should be used like this

print(np.count_nonzero(arr))
  • Related