{"id":39343,"date":"2026-09-10T11:35:30","date_gmt":"2026-09-10T11:35:30","guid":{"rendered":"https:\/\/www.askpython.com\/?p=39343"},"modified":"2026-09-10T11:35:21","modified_gmt":"2026-09-10T11:35:21","slug":"numpy-interp","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-interp","title":{"rendered":"Numpy interp &#8211; One-dimensional linear interpolation for monotonically increasing sample points"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">NumPy interp estimates a value between known samples by joining neighboring points with straight line segments. Give it the x-coordinate you need, the sorted sample positions, and the matching sample values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use numpy.interp for a value between samples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The function returns the first or last sample value when your query falls outside the sample range unless you provide left or right replacements. The sample positions in xp must increase, and xp and fp must have the same length.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nxp = &#x5B;0, 10, 20]\nfp = &#x5B;0, 100, 80]\nvalue = np.interp(15, xp, fp)\nprint(value)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The output is 90.0. The query 15 lies halfway between 10 and 20, so NumPy moves halfway from 100 to 80.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n90.0\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Interpolate several queries at once<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pass an array of query positions when you need a batch of estimates. NumPy applies the same neighboring-segment rule to each value and returns an array with the matching shape.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nqueries = &#x5B;0, 5, 15, 25]\nprint(np.interp(queries, xp, fp))\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n&#x5B;  0.  50.  90.  80.]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Choose boundary values explicitly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Outside the sample range, the defaults are fp[0] on the left and fp[-1] on the right. Set left and right when those defaults would hide a missing-data condition.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(np.interp(&#x5B;-5, 25], xp, fp, left=np.nan, right=np.nan))\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n&#x5B;nan nan]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Common input errors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>xp and fp must contain the same number of values.<\/li>\n\n\n<li>Use one-dimensional sequences for xp and fp.<\/li>\n\n\n<li>A period of zero raises ValueError when periodic interpolation is requested.<\/li>\n\n\n<li>Keep xp increasing. Duplicate sample positions can produce unexpected results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.interp.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy interp documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy interp estimates a value between known samples by joining neighboring points with straight line segments. Give it the x-coordinate you need, the sorted sample positions, and the matching sample values. Use numpy.interp for a value between samples The function returns the first or last sample value when your query falls outside the sample range [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":67059,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-39343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/39343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=39343"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/39343\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/67059"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=39343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=39343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=39343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}