我试图为重新采样时间序列数据创建一个有效的函数。
假设:两组时间序列数据具有相同的开始时间和结束时间。(我是在一个单独的步骤中这样做的。)
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_arrayimport 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 有兴趣听取任何建议。
发布于 2019-06-21 10:02:21
该函数在我的机器上平均每次运行时都需要41\mu s。其中约四分之三(约32\mu s)用于downsampling_indices = np.linspace(...)。为round().astype(int)添加另一个D4,为实际采样添加1\mu s,加上一些呼叫开销,您就到了。
因此,如果您需要多次使用该函数,最好是预先计算或缓存/回忆录抽样索引。如果我正确地理解了您的实现,那么下采样索引计算基本上是独立于数据的,并且只取决于两个序列的长度,所以这可能是可行的。
例如,你可以
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)然后再做
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倍。
https://codereview.stackexchange.com/questions/222681
复制相似问题