Determine whether a numpy array exclusively consists of zeroes.

add a comment

3

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") `

Output:

```
Array contains only zeros
```

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

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

Seen: **17 times**

Last updated: **Mar 30 '23**

How can I set up Gunicorn with a Django Project?

Looking for a Python Module that finds Tags for a Text describing its Content

Need a Function in Python to remove entries less than 2 digits from an Array

How can I convert a Document in Python?

How can I program a Loop in Python?

How can I enable Python Code Highlighting in Askbot?

Copyright QStack.ai, 2010-2023. Content on this site is licensed under the Creative Commons Attribution Share Alike 3.0 license.