Python 我如何解决这个问题'';名称未定义'';

Python 我如何解决这个问题'';名称未定义'';,python,lmfit,Python,Lmfit,我是python新手我有这段代码我想使用子类lmfit.models并实现一个猜测方法 class DecayingSineModel(): def __init__(self, *args, **kwargs): def decaying_sine(self, x, ampl, offset, freq, x0, tau): return ampl * np.sin((x - x0)*freq) * np.exp(-x/tau) + off

我是python新手我有这段代码我想使用子类lmfit.models并实现一个猜测方法

class DecayingSineModel():

     def __init__(self, *args, **kwargs):

        def decaying_sine(self, x, ampl, offset, freq, x0, tau):
            return ampl * np.sin((x - x0)*freq) * np.exp(-x/tau) + offset

        super(DecayingSineModel, self).__init__(decaying_sine, *args, **kwargs)

    def pset(param, value):
        params["%s%s" % (self.prefix, param)].set(value=value)

    def guess(self, data, **kwargs):         
        params = self.make_params()
        pset("ampl", np.max(data) - np.min(data))
        pset("offset", np.mean(data))
        pset("freq", 1)
        pset("x0", 0)
        pset("tau", 1)
        return lmfit.models.update_param_vals(params, self.prefix, **kwargs)

sp = DecayingSineModel()
params = sp.guess(y, x=x)
fit = sp.fit(y, params, x=x)
我收到以下错误 我收到的错误
您几乎肯定希望您的
衰减正弦波
继承自
lmfit.Model
。正如其他人所指出的,您的代码还存在许多其他问题,包括您的
pset
引用了
self
,但没有传入它,而您调用的是
pset
,而不是
self.pset
。您的模型函数
衰减正弦
不应具有
自身

已清理版本:

import numpy as np
import lmfit
import matplotlib.pyplot as plt

class DecayingSineModel(lmfit.Model):
    def __init__(self, *args, **kwargs):
        def decaying_sine(x, ampl, offset, freq, x0, tau):
            return ampl * np.sin((x - x0)*freq) * np.exp(-x/tau) + offset
        super(DecayingSineModel, self).__init__(decaying_sine, *args, **kwargs)

    def guess(self, data, x=None, **kwargs):
        ampl = np.max(data) - np.min(data)
        offset = np.mean(data)
        params = self.make_params(ampl=ampl, offset=offset, freq=1, x0=0, tau=1)
        return lmfit.models.update_param_vals(params, self.prefix, **kwargs)

sp = DecayingSineModel()

x = np.linspace(0, 25, 201)
noise = np.random.normal(size=len(x), scale=0.25)
y = 2 + 7*np.sin(1.6*(x-0.2)) * np.exp(-x/18) + noise

params = sp.guess(y, x=x)
result = sp.fit(y, params, x=x)
print(result.fit_report())

plt.plot(x, y, 'bo')
plt.plot(x, result.best_fit, 'r-')
plt.show()
报告:

[[Model]]
    Model(decaying_sine)
[[Fit Statistics]]
    # function evals   = 83
    # data points      = 201
    # variables        = 5
    chi-square         = 39.266
    reduced chi-square = 0.200
    Akaike info crit   = -318.220
    Bayesian info crit = -301.703
[[Variables]]
    ampl:     6.92483967 +/- 0.123863 (1.79%) (init= 12.59529)
    offset:   1.96307863 +/- 0.031684 (1.61%) (init= 2.139916)
    freq:     1.60060819 +/- 0.001775 (0.11%) (init= 1)
    x0:       0.19650313 +/- 0.010267 (5.23%) (init= 0)
    tau:      18.3528781 +/- 0.614576 (3.35%) (init= 1)
[[Correlations]] (unreported correlations are <  0.100)
    C(ampl, tau)                 = -0.781 
    C(freq, x0)                  =  0.750
[[Model]]
模型(衰减正弦)
[[Fit统计数据]]
#函数evals=83
#数据点=201
#变量=5
卡方检验=39.266
缩减卡方=0.200
Akaike信息临界值=-318.220
贝叶斯信息临界值=-301.703
[[变量]]
平均值:6.92483967+/-0.123863(1.79%)(初始值=12.59529)
偏移量:1.96307863+/-0.031684(1.61%)(初始值=2.139916)
频率:1.60060819+/-0.001775(0.11%)(初始值=1)
x0:0.19650313+/-0.010267(5.23%)(初始值=0)
τ:18.3528781+/-0.614576(3.35%)(初始值=1)
[[相关性]](未报告的相关性<0.100)
C(ampl,tau)=-0.781
C(频率,x0)=0.750

这就是您缩进代码的方式,因为这可以解释它。请发布代码的精确副本,因为它现在非常奇怪…调用
super
是否在
\uuu init\uu
方法中?由于压痕破损,很难说出来。类似地,
sp=DecayingSineModel()
是否在类主体中?还是在课堂之外?投票结束。在不知道OP使用的确切缩进的情况下,我们只能猜测错误来自何处。您说希望“将lmfit.models子类化”,但您的
声明没有提到lmfit。我还想知道为什么在这个软件的介绍中,你不使用简单的模式来描述模型。是的,谢谢,但在这样做之后,我又收到了这个错误。嗯,我怀疑:当我尝试你的代码时,我没有得到NameError错误。但是,正如其他人所指出的,您的代码还有其他几个问题。我更新了我的答案以提供一个工作版本。@M Newville感谢您的回复,我不知道为什么即使在您的工作版本中我也会收到此错误,可能还有另一个问题,我正在widows 7中使用Python 2.7.14,是否有可能正因为如此,我遇到了此问题。发布完整、准确的代码,显示问题和完整,确切的错误消息。我只使用了你的工作版本,并且更新了我的错误图像