首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间序列的有效重采样

时间序列的有效重采样
EN

Code Review用户
提问于 2019-06-21 03:47:20
回答 1查看 492关注 0票数 2

我试图为重新采样时间序列数据创建一个有效的函数。

假设:两组时间序列数据具有相同的开始时间和结束时间。(我是在一个单独的步骤中这样做的。)

重采样函数(低效)

代码语言:javascript
复制
import numpy as np

def resample(desired_time_sequence, data_sequence):
    downsampling_indices = np.linspace(0, len(data_sequence)-1, len(desired_time_sequence)).round().astype(int)
    downsampled_array = [data_sequence[ind] for ind in downsampling_indices] 
    return  downsampled_array

速度测试

代码语言:javascript
复制
import timeit
def test_speed(): resample([1,2,3], [.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6])
print(timeit.timeit(test_speed, number=100000))
# 1.5003695999998854 

有兴趣听取任何建议。

EN

回答 1

Code Review用户

回答已采纳

发布于 2019-06-21 10:02:21

该函数在我的机器上平均每次运行时都需要41\mu s。其中约四分之三(约32\mu s)用于downsampling_indices = np.linspace(...)。为round().astype(int)添加另一个D4,为实际采样添加1\mu s,加上一些呼叫开销,您就到了。

因此,如果您需要多次使用该函数,最好是预先计算或缓存/回忆录抽样索引。如果我正确地理解了您的实现,那么下采样索引计算基本上是独立于数据的,并且只取决于两个序列的长度,所以这可能是可行的。

例如,你可以

代码语言:javascript
复制
import functools

...

@functools.lru_cache()
def compute_downsampling_indices_cached(n_samples, data_sequence_len):
    """Compute n_samples downsampling indices for data sequences of a given length"""
    return np.linspace(0, data_sequence_len-1, n_samples).round().astype(int)

然后再做

代码语言:javascript
复制
def resample_cache(n_samples, data_sequence):
    downsampling_indices = compute_downsampling_indices_cached(n_samples, len(data_sequence))
    return [data_sequence[ind] for ind in downsampling_indices]

请注意,我将desired_time_sequence替换为n_samples,然后必须将其设置为len(desired_time_sequence),因为您并不关心desired_time_sequence中的实际值。

还可以从NumPy索引中获益,并将return np.array(data_sequence)[downsampling_indices]用于更大的输入。你得亲自检查一下。

在我的机器上,resample_cache(...)采用了1.7\mu s,它的速度大约提高了20倍。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/222681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档