Index a 2D Numpy array with 2 lists of indices

后端 未结 4 569
感动是毒
感动是毒 2020-11-22 12:27

I\'ve got a strange situation.

I have a 2D Numpy array, x:

x = np.random.random_integers(0,5,(20,8))

And I have 2 indexers--one w

4条回答
  •  眼角桃花
    2020-11-22 13:07

    I think you are trying to do one of the following (equlvalent) operations:

    x_does_work = x[row_indices,:][:,col_indices]
    x_does_work = x[:,col_indices][row_indices,:]
    

    This will actually create a subset of x with only the selected rows, then select the columns from that, or vice versa in the second case. The first case can be thought of as

    x_does_work = (x[row_indices,:])[:,col_indices]
    

提交回复
热议问题