numpy.stride_tricks返回垃圾值 [英] numpy.stride_tricks returns junk values
问题描述
我正在尝试使用numpy.strided_tricks
重塑一个numpy数组.这是我要遵循的指南: https://stackoverflow.com/a/2487551/4909087
I'm trying to reshape a numpy array using numpy.strided_tricks
. This is the guide I'm following: https://stackoverflow.com/a/2487551/4909087
我的用例非常相似,不同之处在于我需要跨度为3.
My use case is very similar, with the difference being that I need strides of 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上,但显然没有.我要去哪里错了?
I was pretty sure I'd followed the example to a T, but evidently not. Where am I going wrong?
推荐答案
我不知道为什么您认为需要跨度为3.您需要跨度跨度以字节为单位的a
元素与下一个元素之间的距离.可以使用a.strides
:
I have no idea why you think you need strides of 3. You need strides the distance in bytes between one element of a
and the next, which you can get using a.strides
:
as_strided(a, (len(a) - 2, 3), a.strides*2)
这篇关于numpy.stride_tricks返回垃圾值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!