Ask Your Question

Determine whether a numpy array exclusively consists of zeroes.

asked 2023-03-27 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2023-03-30 00:00:00 +0000

lakamha gravatar image

You can use the numpy function np.count_nonzero to count the number of non-zero elements in an array. If the count is 0, then the array consists exclusively of zeros.

Here's an example:

import numpy as np # Example array a = np.zeros((3, 4)) # Check if array exclusively consists of zeros if np.count_nonzero(a) == 0: print("Array contains only zeros") else: print("Array contains at least one non-zero element") 


Array contains only zeros
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2023-03-27 11:00:00 +0000

Seen: 17 times

Last updated: Mar 30 '23