DumbQ
DumbQ

Reputation: 75

interpolation between arrays in python

What is the easiest and fastest way to interpolate between two arrays to get new array.

For example, I have 3 arrays:

x = np.array([0,1,2,3,4,5])
y = np.array([5,4,3,2,1,0])
z = np.array([0,5])

x,y corresponds to data-points and z is an argument. So at z=0 x array is valid, and at z=5 y array valid. But I need to get new array for z=1. So it could be easily solved by:

a = (y-x)/(z[1]-z[0])*1+x

Problem is that data is not linearly dependent and there are more than 2 arrays with data. Maybe it is possible to use somehow spline interpolation?

Upvotes: 1

Views: 2528

Answers (1)

J. P. Petersen
J. P. Petersen

Reputation: 5031

This is a univariate to multivariate regression problem. Scipy supports univariate to univariate regression, and multivariate to univariate regression. But you can instead iterate over the outputs, so this is not such a big problem. Below is an example of how it can be done. I've changed the variable names a bit and added a new point:

import numpy as np
from scipy.interpolate import interp1d

X = np.array([0, 5, 10])
Y = np.array([[0, 1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1, 0],
              [8, 6, 5, 1, -4, -5]])

XX = np.array([0, 1, 5])  # Find YY for these

YY = np.zeros((len(XX), Y.shape[1]))
for i in range(Y.shape[1]):
    f = interp1d(X, Y[:, i])
    for j in range(len(XX)):
        YY[j, i] = f(XX[j])

So YY are the result for XX. Hope it helps.

Upvotes: 3

Related Questions