1. 程式人生 > 程式設計 >python的scipy實現插值的示例程式碼

python的scipy實現插值的示例程式碼

插值對於一些時間序列的問題可能比較有用。

Show the code directly:

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
 
x=np.linspace(0,10*np.pi,num=20)
y=np.sin(x)
f1=interp1d(x,y,kind='linear')#線性插值
f2=interp1d(x,kind='cubic')#三次樣條插值
x_pred=np.linspace(0,num=1000)
y1=f1(x_pred)
y2=f2(x_pred)
plt.plot(x_pred,y1,'r',label='linear')
plt.plot(x_pred,y2,'b--',label='cubic')
plt.legend()
plt.show()

官網上有更詳細的引數使用:https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.interp1d.html

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。