Bilinear interpolation in Python
Linear interpolation is used for curve fitting with the help of linear polynomials.
Bilinear interpolation is an extension of linear interpolation and is used to interpolate functions of any two given variables with the help of linear interpolation.
Let's demonstrate different ways to implement bilinear interpolation in Python.
用户定义
Creating a function to implement bilinear interpolation
in Python
Here, we create a 用户定义
function associated with four points and utilize bilinear interpolation in Python.
def bilinterpol(a, b, pts):
i = sorted(pts)
(a1, b1, x11), (_a1, b2, x12), (a2, _b1, x21), (_a2, _b2, x22) = i
if a1 != _a1 or a2 != _a2 or b1 != _b1 or b2 != _b2:
print("The given points do not form a rectangle")
if not a1 <= a <= a2 or not b1 <= b <= b2:
print("The (a, b) coordinates are not within the rectangle")
Y = (
x11 * (a2 - a) * (b2 - b)
+ x21 * (a - a1) * (b2 - b)
+ x12 * (a2 - a) * (b - b1)
+ x22 * (a - a1) * (b - b1)
) / ((a2 - a1) * (b2 - b1) + 0.0)
return Y
pts = [
(0, 1, 12),
(4, 1, 0),
(0, 3, -4),
(4, 3, 8),
]
print(bilinterpol(2, 3, pts))
Output:
2.0
scipy.interpolate.interp2d()
Implementing bilinear interpolation
in Python using
SciPy
Library is Scientific Python
short for and is open source.
Consists of a large number of utility functions that help with data science, optimization, interpolation, linear algebra, signal processing, etc. SciPy
The library uses and depends on NumPy
the library.
This method can handle NumPy
very complex problems of manipulating arrays. scipy.interpolate.interp2d()
The function in our case implements bilinear interpolation on a 2d grid.
grammar:
scipy.interpolate.interp2d(
x, y, z, kind="linear", copy=True, bounds_error=False, fill_value=None
)
This function contains three important parameters that need to be understood in order to use it correctly.
-
x, y
Both contain array-like values describing the data point at a given coordinate. represents the column coordinates. In contrast, represents the coordinatesx
, considering that the data points lie on a grid .y
行
-
z
Contains an array-like value that specifies the value of the function to be interpolated using the given set of data points. -
kind
Specifies the type of interpolation to use. It can be eitherlinear
, ,cubic
orquintic
. If no argument is passed, the value defaults tolinear
.
scipy.interpolate.interp2d()
The following code implements bilinear interpolation in Python
using .
from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-15.01, 15.01, 1.00)
y = np.arange(-15.01, 15.01, 1.00)
xx, yy = np.meshgrid(x, y)
z = np.cos(xx ** 2 + yy ** 2)
f = interpolate.interp2d(x, y, z, kind="quintic")
xnew = np.arange(-15.01, 15.01, 1e-2)
ynew = np.arange(-15.01, 15.01, 1e-2)
znew = f(xnew, ynew)
plt.plot(x, z[0, :], "ro-", xnew, znew[0, :], "b-")
plt.show()
Output:
Code Explanation:
-
All three essential libraries, viz.
SciPy
, ,NumPyc
andMatPlotLib
, are imported into the code. -
Then use
numpy.arrange()
the function to insert the values into the variable in the form of an arrayx 和 y
. -
Execution continues with
meshgrid()
the function, which generates an1d
array withx 和 y
as the Cartesian index. -
The function is then used
cos()
to find the cosine value, which determines the value of the main function in the codez
. -
Finally,
matplotlib
the results are described with the help of library functions.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a Low-Pass Filter in Python
Publish Date:2025/05/07 Views:89 Category:Python
-
Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:97 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Using fetchall() in Python to extract elements from a database
Publish Date:2025/05/07 Views:171 Category:Python
-
This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra
Parsing log files in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten
Declaring a variable without a value in Python
Publish Date:2025/05/07 Views:57 Category:Python
-
A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e
Defining class global variables in Python
Publish Date:2025/05/07 Views:81 Category:Python
-
A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in
Incrementing loop step by 2 in Python
Publish Date:2025/05/07 Views:199 Category:Python
-
In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo
Pool map with multiple parameters in Python
Publish Date:2025/05/07 Views:104 Category:Python
-
multiprocessing This article will explain different ways to perform parallel function execution using the module in Python . multiprocessing The module provides functionality to perform parallel function execution using multiple inputs and
Python if...else in Lambda function
Publish Date:2025/05/07 Views:68 Category:Python
-
lambda Functions are used to implement some simple logic in Python and can be thought of as anonymous functions. It can have multiple parameters but only one expression, just def like any other function defined using the keyword. We can def