Python 对 numpy 数组中的每个第 n 个条目进行子采样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25876640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:46:34  来源:igfitidea点击:

subsampling every nth entry in a numpy array

pythonarraysnumpy

提问by Rich Williams

I am a beginner with numpy, and I am trying to extract some data from a long numpy array. What I need to do is start from a defined position in my array, and then subsample every nth data point from that position, until the end of my array.

我是 numpy 的初学者,我正在尝试从长 numpy 数组中提取一些数据。我需要做的是从数组中定义的位置开始,然后从该位置对每个第 n 个数据点进行子采样,直到数组结束。

basically if I had

基本上如果我有

a = [1,2,3,4,1,2,3,4,1,2,3,4....] 

I want to subsample this to start at a[1]and then sample every fourth point from there, to produce something like

我想对它进行二次采样a[1],然后从那里开始每四个点采样一次,以产生类似的东西

b = [2,2,2.....]

回答by behzad.nouri

You can use numpy's slicing, simply start:stop:step.

您可以简单地使用numpy 的切片start:stop:step

>>> xs
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
>>> xs[1::4]
array([2, 2, 2])

This creates a viewof the the original data, so it's constant time. It'll also reflect changes to the original array and keep the whole original array in memory:

这将创建原始数据的视图,因此它是常数 time。它还将反映对原始数组的更改并将整个原始数组保留在内存中:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2]         # O(1), constant time
>>> b[:] = 0           # modifying the view changes original array
>>> a                  # original array is modified
array([0, 2, 0, 4, 0])

so if either of the above things are a problem, you can make a copy explicitly:

因此,如果上述任何一项都有问题,您可以明确复制:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2].copy()  # explicit copy, O(n)
>>> b[:] = 0           # modifying the copy
>>> a                  # original is intact
array([1, 2, 3, 4, 5])

This isn't constant time, but the result isn't tied to the original array. The copy also contiguous in memory, which can make some operations on it faster.

这不是常数时间,但结果与原始数组无关。副本在内存中也是连续的,这可以使对其进行一些操作更快。