Python中具有折断幂定律的曲线拟合 [英] Curve fitting with broken power law in Python

查看:63
本文介绍了Python中具有折断幂定律的曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循并重复使用@ThePredator某人建议的一段代码(带有我自己的数据)(由于我目前不具备50的声誉,所以我无法对此线程发表评论).完整的代码如下:

Im trying to follow and re-use a piece of code (with my own data) suggested by someone named @ThePredator (I couldn't comment on that thread since I don't currently have the required reputation of 50). The full code is as follows:

import numpy as np # This is the Numpy module
from scipy.optimize import curve_fit # The module that contains the curve_fit routine
import matplotlib.pyplot as plt # This is the matplotlib module which we use for plotting the result

""" Below is the function that returns the final y according to the conditions """

def fitfunc(x,a1,a2):
    y1 = (x**(a1) )[x<xc]
    y2 = (x**(a1-a2) )[x>xc]
    y3 = (0)[x==xc]
    y = np.concatenate((y1,y2,y3))
    return y

x = array([0.001, 0.524, 0.625, 0.670, 0.790, 0.910, 1.240, 1.640, 2.180, 35460])
y = array([7.435e-13, 3.374e-14, 1.953e-14, 3.848e-14, 4.510e-14, 5.702e-14, 5.176e-14, 6.0e-14,3.049e-14,1.12e-17])

""" In the above code, we have imported 3 modules, namely Numpy, Scipy and  matplotlib """

popt,pcov = curve_fit(fitfunc,x,y,p0=(10.0,1.0)) #here we provide random initial parameters a1,a2

a1 = popt[0] 
a2 = popt[1]
residuals = y - fitfunc(x,a1,a2)
chi-sq = sum( (residuals**2)/fitfunc(x,a1,a2) ) # This is the chi-square for your fitted curve

""" Now if you need to plot, perform the code below """
curvey = fitfunc(x,a1,a2) # This is your y axis fit-line

plt.plot(x, curvey, 'red', label='The best-fit line')
plt.scatter(x,y, c='b',label='The data points')
plt.legend(loc='best')
plt.show()

我在运行此代码时遇到问题,我得到的错误如下:

Im having some problem running this code and the errors I get are as follows:

y3 =(0)[x == xc]

y3 = (0)[x==xc]

TypeError:"int"对象没有属性" getitem "

TypeError: 'int' object has no attribute 'getitem'

还有:

xc未定义

我没有看到代码中缺少任何内容(不必定义xc吗?).

I don't see anything missing in the code (xc shouldn't have to be defined?).

作者(@ThePredator)或其他对此有所了解的人可以帮助我确定我没有看到的内容.

Could the author (@ThePredator) or someone else having knowledge about this please help me identify what i haven't seen.

  • 新版本的代码:

  • New version of code:

import numpy as np # This is the Numpy module
from scipy.optimize import curve_fit 
import matplotlib.pyplot as plt 

def fitfunc(x, a1, a2, xc):
    if x.all() < xc:
      y = x**a1
    elif x.all() > xc:
      y = x**(a1 - a2) * x**a2
    else:
      y = 0
    return y

xc = 2
x = np.array([0.001, 0.524, 0.625, 0.670, 0.790, 0.910, 1.240, 1.640, 2.180, 35460])
y = np.array([7.435e-13, 3.374e-14, 1.953e-14, 3.848e-14, 4.510e-14, 5.702e-14, 5.176e-14, 6.0e-14,3.049e-14,1.12e-17])

popt,pcov = curve_fit(fitfunc,x,y,p0=(1.0,1.0)) 

a1 = popt[0] 
a2 = popt[1]
residuals = y - fitfunc(x, a1, a2, xc)
chisq = sum((residuals**2)/fitfunc(x, a1, a2, xc)) 
curvey = [fitfunc(val, a1, a2, xc) for val in x] #  y-axis fit-line

plt.plot(x, curvey, 'red', label='The best-fit line')
plt.scatter(x,y, c='b',label='The data points')
plt.legend(loc='best')
plt.show()

推荐答案

Hi执行以下操作定义您的函数,它将解决.x是一个数组(或列表),并且应返回y作为数组(或列表).然后可以在curvefit中使用它.

Hi Do the following to define your function, and it will solve. x is an array (or list) and it should return y as an array (or list). And then you can use it in curvefit.

def fit_function(x, a1, a2, xc):
    y = []
    for xx in x:
        if xx<xc:
            y.append(x**a1)
        elif xx>xc:
            y.append(x**(a1 - a2) * x**a2)
        else:
            y.append(0.0)
    return y   

这篇关于Python中具有折断幂定律的曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆