Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Numpy interp – One-dimensional linear interpolation for monotonically increasing sample points

  • Shrushti AsutkarShrushti Asutkar
  • September 10, 2026
  • NumPy
Numpy interp – One-dimensional linear interpolation for monotonically increasing sample points

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 unless you provide left or right replacements. The sample positions in xp must increase, and xp and fp must have the same length.

import numpy as np

xp = [0, 10, 20]
fp = [0, 100, 80]
value = np.interp(15, xp, fp)
print(value)

The output is 90.0. The query 15 lies halfway between 10 and 20, so NumPy moves halfway from 100 to 80.

90.0

Interpolate several queries at once

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.

queries = [0, 5, 15, 25]
print(np.interp(queries, xp, fp))
[  0.  50.  90.  80.]

Choose boundary values explicitly

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.

print(np.interp([-5, 25], xp, fp, left=np.nan, right=np.nan))
[nan nan]

Common input errors

  • xp and fp must contain the same number of values.
  • Use one-dimensional sequences for xp and fp.
  • A period of zero raises ValueError when periodic interpolation is requested.
  • Keep xp increasing. Duplicate sample positions can produce unexpected results.

Reference

NumPy interp documentation

Shrushti Asutkar
Shrushti Asutkar
Articles: 16
How to Read CSV with Headers Using Pandas?
Previous Post How to Read CSV with Headers Using Pandas?
Next Post List Comprehension Python: Write Concise Code
List Comprehension Python: Write Concise Code

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
np.outer(): Outer Product in NumPy

np.outer(): Outer Product in NumPy

  • November 14, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.