[英]Array Indexing in multi dimensional numpy array
I'm new to numpy and trying to understand the following example from here . 我是numpy的新手,并试图从这里理解以下示例。 I'm having trouble understanding the output of
我在理解...的输出时遇到了麻烦
>>> palette[image]
When the indexed array a is multidimensional, a single array of indices refers to the first dimension of a. 当索引数组a为多维时,单个索引数组指a的第一维。 The following example shows this behavior by converting an image of labels into a color image using a palette.
下面的示例通过使用调色板将标签图像转换为彩色图像来显示此行为。
>>> 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]]])
You are creating a 3D array, where first 2D array (withing 3D array) is given by extracting rows from palette
as given by indices of image[0]
and the second array is given by extracting rows from palette
as given by indices of image[1]
. 要创建一个三维阵列,其中第一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
This might help you understand: 这可以帮助您了解:
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.