Apply numpy nonzero row?

I have a 2d boolean array from which I am trying to extract indices of true values. The unnecessary Numry function decomposes my 2d array into a list of x and y positions, which is problematic.

Is it possible to find column indices of elements truewhile maintaining row order?

Each of the true values ​​in the columns is related to each other on the same row, so dividing them into pairs (index of an index row, column) is not useful. Is it possible?

I thought that maybe it np.apply_along_axiscould be useful.

+4
source share
1 answer

I did not quite understand what you wanted (maybe an example will help), but two guesses:

If you want to see if there are any Trues in the line, then:

np.any(a, axis=1)

.

True ,

testarray = np.array([
    [True, False, True],
    [True, True, False],
    [False, False, False],
    [False, True, False]])

collists = [ np.nonzero(t)[0] for t in testarray ]

:

>>> collists
[array([0, 2]), array([0, 1]), array([], dtype=int64), array([1])]

True 3, :

>>> collists[3]
array([1])  

, , . . , , 10000 x 10000, 774 .

+6

Source: https://habr.com/ru/post/1548107/


All Articles