在单个呼叫中访问数组(“混合索引”)的多个维度?

发布于 2025-02-08 07:11:10 字数 1256 浏览 1 评论 0 原文

给定以下数组:示例 *行 *列

arr_3d = np.array([
    [
        [ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]
    ],
    [
        [10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]],
    [
        [19, 20, 21],
        [22, 23, 24],
        [25, 26, 27]
    ]
])

这有效,但是我可以在相同的 [_,_,_,_]] 调用中访问行和列?

>>> arr_3d[[1,2],:,:][:,:,[0,1]]

array([
    [
        [10, 11],
        [13, 14],
        [16, 17]
    ],
    [
        [19, 20],
        [22, 23],
        [25, 26]
    ]
])

这并不是预期的:


>>> arr_3d[[1,2],:,[0,1]]


array([
    [10, 13, 16],
    [20, 23, 26]
])

更新:看起来这是混合索引的已知挑战

Given the following array: samples * rows * columns

arr_3d = np.array([
    [
        [ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]
    ],
    [
        [10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]],
    [
        [19, 20, 21],
        [22, 23, 24],
        [25, 26, 27]
    ]
])

This works, but can I access rows and columns in the same [_,_,_] call?

>>> arr_3d[[1,2],:,:][:,:,[0,1]]

array([
    [
        [10, 11],
        [13, 14],
        [16, 17]
    ],
    [
        [19, 20],
        [22, 23],
        [25, 26]
    ]
])

This does not behave as expected:


>>> arr_3d[[1,2],:,[0,1]]


array([
    [10, 13, 16],
    [20, 23, 26]
])

UPDATE: looks like this is a known challenge of mixed indexing

https://numpy.org/neps/nep-0021-advanced-indexing.html#mixed-indexing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-02-15 07:11:10

您可以使用:

arr_3d[np.ix_([1,2], np.arange(arr_3d.shape[1]), [0,1])]

输出:

array([[[10, 11],
        [13, 14],
        [16, 17]],

       [[19, 20],
        [22, 23],
        [25, 26]]])

You can use:

arr_3d[np.ix_([1,2], np.arange(arr_3d.shape[1]), [0,1])]

output:

array([[[10, 11],
        [13, 14],
        [16, 17]],

       [[19, 20],
        [22, 23],
        [25, 26]]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文