Python 在类中使用scipy.optimize.curve_拟合

Python 在类中使用scipy.optimize.curve_拟合,python,numpy,scipy,curve-fitting,class-method,Python,Numpy,Scipy,Curve Fitting,Class Method,我有一个描述数学函数的类。该类需要能够根据传入的数据进行最小二乘拟合。i、 e.您可以调用如下方法: classinstance.Fit(x,y) 它还调整其内部变量以最适合数据。我正在尝试使用scipy.optimize.curve_来拟合它,它需要我传入一个模型函数。问题是模型函数在类中,需要访问类的变量和成员来计算数据。但是,曲线拟合不能调用第一个参数为self的函数。是否有一种方法可以使曲线拟合使用类的方法作为其模型函数 以下是显示问题的最小可执行代码段: import numpy a

我有一个描述数学函数的类。该类需要能够根据传入的数据进行最小二乘拟合。i、 e.您可以调用如下方法:

classinstance.Fit(x,y)
它还调整其内部变量以最适合数据。我正在尝试使用scipy.optimize.curve_来拟合它,它需要我传入一个模型函数。问题是模型函数在类中,需要访问类的变量和成员来计算数据。但是,曲线拟合不能调用第一个参数为self的函数。是否有一种方法可以使曲线拟合使用类的方法作为其模型函数

以下是显示问题的最小可执行代码段:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# This is a class which encapsulates a gaussian and fits itself to data.
class GaussianComponent():
    # This is a formula string showing the exact code used to produce the gaussian.  I
    # It has to be printed for the user, and it can be used to compute values.
    Formula = 'self.Amp*np.exp(-((x-self.Center)**2/(self.FWHM**2*np.sqrt(2))))'

    # These parameters describe the gaussian.
    Center = 0
    Amp = 1
    FWHM = 1

    # HERE IS THE CONUNDRUM: IF I LEAVE SELF IN THE DECLARATION, CURVE_FIT
    # CANNOT CALL IT SINCE IT REQUIRES THE WRONG NUMBER OF PARAMETERS.
    # IF I REMOVE IT, FITFUNC CAN'T ACCESS THE CLASS VARIABLES.
    def FitFunc(self, x, y, Center, Amp, FWHM):
        eval('y - ' + self.Formula.replace('self.', ''))

    # This uses curve_fit to adjust the gaussian parameters to best match the
    # data passed in.
    def Fit(self, x, y):
        #FitFunc = lambda x, y, Center, Amp, FWHM: eval('y - ' + self.Formula.replace('self.', ''))
        FitParams, FitCov = curve_fit(self.FitFunc, x, y, (self.Center, self.Amp, self.FWHM))
        self.Center = FitParams[0]
        self.Amp = FitParams[1]
        self.FWHM = FitParams[2]

    # Give back a vector which describes what this gaussian looks like.
    def GetPlot(self, x):
        y = eval(self.Formula)
        return y

# Make a gausssian with default shape and position (height 1 at the origin, FWHM 1.
g = GaussianComponent()

# Make a space in which we can plot the gaussian.
x = np.linspace(-5,5,100)
y = g.GetPlot(x)

# Make some "experimental data" which is just the default shape, noisy, and
# moved up the y axis a tad so the best fit will be different.
ynoise = y + np.random.normal(loc=0.1, scale=0.1, size=len(x))

# Draw it
plt.plot(x,y, x,ynoise)
plt.show()

# Do the fit (but this doesn't work...)
g.Fit(x,y)
这会生成下面的图表,然后崩溃,因为模型函数在尝试拟合时不正确


提前谢谢

啊!这实际上是我代码中的一个bug。如果我更改此行:

def FitFunc(self, x, y, Center, Amp, FWHM):

那我们就没事了。所以曲线拟合确实正确地处理了自参数,但我的模型函数不应该包括y


(尴尬!)

我花了一些时间查看您的代码,结果不幸迟到了2分钟。无论如何,为了让事情变得更有趣,我对你们的课做了一些编辑,下面是我编造的:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

class GaussianComponent():

    def __init__(self, func, params=None):
        self.formula = func
        self.params = params

    def eval(self, x):
        allowed_locals = {key: self.params[key] for key in self.params}
        allowed_locals["x"] = x
        allowed_globals = {"np":np}
        return eval(self.formula, allowed_globals, allowed_locals)

    def Fit(self, x, y):
        FitParams, FitCov = curve_fit(self.eval, x, y, self.params)
        self.fitparams = fitParams


# Make a gausssian with default shape and position (height 1 at the origin, FWHM 1.
g = GaussianComponent("Amp*np.exp(-((x-Center)**2/(FWHM**2*np.sqrt(2))))", 
                      params={"Amp":1, "Center":0, "FWHM":1})

**SNIPPED FOR BREVITY**
我相信你也许会发现这是一个更令人满意的解决方案

当前,所有高斯参数都是类属性,这意味着如果您尝试使用不同的参数值创建类的第二个实例,您也将更改第一个类的值。通过将所有参数作为实例属性推送,您就可以摆脱它。这就是为什么我们首先要上课

您对
self
的问题源于您在
公式中编写
self
。现在你不必再这样了。我认为这样更有意义,因为当实例化类的对象时,可以向声明的函数添加任意多或任意小的参数。现在它甚至不必是高斯分布(不像以前)

只需将所有参数放到字典中,就像
curve\u fit
那样,然后忘记它们

通过明确说明
eval
可以使用什么,您可以帮助确保任何作恶者更难破解您的代码。尽管如此,这仍然是可能的,它始终与
eval
一起使用

祝你好运,问问你是否需要澄清一些事情

对我来说,
curve\u fit
正确地处理了。。。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

class GaussianComponent():

    def __init__(self, func, params=None):
        self.formula = func
        self.params = params

    def eval(self, x):
        allowed_locals = {key: self.params[key] for key in self.params}
        allowed_locals["x"] = x
        allowed_globals = {"np":np}
        return eval(self.formula, allowed_globals, allowed_locals)

    def Fit(self, x, y):
        FitParams, FitCov = curve_fit(self.eval, x, y, self.params)
        self.fitparams = fitParams


# Make a gausssian with default shape and position (height 1 at the origin, FWHM 1.
g = GaussianComponent("Amp*np.exp(-((x-Center)**2/(FWHM**2*np.sqrt(2))))", 
                      params={"Amp":1, "Center":0, "FWHM":1})

**SNIPPED FOR BREVITY**