How to implement linear interpolation in Python?
Last Updated :
10 May, 2022
Linear Interpolation is the technique of determining the values of the functions of any intermediate points when the values of two adjacent points are known. Linear interpolation is basically the estimation of an unknown value that falls within two known values. Linear Interpolation is used in various disciplines like statistical, economics, price determination, etc. It is used to fill the gaps in the statistical data for the sake of continuity of information.
By using the following formula we can Linearly interpolate the given data point
Linear Interpolation : y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}
Here (x1, y1) are the coordinates of the first data point. And (x2,y2) are coordinates of the second data point, where x is the point on which we perform interpolation and y is the interpolated value.
Example Problem:
Let's take an example for better understanding. We have the following data values where x denotes the number and y is the function of the square root of x. Our task is to find the square root of 5.5 (x).
x
| 1
| 2
| 3
| 4
| 5
| 6
|
---|
y ( f(x) = √x )
| 1
| 1.4142
| 1.7320
| 2
| 2.2360
| 2.4494
|
---|
We can use the Linear Interpolation method here.
1. Find the two adjacent (x1, y1) ,(x2,y2) from the x. i.e. (5,2.2360) and (6,2.4494).
Where x1 = 5, x2= 6, y1 = 2.2360, y2 = 2.4494, and we interpolate at point x = 5.5.
2. Using the formula y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}
3. After putting the values in the above equation.
y = 2.2360+(5.5-5)\frac{(2.4494-2.2360)}{(6 - 5)}
y = 2.3427
At x = 5.5 the value of Y will be 2.3427. So by using linear interpolation we can easily determine the value of a function between two intervals.
Approach 1:
Using the formula Linear Interpolation : y(x) = y1 + (x - x1) \frac{(y2 - y1) }{ (x2 - x1)}
Example: Suppose we have a dataset of the population of a city and the year.
X(Year)
| 2016
| 2017
| 2018
| 2019
| 2021
|
---|
Y(Population)
| 10001
| 12345
| 74851
| 12124
| 5700
|
---|
Here, X is the year and Y is the population in any city. Our task to find the population of the city in the year 2020.
We choose our (x1, y1) ,(x2,y2) as x1=2019 , y1=12124, x2=2021, y2=5700, x = 2020, y = ?
Here (x1, y1) and (x2, y2) are two adjacent points and x is the year for which we want to predict the value of the y population.
Python3
# Python3 code
# Implementing Linear interpolation
# Creating Function to calculate the
# linear interpolation
def interpolation(d, x):
output = d[0][1] + (x - d[0][0]) * ((d[1][1] - d[0][1])/(d[1][0] - d[0][0]))
return output
# Driver Code
data=[[2019, 12124],[2021, 5700]]
year_x=2020
# Finding the interpolation
print("Population on year {} is".format(year_x),
interpolation(data, year_x))
OutputPopulation on year 2020 is 8912.0
Approach 2:
Using scipy.interpolate.interp1d
Similarly, we can achieve linear interpolation using a scipy library function called interpolate.interp1d.
Syntax : scipy.interpolate.interp1d(x, y, kind='linear', axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
Sr. no.
| Parameters
| Description
|
---|
1.
| x
| A 1-D array of real values.
|
2.
| y
| A N-D array of real values.
|
3.
| kind
| i.e. kind of interpolation do you want it can be ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’, by defaults it is linear.
|
4.
| axis
| Specifies the axis of y along which we interpolate.
|
5.
| copy
| It holds boolean values if True, the class makes internal copies of x and y .
|
6.
| bounds_error
| It holds boolean values If True, a ValueError is raised when interpolation is attempted on a value outside the range of x.
|
Example:
Let's have a random dataset :
X = [1,2,3,4,5], Y = [11,2.2,3.5,-88,1], and we want to find the value of Y at point 2.5.
Python3
# Implementation of Linear Interpolation using Python3 code
# Importing library
from scipy.interpolate import interp1d
X = [1,2,3,4,5] # random x values
Y = [11,2.2,3.5,-88,1] # random y values
# test value
interpolate_x = 2.5
# Finding the interpolation
y_interp = interp1d(X, Y)
print("Value of Y at x = {} is".format(interpolate_x),
y_interp(interpolate_x))
Output
Value of y at x = 2.5 is 2.85
Similar Reads
Linear Interpolation in Excel - 10 Methods with Example
Linear interpolation in Excel is a powerful method for estimating unknown values between two data points within a dataset. Imagine working with financial models, engineering calculations, or scientific data where you need to predict or fill in missing values with precision. Linear interpolation allo
15+ min read
Interpolation in Python
Interpolation in Python refers to the process of estimating unknown values that fall between known values. This concept is commonly used in data analysis, mathematical modeling, and graphical representations. Python provides several ways to perform interpolation, including the use of libraries like
2 min read
Linear Interpolation in MATLAB
Interpolation is a numerical method of finding new data points by finding a pattern in a given set of discrete data points. There are various types and methods of interpolation in the field of Numerical Analysis such as linear interpolation, cubic interpolation, spline interpolation, etc. The key p
3 min read
Linear Interpolation Formula
Linear Interpolation Formula is a method that constructs the new data points from the given set of data points. Linear interpolation is used for fitting curves using linear polynomials. It finds the unknown values in the table. What is the Linear Interpolation Formula?The formula of linear interpola
4 min read
Python Scipy - ndimage.interpolation.geometric_transform() function
The given mapping function is used to find, for each point in the output, the corresponding coordinates in the input Syntax: scipy.ndimage.interpolation.geometric_transform(input, mapping,  order=3) Parameters  input : takes an array.mapping : accepts a tuple data structure similar to length of giv
1 min read
Interpolation in Machine Learning
In machine learning, interpolation refers to the process of estimating unknown values that fall between known data points. This can be useful in various scenarios, such as filling in missing values in a dataset or generating new data points to smooth out a curve. In this article, we are going to exp
7 min read
What is Bilinear Interpolation?
Bilinear interpolation is an extension of linear interpolation to a two-dimensional space. It approximates the value at a certain point within a grid by sampling the coordinates with values of four other grid points. The outstanding thing about the interpolation is that this is done in one direction
5 min read
Nearest-Neighbor Interpolation Algorithm in MATLAB
Nearest neighbor interpolation is a type of interpolation. This method simply determines the "nearest" neighboring pixel and assumes its intensity value, as opposed to calculating an average value using some weighting criteria or producing an intermediate value based on intricate rules. Interpolatio
3 min read
How To Implement Weighted Mean Square Error in Python?
In this article, we discussed the implementation of weighted mean square error using python. Mean squared error is a vital statistical concept, that is nowadays widely used in Machine learning and Deep learning algorithm. Mean squared error is basically a measure of the average squared difference be
2 min read
Interpolator in Android with Example
An interpolator is a function (in the mathematical sense) that outputs values "interpolated" between a range of values that are given to it as input. Interpolation is simply a method to generate new data points between two fixed data points. The exact values of these generated data points are determ
8 min read