为什么带有1)列表作为索引和2)索引0:1 vs 2)的numpy数组给出不同的结果

时间:2019-04-25 15:03:45

标签: python numpy indexing

我不明白为什么以下两种情况给出不同的结果。 我想对轴1重新排序(基于0的计数),并且只对轴3上索引为0的元素进行排序。

(将(1,2,0)更改为[1,2,0]np.array((1,2,0))没什么。)

>> 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)

0 个答案:

没有答案