scipy插值interpolation


>>> from scipy.interpolate import interp1d
#
interp1d表示1維插值
>>>
>>> x = np.linspace(0, 10, num=11, endpoint=True) >>> y = np.cos(-x**2/9.0) >>> f = interp1d(x, y) >>> f2 = interp1d(x, y, kind='cubic')
#
kind='cubic' 省去kind參數 默認為線性插值
>>>
>>> xnew = np.linspace(0, 10, num=41, endpoint=True)
#xnew表示插值之后,畫圖,選取的畫圖點數,再用線將這些點連起來 >>> import matplotlib.pyplot as plt >>> plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--') >>> plt.legend(['data', 'linear', 'cubic'], loc='best') >>> plt.show()
../_images/interpolate-1.png

插值的方法 通過kind 導入;

f2 = interp1d(x, y, kind='cubic') 返回一個插值后的函數

>>> from scipy.interpolate import interp1d 
>>>
>>> x = np.linspace(0, 10, num=11, endpoint=True) >>> y = np.cos(-x**2/9.0) >>> f1 = interp1d(x, y, kind='nearest') >>> f2 = interp1d(x, y, kind='previous') >>> f3 = interp1d(x, y, kind='next') 
>>>
>>> xnew = np.linspace(0, 10, num=1001, endpoint=True) >>> import matplotlib.pyplot as plt >>> plt.plot(x, y, 'o') >>> plt.plot(xnew, f1(xnew), '-', xnew, f2(xnew), '--', xnew, f3(xnew), ':') >>> plt.legend(['data', 'nearest', 'previous', 'next'], loc='best') >>> plt.show() 
../_images/interpolate-2.png
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM