Compress numpy array (matrix) by deleting columns using another numpy array as mask

I have a 2D numpy array (i.e. matrix) A , which contains useful data intersecting with garbage in the form of column vectors, and also a selection array B , which contains "1" for these columns which are important, and 0 for those who does not do this. Is there a way to select only those columns from A that correspond to units in B ? i.e. matrix

 A = array([[ 0, 1, 2, 3, 4], and a vector B = array([ 0, 1, 0, 1, 0]) [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) 

I want too

 array([[1, 3], [6, 8], [11, 13], [16, 18], [21, 23]]) 

Is there an elegant way to do this? Right now, I have a for loop that iterates through B

NOTE. The matrices I'm dealing with are large, so I don't want to use numpy masked arrays, since I just don't want masked data

+6
source share
3 answers
 >>> A array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) >>> B = NP.array([ 0, 1, 0, 1, 0]) >>> # convert the indexing array to a boolean array >>> B = NP.array(B, dtype=bool) >>> # index A against B--indexing array is placed after the ',' because >>> # you are selecting columns >>> res = A[:,B] >>> res array([[ 1, 3], [ 6, 8], [11, 13], [16, 18], [21, 23]]) 


The index-based slicing syntax in NumPy is elegant and simple. A few rules cover most use cases:

  • form [rows, columns]

  • specify all rows or all columns using the colon ":", for example, [:, 4] (extracts the entire fifth column)

+8
source

Not sure if this is the most efficient way (due to transposition), but it should be better than the for loop:

 AT[B == 1].T 
+2
source

I was interested in doing the same, but to cut the row and column using the logical values โ€‹โ€‹of the vector B, the solution was simple:

 res = A[:,B][B,:] 
0
source

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


All Articles