gpt4 book ai didi

python - 如何正确使用 numpy as_strided(来自 np.stride_tricks)?

转载 作者:行者123 更新时间:2023-11-28 20:35:06 35 4
gpt4 key购买 nike

我正在尝试使用 numpy.strided_tricks reshape 一个 numpy 数组。这是我正在关注的指南:https://stackoverflow.com/a/2487551/4909087

我的用例非常相似,不同之处在于我需要步幅为 3。

给定这个数组:

a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

我想得到:

array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8],
[7, 8, 9]])

这是我尝试过的:

import numpy as np

as_strided = np.lib.stride_tricks.as_strided
a = np.arange(1, 10)

as_strided(a, (len(a) - 2, 3), (3, 3))

array([[ 1, 2199023255552, 131072],
[ 2199023255552, 131072, 216172782113783808],
[ 131072, 216172782113783808, 12884901888],
[216172782113783808, 12884901888, 768],
[ 12884901888, 768, 1125899906842624],
[ 768, 1125899906842624, 67108864],
[ 1125899906842624, 67108864, 4]])

我很确定我已经按照这个例子做了 T,但显然不是。我哪里错了?

最佳答案

公认的答案(和讨论)很好,但为了不想运行自己的测试用例的读者的利益,我将尝试说明发生了什么:

In [374]: a = np.arange(1,10)
In [375]: as_strided = np.lib.stride_tricks.as_strided

In [376]: a.shape
Out[376]: (9,)
In [377]: a.strides
Out[377]: (4,)

对于连续的一维数组,strides 是元素的大小,这里是 4 个字节,一个 int32。要从一个元素转到下一个元素,它会向前移动 4 个字节。

OP 尝试了什么:

In [380]: as_strided(a, shape=(7,3), strides=(3,3))
Out[380]:
array([[ 1, 512, 196608],
[ 512, 196608, 67108864],
[ 196608, 67108864, 4],
[ 67108864, 4, 1280],
[ 4, 1280, 393216],
[ 1280, 393216, 117440512],
[ 393216, 117440512, 7]])

这步进了 3 个字节,跨越了 int32 边界,并且给出了大部分难以理解的数字。如果数据类型是 bytes 或 uint8,If 可能更有意义。

使用 a.strides*2(元组复制)或 (4,4) 我们得到了所需的数组:

In [381]: as_strided(a, shape=(7,3), strides=(4,4))
Out[381]:
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8],
[7, 8, 9]])

列和行都步进一个元素,导致移动窗口步进 1。我们还可以设置 shape=(3,7),3 个窗口 7 个元素长。

In [382]: _.strides
Out[382]: (4, 4)

将每个窗口的步长更改为 (8,4) 步骤 2 个元素。

In [383]: as_strided(a, shape=(7,3), strides=(8,4))
Out[383]:
array([[ 1, 2, 3],
[ 3, 4, 5],
[ 5, 6, 7],
[ 7, 8, 9],
[ 9, 25, -1316948568],
[-1316948568, 184787224, -1420192452],
[-1420192452, 0, 0]])

但是形状不对,向我们显示原始数据缓冲区末尾的字节数。这可能很危险(我们不知道这些字节是否属于其他某个对象或数组)。使用这种大小的数组,我们无法获得完整的 2 步窗口集。

现在每行的第 3 步元素 (3*4, 4):

In [384]: as_strided(a, shape=(3,3), strides=(12,4))
Out[384]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [385]: a.reshape(3,3).strides
Out[385]: (12, 4)

这与 3x3 reshape 的形状和步幅相同。

我们可以设置负步幅值和 0 值。事实上,沿具有正步幅的维度进行负步幅切片将给出负步幅,并且广播通过设置 0 步幅来工作:

In [399]: np.broadcast_to(a, (2,9))
Out[399]:
array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9]])
In [400]: _.strides
Out[400]: (0, 4)

In [401]: a.reshape(3,3)[::-1,:]
Out[401]:
array([[7, 8, 9],
[4, 5, 6],
[1, 2, 3]])
In [402]: _.strides
Out[402]: (-12, 4)

但是,负步幅需要调整原始数组的哪个元素是 View 的第一个元素,而 as_strided 没有参数。

关于python - 如何正确使用 numpy as_strided(来自 np.stride_tricks)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483579/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com