对 numpy 数组中的每个第 n 个条目进行子采样 [英] subsampling every nth entry in a numpy array

查看:31
本文介绍了对 numpy 数组中的每个第 n 个条目进行子采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

基本上如果我有

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

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

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

解决方案

您可以使用 numpy 的切片,只需 start:stop:step.

<预><代码>>>>xs数组([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])>>>xs[1::4]数组([2, 2, 2])

这会创建原始数据的视图,因此是恒定时间.它还将反映对原始数组的更改并将整个原始数组保留在内存中:

<预><代码>>>>一种数组([1, 2, 3, 4, 5])>>>b = a[::2] # O(1),常数时间>>>b[:] = 0 # 修改视图改变原数组>>>a # 原始数组被修改数组([0, 2, 0, 4, 0])

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

<预><代码>>>>一种数组([1, 2, 3, 4, 5])>>>b = a[::2].copy() # 显式复制,O(n)>>>b[:] = 0 # 修改副本>>>#原件完好无损数组([1, 2, 3, 4, 5])

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

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.

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

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

解决方案

You can use numpy's slicing, simply 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 view of 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:

>>> 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.

这篇关于对 numpy 数组中的每个第 n 个条目进行子采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆