Index a 2D Numpy array with 2 lists of indices

后端 未结 4 570
感动是毒
感动是毒 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:05

    What about:

    x[row_indices][:,col_indices]
    

    For example,

    x = np.random.random_integers(0,5,(5,5))
    ## array([[4, 3, 2, 5, 0],
    ##        [0, 3, 1, 4, 2],
    ##        [4, 2, 0, 0, 3],
    ##        [4, 5, 5, 5, 0],
    ##        [1, 1, 5, 0, 2]])
    
    row_indices = [4,2]
    col_indices = [1,2]
    x[row_indices][:,col_indices]
    ## array([[1, 5],
    ##        [2, 0]])
    

提交回复
热议问题