scipy.optimize.curve_fit函数用法解析

转:https://zhuanlan.zhihu.com/p/144353126

optimize.curve_fit()函数,用于日常数据分析中的数据曲线拟合。

语法:scipy.optimize.curve_fit(f,xdata,ydata,p0=None,sigma=None,absolute_sigma=False,check_finite=True,bounds=(-inf,inf),method=None,jac=None,**kwargs)

参数解析:

(官方文档说明:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy-optimize-curve-fit)

  • f 函数名
    callable

The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

简单来说就是需要拟合的函数y,包括自变量x,参数A,B;
而curve_fit的主要功能就是计算A,B

#要拟合的一次函数
def f_1(x, A, B):return A * x + B
  • xdata
    array_like or object

    The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.
    简单说就是要拟合的自变量数组
  • ydata
    array_like

    The dependent data, a length M array - nominallyf(xdata,...)
    简单说就是要拟合的因变量的值
  • p0
    array_like , optional

    Initial guess for the parameters (length N). If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).
    就是给你的函数的参数确定一个初始值来减少计算机的计算量
    其它的参数均不再说明!

返回值解析

  • popt array
    Optimal values for the parameters so that the sum of the squared residuals off(xdata,*popt)-ydatais minimized
    即残差最小时参数的值
  • pcov 2d array
    The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr=np.sqrt(np.diag(pcov)).

How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.

If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.

简单例子

  • 例子1:拟合直线
# 引用库函数
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as op# 需要拟合的数据组
x_group = np.array([3, 6.1, 9.1, 11.9, 14.9])
y_group = np.array([0.0221, 0.0491, 0.0711, 0.0971, 0.1238])# 需要拟合的函数
def f_1(x, A, B):return A * x + B# 得到返回的A,B值
A, B = op.curve_fit(f_1, x_group, y_group)[0]
# 数据点与原先的进行画图比较
plt.scatter(x_group, y_group, marker='o',label='real')
x = np.arange(0, 15, 0.01)
y = A * x + B
plt.plot(x, y,color='red',label='curve_fit')
plt.legend()
plt.title('%.5fx%.5f=y' % (A, B))
plt.show()

  • 例子2:官方例子[1]
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np# 定义需要拟合的函数
def func(x, a, b, c):return a * np.exp(-b * x) + c# Define the data to be fit with some noise:
# 用numpy的random库生成干扰
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')
# Fit for the parameters a, b, c of the function func:popt, pcov = curve_fit(func, xdata, ydata)
print(popt)plt.plot(xdata, func(xdata, *popt), 'r-',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
# Constrain the optimization to the region of 0 <= a <= 3, 0 <= b <= 1 and 0 <= c <= 0.5:
# 限定范围进行拟合
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
print(popt)plt.plot(xdata, func(xdata, *popt), 'g--',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
#结果
#[2.55423706 1.35190947 0.47450618]
#[2.43708905 1.         0.35015434]

参考

  1. scipy.optimize官方文档 https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy-optimize-curve-fit

scipy.optimize.curve_fit函数用法解析相关推荐

  1. python中curve fit_scipy.optimize.curve_fit函数用法解析

    在日常数据分析中,免不了要用到数据曲线拟合,而optimize.curve_fit()函数正好满足你的需求 scipy.optimize.curve_fit(f,xdata,ydata,p0=None ...

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

    小白的学习笔记,欢迎各位大神批评指正. python 曲线拟合 (一次二次比较简单,直接使用numpy中的函数即可,来自 <https://blog.csdn.net/yefengzhichen ...

  3. scipy中的scipy.optimize.curve_fit

    scipy中的scipy.optimize.curve_fit 这里写目录标题 scipy中的scipy.optimize.curve_fit 参数 Return scipy.optimize.``c ...

  4. scipy.optimize.linprog函数参数最全详解

    scipy.optimize.linprog函数 1.线性规划概念 2.输入格式 3.参数设置: 4.输出格式: 5.例子 6.若有更多Python的问题,请挪步"佐佑思维"公众号 ...

  5. scipy.optimize.linprog()函数--求解线性规划问题

    这里写目录标题 关于方程组的标准形式 参数 c A_ub b_ub A_eq b_eq bounds method callback options maxiter disp presolve 返回值 ...

  6. python中str和input_python中eval()函数和input()函数用法解析

    今天给大家讲解Python中eval()函数和input()函数的用法,希望通过实例的讲解之后大家能对这两个函数有更加深刻的理解. 1.eval()函数 eval(<字符串>)能够以Pyt ...

  7. python里eval和input组合使用_python中eval()函数和input()函数用法解析

    今天给大家讲解Python中eval()函数和input()函数的用法,希望通过实例的讲解之后大家能对这两个函数有更加深刻的理解. 1.eval()函数 eval()能够以Python表达式的方式解析 ...

  8. wordpress php 链接,WordPress中获取页面链接和标题的相关PHP函数用法解析

    get_permalink()(获取文章或页面链接)get_permalink() 用来根据固定连接返回文章或者页面的链接.在获取链接时 get_permalink() 函数需要知道要获取的文章的 I ...

  9. Oracle中decode函数用法解析以及常用场景

    1.decode函数的两种形式 第一种形式 含义解释: decode(条件,值1,返回值1,值2,返回值2,-值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THENRETURN(翻译 ...

最新文章

  1. openshift django目录结果
  2. linux内核的中断上下文,Linux操作系统中中断上下文中的互斥
  3. linux:scp命令
  4. Codeforce 1182B Plus from Picture
  5. 两台服务器实现会话共享
  6. zoj 3707 Calculate Prime S
  7. wepyjs小程序组件调用pages页面的方法
  8. SpringBoot+zk+dubbo架构实践(五):搭建微服务电商架构(内附GitHub地址)
  9. android kill process,为什么Application有时会在killProcess上重启?
  10. 新路由3 高恪魔改固件+底包
  11. Smarty中文手册
  12. 后端+数据库(pycharm+mysql):使用 “flask”制作的调查量表/问卷
  13. 服务器c盘有个inetpub文件夹,inetpub是什么文件夹
  14. 如何改变计算机内存配置文件,电脑内存使用率过高怎么解决?教你如何调整内存大小...
  15. 使用谷歌身份验证器(Google Authenticator)保护你的后台
  16. IOS 16 UITabBarItem设置字体属性崩溃
  17. 关闭word后自动打开新的文档
  18. 英文论文写作排版-IEEE论文排版技巧
  19. Enviro - Sky and Weather v2.3.1.rar
  20. Elasticsearch(5.x、6.x、7.x、8.x)的兼容性

热门文章

  1. FPGA: VGA显示
  2. 怎么用计算机直接截图,电脑怎么截图?教你几招方法
  3. Go程序设计语言3.3 复数
  4. 我明明学了很多东西,却感觉什么也没学到?那你可能会需要知识管理
  5. 【小技巧】Android SDK模拟器 增加手机内存RAM和ROM 横竖屏切换 AVD安装路
  6. Xcode及模拟器SDK下载
  7. 1517_AURIX TC275 SRI中的仲裁功能
  8. 有道难题——2010网易编程挑战赛
  9. 高三数学第一轮复习:对数与对数函数_习题含解析
  10. Java实现定时器(Timer)