Interpolation and fitting
Original link: https://zhuanlan.zhihu.com/p/28149195
1. least square fitting
Example 1
# - * - Coding: UTF-. 8 - * - Import numpy AS NP Import matplotlib.pyplot AS PLT from scipy.optimize Import leastsq # # character set, to prevent distortion Chinese Import matplotlib [matplotlib.rcParams ' font.sans serif- ' ] = [U ' simhei ' ] matplotlib.rcParams [ ' axes.unicode_minus ' ] = False plt.figure (figsize = (9, 9 )) X = np.linspace (0,10,1000 ) X- = np.array ( [8.19, 2.72, 6.39, 8.71, 4.7, 2.66, 3.78 ]) the YNp.array = ([7.01, 2.78, 6.47, 6.71, 4.1, 4.23, 4.05 ]) # calculating an error between the straight line in the parameter p and the raw data DEF F (p): K, B = p return (the Y - (+ K * X- B)) # leastsq array such that the output f of the square and the minimum initial value of the parameter [1,0] R & lt leastsq = (f, [. 1 , 0]) K, B = R & lt [0] Print ( " K = " , K, " B = " , B) plt.scatter (X-, the Y, S = 100, Alpha = 1.0, marker = ' O ' , label = U ' data points ' ) Y = X + K * B AX =plt.gca () ax.set_xlabel (..., fontSize = 20 is ) ax.set_ylabel (..., fontSize = 20 is ) # Set axis label font size plt.plot (X, Y, Color = ' R & lt ' , =. 5 as linewidth, lineStyle = " : " , markersize = 20 is, label = U ' fit curve ' ) plt.legend (LOC = 0, = NumPoints. 1 ) leg = plt.gca () get_legend (). LTEXT = leg. get_texts () plt.setp (LTEXT, fontSize = ' XX-Large ' ) plt.xlabel (U ' amperes / A ') Plt.ylabel (U ' V / V ' ) plt.xlim (0, x.max () * 1.1 ) plt.ylim (0, y.max () * 1.1 ) plt.xticks (fontSize = 20 is ) PLT. yticks (fontSize = 20 is ) # scale font size plt.legend (LOC = ' Upper left ' ) plt.show ()
k= 0.6134953491930442 b= 1.794092543259387
Example 2
# - * - Coding: UTF-. 8 - * - # least squares fit Examples Import numpy AS NP from scipy.optimize Import leastsq Import pylab AS PL # # character set, to prevent distortion Chinese Import matplotlib matplotlib.rcParams [ ' font -serif .sans ' ] = [U ' simhei ' ] matplotlib.rcParams [ ' axes.unicode_minus ' ] = False DEF FUNC (X, P): "" " function used to fit the data: a * cos (2 * pi Theta + K * X *) "" " A, K, Theta = P returnNp.sin * A (X + K * Theta) DEF residuals (P, y, X): "" " Experimental data x, y and fitting between the difference function, p is a need to find the fitting coefficient " "" return Y - FUNC (X, P) X = np.linspace (0, 20 is, 100 ) a, K, Theta = 10,. 3,. 6 # arguments real data y0 = func (x, [a , k, theta] ) # real data Y1 np.random.randn = yO + 2 * (len (X)) # experimental data after the noise is added P0 = [10, 0.2, 0] # first guess function fitting parameter # call leastsq data fitting # residuals calculated as a function of the error # P0 as the initial value of the fitting parameter # args required to fit the experimental data = leastsq plsq (residuals, P0, args = (Y1, X)) Print (U " true parameter: " , [A, K, Theta]) Print (U " fitting parameter " , plsq [0]) # experimental data fitting parameters pl.plot (X, yO, Color = ' R & lt ' , U = label " real data " ) pl.plot (X, Y1, Color = ' B ' , U label = " experimental data noisy " ) pl.plot (X, FUNC (X, plsq [0]), Color = ' G ' , U label = " fit data ") pl.legend() pl.show()
Real parameters: [10, 3, 6 ]
fitting parameters [ -1.16428658 0.24215786 -0.794681]
2. Interpolation
Example 1
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*- import numpy as np import pylab as pl from scipy import interpolate import matplotlib.pyplot as plt ## 设置字符集,防止中文乱码 import matplotlib matplotlib.rcParams['font.sans-serif']=[u'simHei'] matplotlib.rcParams['axes.unicode_minus']=False x = np.linspace(0, 2*np.pi+np.pi/4, 10) y = np.sin(x) x_new = np.linspace(0, 2*np.pi+np.pi/4, 100) f_linear = interpolate.interp1d(x, y) tck = interpolate.splrep(x, y) y_bspline = interpolate.splev(x_new, tck) plt.xlabel(u'安培/A') plt.ylabel(u'伏特/V') plt.plot(x, y, "o", label=u"原始数据") plt.plot(x_new, f_linear(x_new), label=u"线性插值") plt.plot(x_new, y_bspline, label=u"B-spline插值") pl.legend() pl.show()
实例2
# -*- coding: utf-8 -*- import numpy as np from scipy import interpolate import pylab as pl ## 设置字符集,防止中文乱码 import matplotlib matplotlib.rcParams['font.sans-serif']=[u'simHei'] matplotlib.rcParams['axes.unicode_minus']=False #创建数据点集并绘制 pl.figure(figsize=(12,9)) x = np.linspace(0, 10, 11) y = np.sin(x) ax=pl.plot() pl.plot(x,y,'ro') #建立插值数据点 xnew = np.linspace(0, 10, 101) for kind in ['nearest', 'zero','linear','quadratic']: #根据kind创建插值对象interp1d f = interpolate.interp1d(x, y, kind = kind) ynew = f(xnew)#计算插值结果 pl.plot(xnew, ynew, label = str(kind)) pl.xticks(fontsize=20) pl.yticks(fontsize=20) pl.legend(loc = 'lower right') pl.show()
B样条曲线插值
一维数据的插值运算可以通过 interp1d()实现。
其调用形式为:
Interp1d可以计算x的取值范围之内任意点的函数值,并返回新的数组。
interp1d(x, y, kind=‘linear’, …)
参数 x和y是一系列已知的数据点
参数kind是插值类型,可以是字符串或整数
B样条曲线插值
Kind给出了B样条曲线的阶数:
‘
zero‘ ‘nearest’ :0阶梯插值,相当于0阶B样条曲线
‘slinear’‘linear’ :线性插值,相当于1阶B样条曲线
‘quadratic’‘cubic’:2阶和3阶B样条曲线,更高阶的曲线可以直接使用整数值来指定
(1)#创建数据点集:
import numpy as np
x = np.linspace(0, 10, 11)
y = np.sin(x)
(2)#绘制数据点集:
import pylab as pl
pl.plot(x,y,'ro')
创建interp1d对象f、计算插值结果:
xnew = np.linspace(0, 10, 11)
from scipy import interpolate
f = interpolate.interp1d(x, y, kind = kind)
ynew = f(xnew)
根据kind类型创建interp1d对象f、计算并绘制插值结果:
xnew = np.linspace(0, 10, 11)
for kind in ['nearest', 'zero','linear','quadratic']:
#根据kind创建插值对象interp1d
f = interpolate.interp1d(x, y, kind = kind)
ynew = f(xnew)#计算插值结果
pl.plot(xnew, ynew, label = str(kind))#绘制插值结果
如果我们将代码稍作修改增加一个5阶插值
import numpy as np from scipy import interpolate import pylab as pl #创建数据点集并绘制 pl.figure(figsize=(12,9)) x = np.linspace(0, 10, 11) y = np.sin(x) ax=pl.plot() pl.plot(x,y,'ro') #建立插值数据点 xnew = np.linspace(0, 10, 101) for kind in ['nearest', 'zero','linear','quadratic',5]: #根据kind创建插值对象interp1d f = interpolate.interp1d(x, y, kind = kind) ynew = f(xnew)#计算插值结果 pl.plot(xnew, ynew, label = str(kind)) pl.xticks(fontsize=20) pl.yticks(fontsize=20) pl.legend(loc = 'lower right') pl.show() 运行得到
发现5阶已经很接近正弦曲线,但是如果x值选取范围较大,则会出现跳跃。
关于拟合与插值的数学基础可参见霍开拓:拟合与插值的区别?
左边插值,右边拟合