Python – scipy.interpolate.interp1d
scipy.interpolate 包中的 interp1d() 函式用於插值一維函式。它採用值陣列,如 x 和 y,來逼近某個函式 y = f(x),然後使用插值查詢新點的值。
語法
scipy.interpolate.interp1d(x, y)
其中 x 是實值的一維陣列,y 是實值的 N 維陣列。y 沿插值軸的長度必須等於 x 的長度。
示例 1
讓我們考慮以下示例 −
# Import the required libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
# Set the figure size
plt.rcParams["figure.figsize"]=[7.00, 3.50]
plt.rcParams["figure.autolayout"]=True
# Define the values
x = np.arange(0, 10)
y = np.exp(-x/5.0)
# Input Data
plt.subplot(1,2,1)
plt.title("Input X and Y")
plt.plot(x,y)
# Interpolated Data
plt.subplot(1,2,2)
plt.title("Interpolated")
f = interpolate.interp1d(x, y)
x_new = np.arange(0, 7, 0.7)
y_new = f(x_new)
plt.plot(x_new, y_new, 's')
plt.show()輸出
上述程式將生成以下輸出 −
.png)
示例 2
讓我們再舉一個例子 −
# Import the required libraries import matplotlib.pyplot as plt import numpy as np from scipy import interpolate # Set the figure size plt.rcParams["figure.figsize"]=[7.00, 3.50] plt.rcParams["figure.autolayout"]=True # Define the values x = np.arange(0, 10) y = np.exp(-x **2/9.0) # interpolate function f = interpolate.interp1d(x, y) xnew = np.arange(0, 9, 1.2) plt.plot(x, y, 'o', xnew) plt.show()
輸出
上述程式將生成以下輸出 −
.png)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP