Python pyqt_fit.curvefiting示例抛出错误

Python pyqt_fit.curvefiting示例抛出错误,python,pyqt,Python,Pyqt,我试图在代码中使用库,但遇到了麻烦。为了进行实验,我从软件包主页复制了示例代码 以下是我正在运行的代码: import pyqt_fit from pyqt_fit import plot_fit import numpy as np from matplotlib import pylab x = np.arange(0,3,0.01) y = 2*x + 4*x**2 + np.random.randn(*x.shape) def fct(params, x): (a0, a1, a

我试图在代码中使用库,但遇到了麻烦。为了进行实验,我从软件包主页复制了示例代码

以下是我正在运行的代码:

import pyqt_fit
from pyqt_fit import plot_fit
import numpy as np
from matplotlib import pylab
x = np.arange(0,3,0.01)
y = 2*x + 4*x**2 + np.random.randn(*x.shape)
def fct(params, x):
    (a0, a1, a2) = params
    return a0 + a1*x + a2*x*x
fit = pyqt_fit.CurveFitting(x, y, (0,1,0), fct)
result = plot_fit.fit_evaluation(fit, x, y)
print(fit(x)) # Display the estimated values
plot_fit.plot1d(result)
pylab.show()
这就是我得到的错误:

    fit = pyqt_fit.CurveFitting(x, y, (0,1,0), fct)

TypeError: __init__() takes exactly 3 arguments (5 given)
给出了相同的错误

我试着用谷歌搜索我的问题,但找不到一个有效的例子


我需要更改什么才能正确传递所有参数?

我可以通过查看PyQt Fit 1.2源代码来解决这个问题,查看签名在版本1.2和1.3之间是如何更改的。旧的
曲线设置。\uuuu init\uuuu()
如下所示:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
下面的示例如下所示:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
可以看出,它现在希望除了扩展数据和ydata之外的所有数据都被命名为参数,而以前的
p0
fct
都没有命名。[文件中明显缺少这一点。]

示例中的函数调用应该是这样的:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
至少对我来说,它仍然会在下一行抛出一个错误:

 File "untitled0.py", line 18, in <module>
    result = plot_fit.fit_evaluation(fit, x, y)

  File "pyqt_fit/plot_fit.py", line 165, in fit_evaluation
    popt = fit.popt

AttributeError: 'CurveFitting' object has no attribute 'popt'

我可以通过查看PyQt Fit 1.2源代码来解决这个问题,查看签名在版本1.2和1.3之间是如何变化的。旧的
曲线设置。\uuuu init\uuuu()
如下所示:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
下面的示例如下所示:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
可以看出,它现在希望除了扩展数据和ydata之外的所有数据都被命名为参数,而以前的
p0
fct
都没有命名。[文件中明显缺少这一点。]

示例中的函数调用应该是这样的:

def __init__(self, xdata, ydata, p0, fct, args=(), residuals=None,
             fix_params=(), Dfun=None, Dres = None, col_deriv=1,
             constraints = None, *lsq_args, **lsq_kword):
def __init__(self, xdata, ydata, **kwords):
    self._fct = None
    self._Dfun = None
    self._residuals = None

   # snip...

    self.xdata = xdata
    self.ydata = ydata

    for n in kwords:
        setattr(self, n, kwords[n])

# getters and setters for all the other properties
fit = pyqt_fit.CurveFitting(x, y, p0=(0,1,0), function=fct)
至少对我来说,它仍然会在下一行抛出一个错误:

 File "untitled0.py", line 18, in <module>
    result = plot_fit.fit_evaluation(fit, x, y)

  File "pyqt_fit/plot_fit.py", line 165, in fit_evaluation
    popt = fit.popt

AttributeError: 'CurveFitting' object has no attribute 'popt'

自我回答是因为这是一个真正的问题,我花了一段时间试图解决,并想把解决方案摆出来。自我回答是因为这是一个真正的问题,我花了一段时间试图解决,并想把解决方案摆出来。