我有一个NeoprobeApp.py
调用拟合函数的python脚本,Parameter.py
Parameter.py
如下所示
from scipy import optimize
import numpy as np
class Parameter:
def __init__(self, value):
self.value = value
def set(self, value):
self.value = value
def __call__(self):
return self.value
def fit(function, parameters, y, x = None):
def f(params):
i = 0
for p in parameters:
p.set(params[i])
i += 1
return y - function(x)
if x is None: x = arange(y.shape[0])
p = [param() for param in parameters]
optimize.leastsq(f, p)
我成功导入了 fit 函数from Parameter import fit
。但是,当我尝试初始化我的参数时
# Define initial parameters of Gaussian fit
mu = Parameter(0)
sigma = Parameter(20)
height = Parameter(1)
#define Gaussian fit function
def f(angles): return height() * exp(-((angles-mu())/sigma())**2)
fit(f, [mu, sigma, height], n_col_cnts)
我收到错误消息
Traceback (most recent call last):
File "NeoprobeApp.py", line 228, in OnPlot
mu = Parameter(0)
NameError: global name 'Parameter' is not defined
我究竟做错了什么?