2016-03-01 82 views
0

我想在代碼中使用PyQt-Fit庫,但遇到了麻煩。只是爲了試驗,我從包裝主頁複製了示例代碼。pyqt_fit.CurveFitting示例拋出錯誤

這裏是我運行代碼:

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) 

Example code from the docs給出了同樣的錯誤。

我試着用谷歌搜索我的問題,但我找不到一個工作的例子。

我需要更改以正確傳遞所有參數?

回答

3

我可以通過查看PyQt-Fit 1.2源代碼來了解簽名如何在版本1.2和1.3之間更改。老CurveFitting.__init__()是這樣的:

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): 

new one看起來是這樣的:

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 

可以看出,它現在預計,除了外部數據和YDATA一切都被命名爲參數,而先前p0fct未命名。 [這是明顯地從文檔中缺少]

例子中的函數調用真的應該是這樣的:

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' 

它似乎想讓我在試圖繪製它之前調用返回的fit()函數。我只是將print(fit(x))行的順序與這一行切換,並解決了這個問題。

工作的代碼現在是:

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, p0=(0,1,0), function=fct) 
print(fit(x)) # Display the estimated values1 
result = plot_fit.fit_evaluation(fit, x, y) 
plot_fit.plot1d(result) 
pylab.show() 
+0

自我回答,因爲它是一個真正的問題,我花了一段時間試圖解決,並希望把該解決方案在那裏。 – Scimonster