xeeton
xeeton

Reputation: 432

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)

Upvotes: 3

Views: 657

Answers (1)

eumiro
eumiro

Reputation: 212915

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)]

Upvotes: 7

Related Questions