1

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?

2
  • Have you studied the scipy interpolate docs? Commented Jan 6, 2017 at 18:37
  • Before asking a question I have looked at that, and haven't found any direct solutions. Do you have a clue how to solve it? Commented Jan 6, 2017 at 20:14

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.