sách gpt4 ai đã đi

python - 多维 numpy 数组中的数组索引

In lại 作者:行者123 更新时间:2023-12-01 05:43:00 30 4
mua khóa gpt4 Nike

我是 numpy 新手,试图理解 đây 中的以下示例。我无法理解

的输出
>>> 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]]])

1 Câu trả lời

您正在创建一个 3D 数组,其中第一个 2D 数组(带有 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

关于python - 多维 numpy 数组中的数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005810/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com