用另一个数组切片多维数组

发布于 2025-01-19 08:27:58 字数 972 浏览 2 评论 0原文

编辑了一个更清晰的示例,并包含了解决方案

我想对任意维数组进行切片,其中我固定前n维并保留其余维。此外,我希望能够将 n 固定尺寸存储在变量中。例如,

Q = np.arange(24).reshape(2, 3, 4) # array to be sliced
# array([[[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]],
#       [[12, 13, 14, 15],
#        [16, 17, 18, 19],
#        [20, 21, 22, 23]]])

Q[0, 1, ...]  # this is what I want manually
# array([4, 5, 6, 7])

# but programmatically:
s = np.array([0, 1])
Q[s, ...]  # this doesn't do what I want: it uses both s[0] and s[1] along the 0th dimension of Q
# array([[[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]],
#       [[12, 13, 14, 15],
#        [16, 17, 18, 19],
#        [20, 21, 22, 23]]])

np.take(Q, s)  # this unravels the indices and takes the s[i]th elements of Q
# array([0, 1])

Q[tuple(s)]  # this works! Thank you kwin
# array([4, 5, 6, 7])

有没有一种干净的方法可以做到这一点?

edited with a clearer example, and included solution

I'd like to slice an arbitrary dimensional array, where I pin the first n dimensions and keep the remaining dimensions. In addition, I'd like to be able to store the n pinning dimensions in a variable. For example

Q = np.arange(24).reshape(2, 3, 4) # array to be sliced
# array([[[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]],
#       [[12, 13, 14, 15],
#        [16, 17, 18, 19],
#        [20, 21, 22, 23]]])

Q[0, 1, ...]  # this is what I want manually
# array([4, 5, 6, 7])

# but programmatically:
s = np.array([0, 1])
Q[s, ...]  # this doesn't do what I want: it uses both s[0] and s[1] along the 0th dimension of Q
# array([[[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]],
#       [[12, 13, 14, 15],
#        [16, 17, 18, 19],
#        [20, 21, 22, 23]]])

np.take(Q, s)  # this unravels the indices and takes the s[i]th elements of Q
# array([0, 1])

Q[tuple(s)]  # this works! Thank you kwin
# array([4, 5, 6, 7])

Is there a clean way to do this?

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

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

发布评论

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

评论(2

内心旳酸楚 2025-01-26 08:27:58

您可以做到这一点:

Q[tuple(s)]

或以下:

np.take(Q, s)

这两个收益率数组([0.58383736,0.80486868])

恐怕我对s的元组版本与用s本身的索引的工作方式不同。我直观地尝试的另一件事是q [*s],但这是语法错误。

You could do this:

Q[tuple(s)]

Or this:

np.take(Q, s)

Both of these yield array([0.58383736, 0.80486868]).

I'm afraid I don't have a great intuition for exactly why the tuple version of s works differently from indexing with s itself. The other thing I intuitively tried is Q[*s] but that's a syntax error.

醉梦枕江山 2025-01-26 08:27:58

我不确定你想要什么输出,但你可以做几件事。

如果您希望输出如下所示:

array([[[0.46988733, 0.19062458],
        [0.69307707, 0.80242129],
        [0.36212295, 0.2927196 ],
        [0.34043998, 0.87408959],
        [0.5096636 , 0.37797475]],

       [[0.98322049, 0.00572271],
        [0.06374176, 0.98195354],
        [0.63195656, 0.44767722],
        [0.61140211, 0.58889763],
        [0.18344186, 0.9587247 ]]])

Q[list(s)] 应该可以。 np.array([Q[i] for i in s]) 也有效。

如果您希望输出如下所示:

array([0.58383736, 0.80486868])

那么正如 @kwinkunks 提到的,您可以使用 Q[tuple(s)] 或 np.take(Q, s)

I am not sure what output you want but there are several things you can do.

If you want the output to be like this:

array([[[0.46988733, 0.19062458],
        [0.69307707, 0.80242129],
        [0.36212295, 0.2927196 ],
        [0.34043998, 0.87408959],
        [0.5096636 , 0.37797475]],

       [[0.98322049, 0.00572271],
        [0.06374176, 0.98195354],
        [0.63195656, 0.44767722],
        [0.61140211, 0.58889763],
        [0.18344186, 0.9587247 ]]])

Q[list(s)] should work. np.array([Q[i] for i in s]) also works.

If you want the output to be like this:

array([0.58383736, 0.80486868])

Then as @kwinkunks mentioned you could use Q[tuple(s)] or np.take(Q, s)

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