Print all rows of an array not equal to zero in Python

Question:

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]
Asked By: AEinstein

||

Answers:

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!

Answered By: nonlinear
import numpy as np

out = ... # 2d array here

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

# rows:
# [1, 2]
Answered By: Chrysophylaxs
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.

Answered By: buec95

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)
Answered By: Shakir Sadiq
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.