Numpy advanced selection not working

前端 未结 2 642
眼角桃花
眼角桃花 2020-12-11 10:29

Can someone please help me to understand why sometimes the advanced selection doesn\'t work and what I can do to get it to work (2nd case)?

>>> impo         


        
2条回答
  •  暖寄归人
    2020-12-11 11:16

    I think you misunderstood the advanced selection syntax for this case. I used your example, just made it smaller to be easier to see.

    import numpy as np
    b = np.random.rand(5, 4, 3, 2)
    
    # advanced selection works as expected
    print b[[0,1],[0,1]]   # http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
                           # this picks the two i,j=0 (a 3x2 matrix) and i=1,j=1, another 3x2 matrix
    
    # doesn't work - why?
    #print  b[[0,1],[0,1,2]]   # this doesnt' work because [0,1] and [0,1,2] have different lengths
    
    print b[[0,1,2],[0,1,2]]  # works
    

    Output:

    [[[ 0.27334558  0.90065184]
      [ 0.8624593   0.34324983]
      [ 0.19574819  0.2825373 ]]
    
     [[ 0.38660087  0.63941692]
      [ 0.81522421  0.16661912]
      [ 0.81518479  0.78655536]]]
    [[[ 0.27334558  0.90065184]
      [ 0.8624593   0.34324983]
      [ 0.19574819  0.2825373 ]]
    
     [[ 0.38660087  0.63941692]
      [ 0.81522421  0.16661912]
      [ 0.81518479  0.78655536]]
    
     [[ 0.65336551  0.1435357 ]
      [ 0.91380873  0.45225145]
      [ 0.57255923  0.7645396 ]]]
    

提交回复
热议问题