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]]
Answers:
You could transpose and swap the columns.
ind = np.argwhere(x.T)[:, [1, 0]]
This function also give the same output :
import scipy as sp
sp.sparse.csc_matrix(x)
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]]
You could transpose and swap the columns.
ind = np.argwhere(x.T)[:, [1, 0]]
This function also give the same output :
import scipy as sp
sp.sparse.csc_matrix(x)