0

I would like to interpolate array-valued data, i.e., the x-values are one-dimensional, but the y-values are (NumPy) arrays. The following doesn't work:

import numpy as np

xp = np.array([0.0, 1.0, 2.0])
yp = np.array(
    [
        [-1.1, 2.4, 5.1, 5.6],
        [7.1, -2.1, 9.1, 31.0],
        [1.1, 13.4, -5.2, 5.6],
    ]
)

np.interp(0.4, xp, yp)
ValueError: object too deep for desired array

Any hints?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 2
    Does this answer your question? [fastest way to use numpy.interp on a 2-D array](https://stackoverflow.com/questions/43772218/fastest-way-to-use-numpy-interp-on-a-2-d-array) – Daniel F Jul 27 '23 at 13:30

3 Answers3

0
def interp(x, xp, fp, left=None, right=None, period=None):
    """
    One-dimensional linear interpolation for monotonically increasing sample points.

    Returns the one-dimensional piecewise linear interpolant to a function
    with given discrete data points (`xp`, `fp`), evaluated at `x`.

    Parameters
    ----------
    x : array_like
        The x-coordinates at which to evaluate the interpolated values.

    xp : 1-D sequence of floats
        The x-coordinates of the data points, must be increasing if argument
        `period` is not specified. Otherwise, `xp` is internally sorted after
        normalizing the periodic boundaries with ``xp = xp % period``.

    fp : 1-D sequence of float or complex
        The y-coordinates of the data points, same length as `xp`.

The fp (yp) parameter should be a 1-D array and it should have the same length as xp. For example, calling interp on every sublist of transposed yp will work

for lst in yp.T:
    print(np.interp(0.4, xp, lst))

Output:

2.1799999999999997
0.5999999999999999
6.699999999999999
15.76
Guy
  • 46,488
  • 10
  • 44
  • 88
0

according to numpy documentation

numpy.interp

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

Parameters: xarray_like The x-coordinates at which to evaluate the interpolated values.

xp1-D sequence of floats The x-coordinates of the data points, must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.

fp1-D sequence of float or complex The y-coordinates of the data points, same length as xp.

But in your code yp, is 2D array.

You can do it by doing the interpolation for each set of your array using a for loop.

yp = np.array([np.interp(x, xp, yp[0:, i]) for i in range(yp.shape[1])])
print(yp)
[ 2.18  0.6   6.7  15.76]
Harith 75
  • 19
  • 5
0

As others have mentioned, np.interp takes a 1D yp array but you are providing a 2D array. I'll just throw out another option using np.apply_along_axis.

res = np.apply_along_axis(lambda _yp: np.interp(0.4, xp, _yp), 0, yp)
print(res)  # [ 2.18  0.6   6.7  15.76]

And to comment on the list comprehension approaches, you can use the transpose of the array to just loop through the data rather than using range and indexing, which will be very slightly faster as you increase the amount of data.

res = [np.interp(0.4, xp, _yp) for _yp in yp.T]
jared
  • 4,165
  • 1
  • 8
  • 31