首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何有效地映射python中时间序列之间的数据

如何有效地映射python中时间序列之间的数据
EN

Stack Overflow用户
提问于 2019-06-21 00:01:08
回答 1查看 752关注 0票数 0

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

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

重采样功能(低效)

代码语言: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

Stack Overflow用户

回答已采纳

发布于 2019-06-21 12:15:12

顶替

代码语言:javascript
复制
downsampled_array = [data_sequence[ind] for ind in downsampling_indices]

使用

代码语言:javascript
复制
downsampled_array = data_sequence[downsampling_indices]

对我的测试数据提供了7倍的加速。

用于测量加速的代码:

代码语言:javascript
复制
import timeit

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

resample(output_len, data_sequence)
"""

f2 = """
def resample_fast(output_len, data_sequence):
    downsampling_indices = np.linspace(0, len(data_sequence)-1, output_len).round().astype(int)
    downsampled_array = data_sequence[downsampling_indices]
    return downsampled_array

resample_fast(output_len, data_sequence)
"""


setup="""
import numpy as np
data_sequence = np.random.randn(10000)
output_len = 752
"""

print(timeit.timeit(f1, setup, number=1000))
print(timeit.timeit(f2, setup, number=1000))

# prints:
# 0.30194038699846715
# 0.041797632933594286
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56695274

复制
相关文章

相似问题

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