0

I have an array out. I want to print row numbers which have at least one non-zero element. However, I am getting an error. I present the expected output.

import numpy as np

out = np.array([[  0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 423.81345923,   0.        , 407.01354328,
        419.14952534,   0.        , 212.13245959,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [402.93473651,   0.        , 216.08166277, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]])

for i in range(0,len(out)):
    if (out[i]==0):
        print(i)
    else:
        print("None")

The error is

in <module>
    if (out[i]==0):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The expected output is

[1,2]
1
  • if any(out[i]): or if out[i].any(): Commented Dec 30, 2022 at 12:07

4 Answers 4

2
import numpy as np

out = ... # 2d array here

rows = np.where(out.any(axis=1))[0].tolist()

# rows:
# [1, 2]
Sign up to request clarification or add additional context in comments.

Comments

1

The issue with your code is that out[i] is an array. When you check if this array is equal to zero, it returns an array of booleans. The if statement then returns an error. The following code should work:

solution = []
for idx, e in enumerate(out):
    if any(e): # if any element of the array is nonzero
        solution.append(idx)
print(solution)

The output is :

[1, 2]

I hope this helps!

2 Comments

How do I print the output like [1,2] using your code?
You need to store the values in a list. I will edit my answer.
1
import numpy as np

out = np.array([[  0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        , 423.81345923,   0.        , 407.01354328,
        419.14952534,   0.        , 212.13245959,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [402.93473651,   0.        , 216.08166277, 407.01354328,
          0.        , 414.17017965,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ],
       [  0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ,
          0.        ,   0.        ,   0.        ,   0.        ]])

for i in range(0,len(out)):
    if (abs(out[i]).sum()!=0):
        print(i)
    else:
        print("None")

This sums up all values of each index and prints you all rows that contain any non-zero values.

3 Comments

What if we have an array like [0, -1, 0, 1]? Better to use numpy's any :)
You also should avoid using for i in range(0,len(out)) and indexing out after. It would be better to use enumerate.
@Chrysophylaxs thanks for mentioning that! I added the abs() function to overcome this issue
0

We have where and unique attributes in numpy which can help you to achieve this.

get_index = np.where(out != 0)[0]
rows = np.unique(get_index)
print(rows)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.