Numpy interp – One-dimensional linear interpolation for monotonically increasing sample points

In this article, we will understand and implement `numpy.interp()` which is a NumPy function. When given discrete data points (xp, fp), this function returns the one-dimensional piecewise linear interpolant to that function, which is evaluated at x.

What is Numpy interp?

`numpy.interp()` calculates linear interpolant to a function with given data points, the data points given (xp values)are first sorted in increasing order after that the best linear interpolant is found and returned.

Also read: How to Use Numpy Positive in Python?

How to calculate one-dimensional linear interpolation?

Suppose we have points (1,4), (3,12), (4,16), and (10,40) clearly lying on a straight line and we have to calculate the value of y at x = 16 after linear interpolation.

• Plot all the given points on the graph.
• Check if all the points lie on the same line.
• Find the equation of the line using any two points.
• Put x = 16 in the given equation and we will get y = 64(Refer to the plot above)

Syntax of Numpy interp

```numpy.interp(x, xp, fp, left = None, right = None, period = None)
```

Return value:

float or complex (corresponding to fp) or ndarray(The interpolated values, the same shape as x)

Raises ValueError :

• If xp and fp have different length
• If xp or fp are not 1-D sequences
• If period == 0

Also read: NumPy conjugate()- Return the complex conjugate, element-wise.

Implementation of Numpy interp

Importing numpy and declaring our x-axis and y-axis data points.

```import numpy as np

xp = [1,3,4,10,16]
fp = [4,12,16,40,64]
```

Example 1: x as a float number

```x = 5.5
print(np.interp(x,xp,fp))
```

On providing x as a float, xp, and fp to the function `numpy.interp()` , we will get linear interpolation for monotonically increasing data points provided(xp and fp)

Output:

```22.0
```

Example 2: x as an array

```x = [1, 4.7, 27, 34.7]
print(np.interp(x,xp,fp))
```

We can make x as an array of numbers also. The numpy.interp() function will return an array of linearly interpolated values on the y-axis for all the elements in the array x.

Output:

```[ 4.  18.8 64.  64. ]
```

Summary

The numpy.interp() function in NumPy is a powerful tool for finding linear interpolants for discrete data points. The function takes x, xp, and fp as required inputs, and left, right, and period as optional inputs. The output is a one-dimensional piecewise linear interpolant evaluated at x, which can be a float or complex value, or an array of values.

The input data points (xp) must be sorted in increasing order, or they will be sorted internally. The function raises a ValueError if the input data points have different lengths, if they are not 1-D sequences, or if the period is equal to zero. The article provides a comprehensive overview of the numpy.interp() function and provides examples of its usage.

In addition, it is important to note that linear interpolation is a common method for estimating values between two known points. The method is useful in various fields, including science, engineering, and finance. NumPy’s implementation of linear interpolation makes it easy to perform this calculation, especially when working with large arrays of data.