0

I do not understand why the two cases below give different results. I want to reorder over axis 1 (0-based counting), and only take the elements with index 0 on axis 3.

(Changing (1,2,0) to [1,2,0] or np.array((1,2,0)) makes no difference.)

>> w # (3,3,5,5) input
array([[[[206, 172,   9,   2,  43],
         [232, 101,  85, 251, 150],
         [247,  99,   6,  88, 100],
         [250, 124, 244,   2,  73],
         [ 49,  23, 227,   3, 125]],

        [[110, 162, 246, 123, 110],
         [ 67, 197,  87, 230,  29],
         [110,  51,  79, 136, 155],
         [ 86,  62, 121,  18, 113],
         [ 59, 197, 149, 112, 172]],

        [[198, 231, 137,   2, 238],
         [ 47,  97,  94, 102, 206],
         [  1, 232, 189, 173,  75],
         [207, 171,  40,  23, 102],
         [243, 232,  13, 109,  26]]],


       [[[114, 218,  50, 173,  95],
         [ 92,  29, 170, 247,  42],
         [ 75, 251,  65, 246, 231],
         [151, 210,  79,  27, 175],
         [105,  55, 224,  79,   4]],

        [[172, 230,   0, 115,  38],
         [ 10, 165, 169, 230, 163],
         [159, 142,  15, 134, 124],
         [ 91, 161,  19, 103, 214],
         [102, 168, 181,  20,  75]],

        [[ 78,  65, 245,  29, 155],
         [ 40, 108, 198, 180, 231],
         [202,  47,  60, 156, 183],
         [210,  74,  18, 113, 148],
         [231, 177, 240,  15, 200]]],


       [[[ 28,  40, 169, 249, 218],
         [ 96, 205,   3,  38, 106],
         [229, 129,  78, 113,  13],
         [243, 170, 186,  35,  74],
         [111, 224, 132, 184,  23]],

        [[ 21, 181, 126,   5,  42],
         [135,  93, 133, 166, 111],
         [ 85,  85,  31, 220, 124],
         [ 61,   5,  94, 216, 135],
         [  4, 225, 204, 128, 115]],

        [[ 63,  23, 122, 146, 140],
         [245, 139,  76, 173,  12],
         [ 31, 195, 239, 188, 254],
         [253, 231, 187,  22,  15],
         [ 59,  40,  61, 185, 216]]]], dtype=uint16)

>> w[:,(1,2,0),:,0:1] # case 1 without squeeze
array([[[[110],
         [ 67],
         [110],
         [ 86],
         [ 59]],

        [[198],
         [ 47],
         [  1],
         [207],
         [243]],

        [[206],
         [232],
         [247],
         [250],
         [ 49]]],


       [[[172],
         [ 10],
         [159],
         [ 91],
         [102]],

        [[ 78],
         [ 40],
         [202],
         [210],
         [231]],

        [[114],
         [ 92],
         [ 75],
         [151],
         [105]]],


       [[[ 21],
         [135],
         [ 85],
         [ 61],
         [  4]],

        [[ 63],
         [245],
         [ 31],
         [253],
         [ 59]],

        [[ 28],
         [ 96],
         [229],
         [243],
         [111]]]], dtype=uint16)

>> w[:,(1,2,0),:,0:1].squeeze() # case 1 with squeeze, for readability and comparability
array([[[110,  67, 110,  86,  59],
        [198,  47,   1, 207, 243],
        [206, 232, 247, 250,  49]],

       [[172,  10, 159,  91, 102],
        [ 78,  40, 202, 210, 231],
        [114,  92,  75, 151, 105]],

       [[ 21, 135,  85,  61,   4],
        [ 63, 245,  31, 253,  59],
        [ 28,  96, 229, 243, 111]]], dtype=uint16)

>> w[:,(1,2,0),:,0] # case 2: 0 index instead of length-1 slice 0:1
array([[[110,  67, 110,  86,  59],
        [172,  10, 159,  91, 102],
        [ 21, 135,  85,  61,   4]],

       [[198,  47,   1, 207, 243],
        [ 78,  40, 202, 210, 231],
        [ 63, 245,  31, 253,  59]],

       [[206, 232, 247, 250,  49],
        [114,  92,  75, 151, 105],
        [ 28,  96, 229, 243, 111]]], dtype=uint16)

Koen G.
  • 738
  • 5
  • 10
  • See recent https://stackoverflow.com/q/55829631/901925 – hpaulj Apr 25 '19 at 15:07
  • `w[:,(1,2,0),:,0]` has a slice in middle. It produces the same numbers as in the other cases, but 1st and 2nd axes have been switched. The (1,2,0) axis has been put first, and the rest after. Links in the previous answer explain this in more detail. – hpaulj Apr 25 '19 at 16:50
  • Thanks for the helpful references. If I understand correctly: when an advanced index is present, integers also become advanced indices, which are then broadcasted to match shape. If then these advanced indices are not next to each other, there is ambiguity in where to put the resulting axes of the advanced indexing. Hence they are put in front. If this is correct, I understand what's happening, although, I still do not understand the reason why an integer index suddenly becomes an advanced index in the presence of another advanced index. Why not interpret it as ```w[:,:,:,0][:,(1,2,0),:]```? – Koen G. Apr 26 '19 at 08:10
  • You need to look up a relevant github issue. It's been several years since I dug into this. – hpaulj Apr 26 '19 at 12:09
  • Thanks for the help! (For future reference:) I found the following related issues: [12227](https://github.com/numpy/numpy/issues/12227), [5871](https://github.com/numpy/numpy/issues/5871) and [12053](https://github.com/numpy/numpy/issues/12053). [NEP 21](http://www.numpy.org/neps/nep-0021-advanced-indexing.html) is also relevant. – Koen G. Apr 26 '19 at 12:37

0 Answers0