How to access indexs of binary numpy array column_wise using argwhere function

Question:

for example, I have a numpy array-like

    import numpy as np
    x=np.array([[1, 1, 0, 0, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
        [0, 0, 1, 1, 0, 0, 1, 0, 0, 0],
        [0, 1, 0, 1, 0, 0, 0, 0, 1, 0],
        [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]])        

current output:
ind=np.argwhere(x)
#the accessed indexes are row-wise is it possible to access in column_wise

required output:

     [[0 0]
     [4 0]
     [0 1]
     [3 1]
     [2 2]
     [1 3]
     [2 3]
     [3 3]
     [0 4]
     [1 5]
     [2 6]
     [4 6]
     [3 8]]
Asked By: raja

||

Answers:

You could transpose and swap the columns.

ind = np.argwhere(x.T)[:, [1, 0]]
Answered By: bitflip

This function also give the same output :

import scipy as sp
sp.sparse.csc_matrix(x)
Answered By: raja
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.