scipy.interpolate: 插值和平滑处理
生活随笔
收集整理的這篇文章主要介紹了
scipy.interpolate: 插值和平滑处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
scipy有很多插值函數(方法),按維度可分為一維、二維和多維的插值方法,按方法包括拉格朗日和泰勒插值方法等,具體插值函數可參閱如下介紹:
https://docs.scipy.org/doc/scipy/reference/interpolate.html?highlight=scipy%20interpolate#module-scipy.interpolate
一維插值
這里簡單介紹下一維插值方法interpolate.interp1d
import numpy as np from scipy.interpolate import interp1d # 創建待插值的數據 x = np.linspace(0, 10 * np.pi, 20) y = np.cos(x) # 分別用linear和quadratic插值 fl = interp1d(x, y, kind='linear') fq = interp1d(x, y, kind='quadratic') xint = np.linspace(x.min(), x.max(), 1000) # 將x設置為1000個點 yintl = fl(xint) # 線性插值 yintq = fq(xint) # 二次項插值結果如圖:
import matplotlib.pyplot as plt fig,ax = plt.subplots(2,2,figsize=(10,8)) plt.subplot(221) plt.plot(x,y,label='raw-data',marker='o') plt.legend()plt.subplot(223) plt.scatter(xint,yintl,label='linear_interpl',marker='o') plt.legend() plt.subplot(224) plt.scatter(xint,yintq,label='quadratic_interpl',marker='o') plt.legend()平滑度
類似于曲線擬合,平滑度越高,滑動窗口取值越寬,曲線越平滑,具體參數可參考文檔:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline
from scipy.interpolate import UnivariateSpline x = np.linspace(-5,5,200) y = np.exp(-x**2)+np.random.randn(200)/10# 平滑曲線處理,平滑參數s=1 s = UnivariateSpline(x,y,s=1) xs = np.linspace(-5,5,1000) ys = s(xs) plt.plot(x,y,'.-') plt.plot(xs,ys,label='s=1') plt.legend() plt.show()# 平滑曲線處理,平滑參數s=2 s = UnivariateSpline(x,y,s=2) xs = np.linspace(-5,5,1000) ys = s(xs) plt.plot(x,y,'.-') plt.plot(xs,ys,label='s=2') plt.legend() plt.show()?
總結
以上是生活随笔為你收集整理的scipy.interpolate: 插值和平滑处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 剑灵灵值怎么获得-灵值获得方法详解
- 下一篇: Python中import模块的两种模式