[英]Interpolating periodic data in Python
I have a module that collects stats in an inconsistent time interval. 我有一个模块,用于在不一致的时间间隔内收集统计信息。 Unfortunately, to use it nicely in a graph, I need the x
values interpolated to a consistent interval. 不幸的是,为了在图中很好地使用它,我需要将x
值插值到一致的间隔。
Given the following x
, y
pairs, what's the most Pythonic way to do this? 鉴于以下x
, y
对,最恐怖的方法是什么?
(1, 23), (2, 42), (3.5, 89), (5, 73), (7, 54), (8, 41), (8.5, 37), (9, 23)
Use numpy.interp : 使用numpy.interp :
import numpy as np
a = np.array([(1, 23), (2, 42), (3.5, 89), (5, 73), (7, 54), (8, 41), (8.5, 37), (9, 23)])
x = np.arange(1, 10) # target x values
b = zip(x, np.interp(x, a[:,0], a[:,1]))
# b == [(1, 23.0),
# (2, 42.0),
# (3, 73.333333333333329),
# (4, 83.666666666666671),
# (5, 73.0),
# (6, 63.5),
# (7, 54.0),
# (8, 41.0),
# (9, 23.0)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.