Python-曲线拟合(一)

曲线拟合:一种函数近似,不要求通过数据点,合理反应数据的基本趋势。

1. 最小二乘法

拟合函数 φ ( x i ) \varphi(x_i) φ(xi)与原始数据点 _ yi的偏差为:
δ i = φ ( x i ) − y i ( i = 0 , 1 , 2 , ⋯   , n ) \delta_{i}=\varphi\left(x_{i}\right)-y_{i} \quad(i=0,1,2, \cdots, n) δi=φ(xi)yi(i=0,1,2,,n)
最小二乘法: min ⁡ ( ∑ i = 1 n δ i 2 ) \min \left(\sum_{i=1}^{n} \delta_{i}^{2}\right) min(i=1nδi2)
考虑数据误差时: min ⁡ ( ∑ i = 1 n ω i δ i 2 ) \min \left(\sum_{i=1}^{n} \omega_i\delta_{i}^{2}\right) min(i=1nωiδi2)

2. 多项式拟合

2.1 理论基础

  1. 用次插值多项式 ( ) _() Pm(x)来拟合个数据点( < ):
    P m ( x ) = a 0 + a 1 x + ⋯ + a m x m = ∑ j = 0 m a j x j P_{m}(x)=a_{0}+a_{1} x+\cdots+a_{m} x^{m}=\sum_{j=0}^{m} a_{j} x^{j} Pm(x)=a0+a1x++amxm=j=0majxj
  2. 选取多项式系数 _ aj使
    ∑ i = 1 n δ i 2 = ∑ i = 1 n [ y i − P m ( x i ) ] 2 = F ( a 0 , a 1 , ⋯   , a n ) \sum_{i=1}^{n} \delta_{i}^{2}=\sum_{i=1}^{n}\left[y_{i}-P_{m}\left(x_{i}\right)\right]^{2}\\=F\left(a_{0}, a_{1}, \cdots, a_{n}\right) i=1nδi2=i=1n[yiPm(xi)]2=F(a0,a1,,an)
    最小, 由多元函数的极值条件得方程组
    ∂ F ∂ a j = − 2 ∑ i = 1 n [ y i − ∑ k = 0 m a k x i k ] x i j = 0 \frac{\partial F}{\partial a_{j}}=-2 \sum_{i=1}^{n}\left[y_{i}-\sum_{k=0}^{m} a_{k} x_{i}^{k}\right] x_{i}^{j}=0 ajF=2i=1n[yik=0makxik]xij=0
    移项得:
    ∑ k = 0 m a k ( ∑ i = 1 n x i k + j ) = ∑ i = 1 n y i x i j \sum_{k=0}^{m} a_{k}\left(\sum_{i=1}^{n} x_{i}^{k+j}\right)=\sum_{i=1}^{n} y_{i} x_{i}^{j} k=0mak(i=1nxik+j)=i=1nyixij
    写成方程组得:
    { n a 0 + a 1 ∑ i = 1 n x i + a 2 ∑ i = 1 n x i 2 + ⋯ + a m ∑ i = 1 n x i m = ∑ i = 1 n y i a 0 ∑ i = 1 n x i + a 1 ∑ i = 1 n x i 2 + ⋯ + a m ∑ i = 1 n x i m + 1 = ∑ i = 1 n y i x i a 0 ∑ i = 1 n x i m + a 1 ∑ i = 1 n x i m + 1 + ⋯ + a m ∑ i = 1 n x i 2 m = ∑ i = 1 n y i x i m \left\{\begin{aligned} n a_{0}+a_{1} \sum_{i=1}^{n} x_{i}+a_{2} \sum_{i=1}^{n} x_{i}^{2}+\cdots+a_{m} \sum_{i=1}^{n} x_{i}^{m}=\sum_{i=1}^{n} y_{i} \\ a_{0} \sum_{i=1}^{n} x_{i}+a_{1} \sum_{i=1}^{n} x_{i}^{2}+\cdots+a_{m} \sum_{i=1}^{n} x_{i}^{m+1}=\sum_{i=1}^{n} y_{i} x_{i} \\ a_{0} \sum_{i=1}^{n} x_{i}^{m}+a_{1} \sum_{i=1}^{n} x_{i}^{m+1}+\cdots+a_{m} \sum_{i=1}^{n} x_{i}^{2 m}=\sum_{i=1}^{n} y_{i} x_{i}^{m} \end{aligned}\right. na0+a1i=1nxi+a2i=1nxi2++ami=1nxim=i=1nyia0i=1nxi+a1i=1nxi2++ami=1nxim+1=i=1nyixia0i=1nxim+a1i=1nxim+1++ami=1nxi2m=i=1nyixim

当最高幂次 = 1 = 1 m=1时,拟合的是直线,方程组化简为
{ n a 0 + a 1 ∑ i = 1 n x i = ∑ i = 1 n y i a 0 ∑ i = 1 n x i + a 1 ∑ i = 1 n x i 2 = ∑ i = 1 n y i x i \left\{\begin{array}{c} n a_{0}+a_{1} \sum_{i=1}^{n} x_{i}=\sum_{i=1}^{n} y_{i} \\ a_{0} \sum_{i=1}^{n} x_{i}+a_{1} \sum_{i=1}^{n} x_{i}^{2}=\sum_{i=1}^{n} y_{i} x_{i} \end{array}\right. {na0+a1i=1nxi=i=1nyia0i=1nxi+a1i=1nxi2=i=1nyixi

解线性方程组,即可求得 0 , 1 _0, _1 a0,a1.

  1. X = [ 1 x 1 ⋮ ⋮ 1 x n ] , β = [ a 0 a 1 ] , Y = [ y 1 ⋮ y n ] X=\left[\begin{array}{cc} 1 & x_{1} \\ \vdots & \vdots \\ 1 & x_{n} \end{array}\right], \beta=\left[\begin{array}{c} a_{0} \\ a_{1} \end{array}\right], \\Y=\left[\begin{array}{c} y_{1} \\ \vdots \\ y_{n} \end{array}\right] X=11x1xn,β=[a0a1],Y=y1yn
    方程组可记为 X T X β = X T Y X^{T} X \beta=X^{T} Y XTXβ=XTY
  2. 线性方程组的法方程
    X T X β ⃗ = X T y ⃗ X^{T} X \vec{\beta}=X^{T} \vec{y} XTXβ =XTy

X T X = [ 1 1 ⋯ 1 x 1 x 2 ⋯ x n ] [ 1 x 1 1 x 2 ⋮ ⋮ 1 x n ] = [ n ∑ i = 1 n x i ∑ i = 1 n x i ∑ i = 1 n x i 2 ] X^{T} X=\left[\begin{array}{lllr} 1 & 1 & \cdots & 1 \\ x_{1} & x_{2} & \cdots & x_{n} \end{array}\right]\left[\begin{array}{cc} 1 & x_{1} \\ 1 & x_{2} \\ \vdots & \vdots \\ 1 & x_{n} \end{array}\right]\\=\left[\begin{array}{cc} n & \sum_{i=1}^{n} x_{i} \\ \sum_{i=1}^{n} x_{i} & \sum_{i=1}^{n} x_{i}^{2} \end{array}\right] XTX=[1x11x21xn]111x1x2xn=[ni=1nxii=1nxii=1nxi2]

X T y ⃗ = [ 1 1 ⋯ 1 x 1 x 2 ⋯ x n ] [ y 1 y 2 ⋮ y n ] = [ ∑ i = 1 n y i ∑ i = 1 n x i y i ] X^{T} \vec{y}=\left[\begin{array}{cccc} 1 & 1 & \cdots & 1 \\ x_{1} & x_{2} & \cdots & x_{n} \end{array}\right]\left[\begin{array}{c} y_{1} \\ y_{2} \\ \vdots \\ y_{n} \end{array}\right]\\=\left[\begin{array}{l} \sum_{i=1}^{n} y_{i} \\ \sum_{i=1}^{n} x_{i} y_{i} \end{array}\right] XTy =[1x11x21xn]y1y2yn=[i=1nyii=1nxiyi]

法方程化为
[ n ∑ i = 1 n x i ∑ i = 1 n x i ∑ i = 1 n x i 2 ] [ [ β 0 β 1 ] = [ ∑ i = 1 n y i ∑ i = 1 n x i y i ] \left[\begin{array}{cc} n & \sum_{i=1}^{n} x_{i} \\ \sum_{i=1}^{n} x_{i} & \sum_{i=1}^{n} x_{i}^{2} \end{array}\right]\left[\begin{array}{c} {\left[\begin{array}{c} \beta_{0} \\ \beta_{1} \end{array}\right]=\left[\begin{array}{c} \sum_{i=1}^{n} y_{i} \\ \sum_{i=1}^{n} x_{i} y_{i} \end{array}\right]} \end{array}\right. [ni=1nxii=1nxii=1nxi2][[β0β1]=[i=1nyii=1nxiyi]

即:
{ n β 0 + β 1 ∑ i = 1 n x i = ∑ i = 1 n y i β 0 ∑ i = 1 n x i + β 1 ∑ i = 1 n x i 2 = ∑ i = 1 n y i x i \left\{\begin{array}{c} n \beta_{0}+\beta_{1} \sum_{i=1}^{n} x_{i}=\sum_{i=1}^{n} y_{i} \\ \beta_{0} \sum_{i=1}^{n} x_{i}+\beta_{1} \sum_{i=1}^{n} x_{i}^{2}=\sum_{i=1}^{n} y_{i} x_{i} \end{array}\right. {nβ0+β1i=1nxi=i=1nyiβ0i=1nxi+β1i=1nxi2=i=1nyixi

2.2 Python中的多项式拟合

np.polyfit
实例代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d


t = np.ogrid[0:5.5:0.5]
s = t+ 0.2*t**2
s1 = np.array([0.5,-0.18,-0.01,0.13,0.1,0.31,-0.22,-0.31,0.2,0.4,-0.14]) # 假设误差

ss = s+s1
P = np.polyfit(t,ss,2) # 二次曲线拟合
y = np.polyval(P,t) # 利用参数拟合函数值
plt.plot(t,ss,"r*",t,y,"g",lw=4,ms=10)

结果:
Python-曲线拟合(一)_第1张图片

你可能感兴趣的:(Python数据处理,python,线性代数)