python 曲线拟合(numpy.polyfit、scipy.optimize.curve_fit)

小白的学习笔记,欢迎各位大神批评指正。

python 曲线拟合

(一次二次比较简单,直接使用numpy中的函数即可,来自 <https://blog.csdn.net/yefengzhichen/article/details/52767733>

1.多项式拟合

(1)简介

        z= numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)[source]

Deg  Degree of the fitting polynomial 1次多项式即为线性,deg次数太高会有震荡

        采用最小二乘多项式拟合,返回多项式的系数

        p = np.poly1d(z)  可以获得多项式(一元多次函数)    或者直接用yvals=np.polyval(z,5)

        x = p(5)    x带入多项式求得一个值

(2)官方文档

        numpy.polyfit

        来自 <https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html>

        The Polynomial.fit class method is recommended for new code as it is more stable numerically. See the documentation of the method for more information.

        numpy.poly1d

        来自 <https://docs.scipy.org/doc/numpy/reference/generated/numpy.poly1d.html>

        numpy.polyval

        来自 <https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyval.html>

(3)例子  来自 <https://www.cnblogs.com/jingsupo/p/python_curve_fit.html>

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(1,17,1)

y = np.array([4.00,6.40,8.00,8.80,9.22,9.50,9.70,9.86,10.00,10.20,10.32,10.42,10.50,10.55,10.58,10.60])

z1 = np.polyfit(x,y,3#3次多项式拟合  可以改为5 次多项式。。。。 返回三次多项式系数

p1= np.poly1d(z1)

print(p1) #在屏幕上打印拟合多项式

yvals = p1(x)#也可以使用yvals=np.polyval(z1,x)

plot1 = plt.plot(x,y,'*',label='original values')

plot2 = plt.plot(x,yvals,'r',label='polyfit values')

plt.xlabel('xaxis')

plt.ylabel('yaxis')

plt.legend(loc=4)  #指定legend的位置,读者可以自己help它的用法

plt.title('polyfitting')

plt.show()

plt.savefig('p1.png')

 

2.scipy.optimize.curve_fit

(1)简介

Fitting data with SciPy 来自 <http://scipyscriptrepo.com/wp/?p=76>

The scipy.optimize module contains a least squares curve fit routine that requires as input a user-defined fitting function (in our case fitFunc ),  the x-axis data (in our case, t) and the y-axis data (in our case, noisy). The curve_fit routine returns an array of fit parameters, and a matrix of covariance data 协方差(the square root of the diagonal values 对角线值are the 1-sigma uncertainties on the fit parameters—provided you have a reasonable fit in the first place.):

当然,curve_fit()函数不仅可以用于直线、二次曲线、三次曲线的拟合和绘制,仿照代码中的形式,可以适用于任意形式的曲线的拟合和绘制,只要定义好合适的曲线方程即可。来自 <https://blog.csdn.net/guduruyu/article/details/70313176>

调用curve_fit()函数,核心步骤

         1) 定义需要拟合的函数类型,如:

            def func(x, a, b):

                return a*np.exp(b/x)

          2) 调用 popt, pcov = curve_fit(func, x, y) 函数进行拟合,并将拟合系数存储在popt中,a=popt[0]、b=popt[1]进行调用;

           3) 调用func(x, a, b)函数,其中x表示横轴表,a、b表示对应的参数。

来自 <http://www.mamicode.com/info-detail-1793339.html>

(2)官方文档

scipy.optimize.curve_fit   来自 <https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html>

(3)例子   来自 <https://www.cnblogs.com/jingsupo/p/python_curve_fit.html>

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

import numpy as np

#用指数形式来拟合

x=np.arange(1,17,1)

y=np.array([4.00,6.40,8.00,8.80,9.22,9.50,9.70,9.86,10.00,10.20,10.32,10.42,10.50,10.55,10.58,10.60])

def func(x,a,b):

      return a*np.exp(b/x)

popt,pcov=curve_fit(func,x,y)

a=popt[0] #popt里面是拟合系数,读者可以自己help其用法

b=popt[1]

yvals=func(x,a,b)

plot1=plt.plot(x,y,'*',label='original values')

plot2=plt.plot(x,yvals,'r',label='curve_fit values')

plt.xlabel('xaxis')

plt.ylabel('yaxis')

plt.legend(loc=4)  #指定legend的位置,读者可以自己help它的用法

plt.title('curve_fit')

plt.show()

plt.savefig('p2.png')

 

 

1.幂函数和指数函数的例子

python指数、幂数拟合curve_fit  来自 <https://blog.csdn.net/yefengzhichen/article/details/52767733>

2.高斯拟合的例子

python scipy.optimize curve_fit 多高斯拟合 来自 <https://blog.csdn.net/vola9527/article/details/40432609>

def f_gauss(x, A, B, C, sigma):

       return A*np.exp(-(x-B)**2/(2*sigma**2)) + C

 来自 <https://blog.csdn.net/guduruyu/article/details/70313176>
 

其他:

可以借助Pandas导入数据,再进行处理

来自 <http://www.mamicode.com/info-detail-1793339.html>

 

矩阵运算:

Python 二维曲线拟合 

来自 <http://www.aspku.com/tech/jiaoben/python/338265.html>

python_numpy最小二乘法的直线、曲线拟合

来自 <https://www.jianshu.com/p/354b1f2a5fd0>

 

3.使用神经网络拟合曲线(MATLAB/Python)

来自 <https://blog.csdn.net/hongbin_xu/article/details/79674611>

 

  • 48
    点赞
  • 271
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值