如何采样一个numpy数组并有效地对每个采样执行计算? [英] How to sample a numpy array and perform computation on each sample efficiently?

查看:242
本文介绍了如何采样一个numpy数组并有效地对每个采样执行计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个1d数组,我想用一个移动的窗口进行采样,并在窗口内将每个元素除以第一个元素.

Assume I have a 1d array, what I want is to sample with a moving window and within the window divide each element by the first element.

例如,如果我有[2, 5, 8, 9, 6]并且窗口大小为3,则结果将为

For example if I have [2, 5, 8, 9, 6] and a window size of 3, the result will be

[[1, 2.5, 4],
 [1, 1.6, 1.8],
 [1, 1.125, 0.75]].

我现在要做的基本上是一个for循环

What I'm doing now is basically a for loop

import numpy as np
arr = np.array([2., 5., 8., 9., 6.])
window_size = 3
for i in range(len(arr) - window_size + 1):
  result.append(arr[i : i + window_size] / arr[i])

当数组很大时,它会很慢,我想知道是否有更好的方法?我想无法解决O(n ^ 2)的复杂性,但是也许numpy有一些我不知道的优化.

When the array is large it is quite slow, I wonder whether there's better ways? I guess there is no way around the O(n^2) complexity, but perhaps numpy has some optimizations that I don't know of.

推荐答案

这是使用的矢量化方法broadcasting -

N = 3  # Window size
nrows = a.size-N+1
a2D = a[np.arange(nrows)[:,None] + np.arange(N)]
out = a2D/a[:nrows,None].astype(float)

我们还可以使用 NumPy strides 来提高效率提取滑动窗口,就像这样-

We can also use NumPy strides for a more efficient extraction of sliding windows, like so -

n = a.strides[0]
a2D = np.lib.stride_tricks.as_strided(a,shape=(nrows,N),strides=(n,n))

样品运行-

In [73]: a
Out[73]: array([4, 9, 3, 6, 5, 7, 2])

In [74]: N = 3
    ...: nrows = a.size-N+1
    ...: a2D = a[np.arange(nrows)[:,None] + np.arange(N)]
    ...: out = a2D/a[:nrows,None].astype(float)
    ...: 

In [75]: out
Out[75]: 
array([[ 1.        ,  2.25      ,  0.75      ],
       [ 1.        ,  0.33333333,  0.66666667],
       [ 1.        ,  2.        ,  1.66666667],
       [ 1.        ,  0.83333333,  1.16666667],
       [ 1.        ,  1.4       ,  0.4       ]])

这篇关于如何采样一个numpy数组并有效地对每个采样执行计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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