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