{"id":17707,"date":"2021-11-11T15:37:29","date_gmt":"2021-11-11T10:07:29","guid":{"rendered":"https:\/\/java2blog.com\/?p=17707"},"modified":"2023-07-08T17:09:37","modified_gmt":"2023-07-08T11:39:37","slug":"bilinear-interpolation-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/bilinear-interpolation-python\/","title":{"rendered":"Bilinear Interpolation in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#What_is_Bilinear_Interpolation\">What is Bilinear Interpolation?<\/a><\/li><li><a href=\"#Creating_a_function_to_perform_bilinear_interpolation_in_Python\">Creating a function to perform bilinear interpolation in Python<\/a><\/li><li><a href=\"#Using_the_scipyinterpolateinterp2d_function_to_perform_bilinear_interpolation_in_Python\">Using the scipy.interpolate.interp2d() function  to perform bilinear interpolation in Python<\/a><\/li><\/ul><\/div>\n<h2><span id=\"What_is_Bilinear_Interpolation\">What is Bilinear Interpolation?<\/span><\/h2>\n<p>Linear Interpolation in mathematics helps curve fitting by using linear polynomials that make new data points between a specific range of a discrete set of definite data points.<\/p>\n<p>The term Bilinear Interpolation is an extension to linear interpolation that performs the interpolation of functions containing two variables (for example, x and y) on a rectilinear two-dimensional grid.<\/p>\n<p>This tutorial will demonstrate how to perform such Bilinear Interpolation in Python.<\/p>\n<h2><span id=\"Creating_a_function_to_perform_bilinear_interpolation_in_Python\">Creating a function to perform bilinear interpolation in Python<\/span><\/h2>\n<p>We can implement the logic for Bilinear Interpolation in a function. This function works for a collection of 4 points. It is a very basic implementation of the mathematical formula for Bilinear Interpolation. <\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\">\ndef binterpolation(x, y, points):\n    pts = sorted(points)      \n    (x1, y1, q11), (_x1, y2, q12), (x2, _y1, q21), (_x2, _y2, q22) = pts\n    if x1 != _x1 or x2 != _x2 or y1 != _y1 or y2 != _y2:\n        print('The given points do not form a rectangle')\n    if not x1 &lt;= x &lt;= x2 or not y1 &lt;= y &lt;= y2:\n        print('The (x, y) coordinates are not within the rectangle')\n    P = (q11 * (x2 - x) * (y2 - y) +\n            q21 * (x - x1) * (y2 - y) +\n            q12 * (x2 - x) * (y - y1) +\n            q22 * (x - x1) * (y - y1)\n           ) \/ ((x2 - x1) * (y2 - y1) + 0.0)\n    return P\npoints = [(0, 1, 12),\n         (4, 1, 0),\n         (0, 3, -4),\n         (4, 3, 8),\n    ]\nbinterpolation(1,2, points)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n4\n<\/div>\n<h2><span id=\"Using_the_scipyinterpolateinterp2d_function_to_perform_bilinear_interpolation_in_Python\">Using the <code>scipy.interpolate.interp2d()<\/code> function  to perform bilinear interpolation in Python<\/span><\/h2>\n<p>The <code>scipy<\/code> library helps perform different mathematical and scientific calculations like linear algebra, integration, and many more. <\/p>\n<p>The <code>scipy.interpolate.interp2d()<\/code> function performs the interpolation over a two-dimensional grid. This method can handle more complex problems. This method represents functions containing <code>x<\/code>, <code>y<\/code>, and <code>z<\/code>, array-like values that make functions like <code>z = f(x, y)<\/code>. It will return the scalar value of <code>z<\/code>.<\/p>\n<p>We can use it as shown below.<\/p>\n<pre code=\"python\">\nscipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None)\n<\/pre>\n<p>To use this function, we need to understand the three main parameters.<\/p>\n<ul>\n<li><code>x, y (array_like)<\/code> &#8211; This parameter defines the data points of the coordinates. If the data points lie on a regular grid, then <code>x<\/code> can represent the column coordinates and <code>y<\/code> can represent the <code>row<\/code> coordinates. <\/li>\n<li><code>z (array_like)<\/code> &#8211; This parameter defines the value of the function to interpolate at the given data points.<\/li>\n<li><code>kind (&#039;linear&#039;, &#039;cubic&#039;, &#039;quintic&#039;)<\/code> &#8211; This parameter informs the kind of spline interpolation to use. The default value of this parameter is <code>linear<\/code>.  <\/li>\n<\/ul>\n<p>Now let us see how to perform bilinear interpolation using this method.<\/p>\n<pre code=\"python\">\nfrom scipy import interpolate\nimport numpy as np\nimport matplotlib.pyplot as plt\nx = np.arange(-10.01, 10.01, 0.50)\ny = np.arange(-10.01, 10.01, 0.50)\nxx, yy = np.meshgrid(x, y)\nz = np.cos(xx**2+yy**2)\nf = interpolate.interp2d(x, y, z, kind='quintic')\nxnew = np.arange(-10.01, 10.01, 1e-2)\nynew = np.arange(-10.01, 10.01, 1e-2)\nznew = f(xnew, ynew)\nplt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')\nplt.show()\n<\/pre>\n<p>Output:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2021\/10\/Screenshot-2021-10-04-at-1.33.17-AM-300x190.png\" alt=\"\" \/><\/p>\n<p>In the above code, <\/p>\n<ul>\n<li>The <code>numpy.arange()<\/code> function returns evenly spaced values within a given interval. This function takes the values in the form of an array. <\/li>\n<li>The <code>numpy.meshgrid()<\/code> function creates a rectangular grid with the help of one-dimensional arrays that represent cartesian indexing or matrix indexing. <\/li>\n<li>Finally, the <code>cos()<\/code> function of the <code>numpy<\/code> library helps in finding the cosine value. We use this as the main function in the above code, i.e., <code>z<\/code>. <\/li>\n<\/ul>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/search-in-row-wise-and-column-wise-sorted-matrix\/\" target=\"_blank\" rel=\"noopener\">Search in a row wise and column wise sorted matrix<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/search-in-row-wise-and-column-wise-sorted-matrix\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/perfect-number-python\/\" target=\"_blank\" rel=\"noopener\">How to calculate difference between two dates in Java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/perfect-number-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsWhat is Bilinear Interpolation?Creating a function to perform bilinear interpolation in PythonUsing the scipy.interpolate.interp2d() function to perform bilinear interpolation in Python What is Bilinear Interpolation? Linear Interpolation in mathematics helps curve fitting by using linear polynomials that make new data points between a specific range of a discrete set of definite data points. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":18311,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/17707"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=17707"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/17707\/revisions"}],"predecessor-version":[{"id":24605,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/17707\/revisions\/24605"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18311"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=17707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=17707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=17707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}