python双曲线拟合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python双曲线拟合相关的知识,希望对你有一定的参考价值。

待拟合函数 y = alpha * pow(x, beta) 

输入: x数组,y数组

输出: alpha,beta,相关系数R2

from scipy.optimize import leastsq
from pylab import *
import numpy as np

xdata = np.array([4.79616, 11.63, 37.5534, 105.414])

ydata = np.array([1.33921, 0.755319, 0.34085, 0.0554339])

# Define function for calculating a power law
powerlaw = lambda x, alpha, beta: alpha * (x ** beta)
logx = log10(xdata)
logy = log10(ydata)

# define our (line) fitting function
fitfunc = lambda p, x: p[0] + p[1] * x   
errfunc = lambda p, x, y: (y - fitfunc(p, x))

pinit = [1.0, -1.0]
out, cov, infodict, mesg, ier = leastsq(errfunc, pinit,
                       args = (logx, logy), full_output=1)

beta = out[1]
alpha = 10.0 ** out[0]

ss_err = (infodict[‘fvec‘] ** 2).sum()
ss_tot = ((ydata - ydata.mean()) ** 2).sum()
r2 = 1 - (ss_err / ss_tot)

print(‘Alpha: %f, Beta: %f‘ % (alpha, beta))
print (‘R2: %f‘ % r2)

  

 

以上是关于python双曲线拟合的主要内容,如果未能解决你的问题,请参考以下文章

python双曲线拟合

Python 怎么用曲线拟合数据

使用 Python 将 sigmoid 函数(“S”形曲线)拟合到数据中

python 拟合曲线并求参

tensorflow 曲线拟合

python应用 曲线拟合03