## Introduction to NumPy interpolate

Interpolation creates new prediction data points from a distinct set of data points. which is known, and There are many types of interpolation such as piece-wise constant, polynomial, linear, basis spline, and cubic spline. In interpolation, the data offer the shape of the approximate function, with piece-wise or higher-order polynomial equations to accurately counterpart the data points at those given distinct positions. In this topic, we are going to learn about NumPy interpolate.

### Syntax and Parameters

The interpolation in numpy is achieved by using the function numpy.interp

The basic syntax of the numpy interpolates function is,

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

The above-mentioned syntax is for one-dimensional linear interpolation.

It will return the one-dimensional piecewise linear interpolant values to the function given with distinct data points xp and fp, which is evaluated at x.

**Parameters**

**x**: it is a one-dimensional array for which we need to do interpolation. It contains the x coordinates of the interpolated data points.**xp**: It refers to the one-dimensional sequence of float data type, which contains the x coordinates of the data points. The values should be increasing.**fp**: It refers to the one-dimensional sequence of float data type, which contains the x coordinates of the data points. The length of fp and xp should be the same, i.e. the number of x coordinates should be the same as y coordinates.**left**: Optional parameter of the float data type. It refers to the value to return for the condition x < xp[0], The default value is fp[0].**right**: Optional parameter of the float data type. It refers to the value to return for the condition x > xp[-1], The default value is fp[-1].**period**: Optional parameter of the float data type. A period is specified for the x-coordinates. This specification permits the appropriate interpolation of angular x-coordinates. The parameters right and left will be discounted if the period is specified.**y**: It is the return value of the interpolated function. The data type will be either float or complex depending on the fp data points or n-dimensional array.**ValueError:**Error will be thrown If xp and fp are of different length or If xp or fp is not one-dimensional sequences or If period == 0

### Examples of NumPy interpolate

Given below are the examples of NumPy interpolate:

#### Example #1

Let us discuss a basic example for understanding how the numpy interpolate function works.

**Code:**

```
# simple program in python to explain numpy.interp() function
# importing numpy python library as np
import numpy as np
x = 5
xp = [2, 4, 6, 8, 10]
print("x coordinates =", xp)
fp = [1, 2, 3, 4, 5]
print("y coordinates =", fp)
interpolated_value = np.interp(x, xp, fp)
print ("The interpolated value for x = 5 is ", interpolated_value)
```

**Output:**

Here in the above example, we have declared two one-dimensional arrays, xp and fp, which contains the x coordinates and y coordinates of discrete data points. Then we have assigned a value of 5 to the one-dimensional array x, for which we need to find the y coordinate. In the next step, we have printed the two arrays. In the next step, interpolation is done using the interp function. In the last step, we have printed the interpolated value. The interpolated value returned is 2.5 for the value x = 5.

**Important note**

The x coordinates must be continuously increasing. But this is not enforced explicitly. If the x coordinate values are not increasing, then the results of interpolated values are meaningless. To check whether the sequence is increasing continuously, follow the step shown below.

`np.all(np.diff(xp) > 0)`

#### Example #2

**Code:**

```
# program to check whether x coordinates are strictly increasing.
import numpy as np
xp = [2, 4, 6, 8, 10]
fp = [5, 4, 3, 2, 1]
#check condition
print (np.all(np.diff(xp) > 0))
print (np.all(np.diff(fp) > 0))
```

**Output:**

In the above example, you can see that the x coordinates are continuously increasing, and the y coordinates are continuously decreasing. To check, we run the above program. In the first condition, we check the x coordinates, and the result is true. In the second condition, we check the y coordinates, and the result is false.

#### Example #3

This example will discuss how we can use the decimal argument to understand the interpolation concept better.

**Code:**

```
# simple program in python to explain interpolation
# importing numpy python library as np
import numpy as np
x = [1.5, 2.75, 4.25, 7.35, 9.13]
xp = [2, 4, 6, 8, 10]
print("x coordinates =", xp)
fp = [1, 2, 3, 4, 5]
print("y coordinates =", fp)
interpolated_value = np.interp(x, xp, fp)
print ("The interpolated values are ", interpolated_value)
```

**Output:**

Here in the above example, we have declared two one-dimensional arrays, xp and fp, which contains the x coordinates and y coordinates of discrete data points. Then we have assigned five values to the one-dimensional array x, for which we need to find the y coordinates. In the next step, we have printed the two arrays. In the next step, interpolation is done using the interp function. Finally, in the last step, we have printed the interpolated values.

#### Example #4

Let’s try to find the interpolant values for the sine function.

**Code:**

```
# code to plot interpolant values for the sine function.
import numpy as np
x = np.linspace(0, 2*np.pi, 10)y = np.sin(x)x_values = np.linspace(0, 2*np.pi, 50)yinterp = np.interp(x_values, x, y)import matplotlib.pyplot as grphgrph.plot(x, y, 'o')grph.plot(x_values, yinterp, '-x')grph.show()
```

**Output:**

In the above example, we are declaring x and y coordinates as a sine function. Then we are finding the interpolant values using the interp function. Then we are importing the matplotlib.pyplot library to plot the curve. In the last step, we are plotting the graph and displaying the output.

### Conclusion

This article discussed the Numpy Interp function in detail using various examples to get a clear understanding of the numpy interp function and its uses. We have also discussed how to find interpolant values for the sine function as well. Similarly, we can use the interp function to find unknown values for various functions. We hope this article helps.

### Recommended Articles

This is a guide to NumPy interpolate. Here we discuss the various examples to understand the numpy interp function and its uses. You may also have a look at the following articles to learn more –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses