簡體   English   中英

多維numpy數組中的數組索引

[英]Array Indexing in multi dimensional numpy array

我是numpy的新手,並試圖從這里理解以下示例。 我在理解...的輸出時遇到了麻煩

>>> palette[image] 

當索引數組a為多維時,單個索引數組指a的第一維。 下面的示例通過使用調色板將標簽圖像轉換為彩色圖像來顯示此行為。

>>> palette = array( [ [0,0,0],                # black
...                    [255,0,0],              # red
...                    [0,255,0],              # green
...                    [0,0,255],              # blue
...                    [255,255,255] ] )       # white
>>> image = array( [ [ 0, 1, 2, 0 ],           # each value corresponds to a color in the palette
...                  [ 0, 3, 4, 0 ]  ] )
>>> palette[image]                            # the (2,4,3) color image
array([[[  0,   0,   0],
        [255,   0,   0],
        [  0, 255,   0],
        [  0,   0,   0]],
       [[  0,   0,   0],
        [  0,   0, 255],
        [255, 255, 255],
        [  0,   0,   0]]])

要創建一個三維陣列,其中第一2D陣列(withing 3D陣列)是通過提取從行給定的palette通過的指數給出image[0]和所述第二陣列是通過提取從行給定的palette通過的指數給出image[1]

>>> palette = array( [ [0,0,0],                # black
...                    [255,0,0],              # red
...                    [0,255,0],              # green
...                    [0,0,255],              # blue
...                    [255,255,255] ] )       # white
>>> image = array( [ [ 0, 1, 2, 0 ],           # each value corresponds to a color in the palette
...                  [ 0, 3, 4, 0 ]  ] )
>>> palette[image]                            # the (2,4,3) color image
array([[[  0,   0,   0], # row at index 0 of palete
        [255,   0,   0], # index 1
        [  0, 255,   0], # index 2
        [  0,   0,   0]], # index 0
       [[  0,   0,   0], # index 0
        [  0,   0, 255], # index 3
        [255, 255, 255], # index 4
        [  0,   0,   0]]]) # index 0

這可以幫助您了解:

array([[[  0,   0,   0],   # palette[0]
        [255,   0,   0],   # palette[1]
        [  0, 255,   0],   # palette[2]
        [  0,   0,   0]],  # palette[0]

       [[  0,   0,   0],   # palette[0]
        [  0,   0, 255],   # palette[3]
        [255, 255, 255],   # palette[4]
        [  0,   0,   0]]]) # palette[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM