Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Given the following x, y pairs, what's the most Pythonic way to do this?

(1, 23), (2, 42), (3.5, 89), (5, 73), (7, 54), (8, 41), (8.5, 37), (9, 23)
like image 603
xeeton Avatar asked Jul 09 '26 18:07

xeeton


1 Answers

Use 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)]
like image 135
eumiro Avatar answered Jul 12 '26 10:07

eumiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!