【问题标题】:How to fix "TypeError: f() takes 3 positional arguments when 4 were given error" when fitting a curve拟合曲线时如何解决“TypeError:f() 需要 3 个位置参数时给出 4 个错误”
【发布时间】:2019-04-08 01:29:44
【问题描述】:

我试图找到 x-offset 高斯函数,当与一组数据进行卷积(使用 numpy.convolve)时,该函数适合另一组数据。然而,当我尝试对我的数据进行这一系列复杂的操作时,它会给我一个关于参数数量的错误。

我有 5 个信号,它们的幅度记录在稍微不同的日期。理论上,后面的 4 个信号(g、r、i 和 z)在与我建模为高斯曲线的某个卷积函数卷积后可以建模为等于第一个信号 u。

我试图找到参数 a mu 和 sigma 来描述 4 个信号卷积中的每一个的高斯。我这样做是通过对第一个信号 u 进行插值以生成更多描述它的点,将这些插值通过与未知高斯的卷积,然后使用 scipy curve_fit 函数找到最适合 4 个记录信号中的每一个的参数。

我尝试过以各种方式嵌套我的函数,我尝试将所有函数放入一个命令中,而不是调用我自己定义的函数,这是我最接近成功执行此任务的方法。

#defined the gaussian function with the 3 parameters a mu and sig to be found. 
#test_x is a numpy.linspace array with numbers from -10 to 10.
def gauss(a, mu, sig):
    return a * np.exp(-np.power(test_x - mu, 2.) / (2 * np.power(sig, 2.)))

#defined the convolution of the callable interpolated u data (interpu) with 
#the defined gaussian function gauss
def gaussconv(a, mu, sig):
    return np.convolve(interpu, gauss(a, mu, sig))

#defined a function that should in theory model the convolved interpolation 
#and gaussian against the inputted x data datex and magnitude dana normx, with 
#the initial guesses for a mu and sig being 0.15, 1, and 0.5 respectively
def fit(datex, normx):
    return spo.curve_fit(gaussconv, datex, normx, p0=[0.15,1,0.5])

理想情况下,它应该输出计算出的 mu 和 sig 值,这些值最适合与记录数据的卷积,但我收到此错误消息:

TypeError: gaussconv() takes 3 positional arguments but 4 were given. 

任何帮助将不胜感激。

【问题讨论】:

  • edit 并添加完整的错误消息
  • def gaussconv(a, mu, sig, arg4): 中添加第四个参数并显示所有参数以查看您得到的结果。也许它会帮助你。

标签: python numpy scipy curve-fitting


【解决方案1】:

curve_fit() 将数据和三个参数(总共 4 个项目)传递给 gaussconv()。 gaussconv() 的第一个参数应该是数据。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2021-05-06
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多