【问题标题】:TypeError: Can not understand类型错误:无法理解
【发布时间】:2020-02-01 00:30:22
【问题描述】:

我正在拟合一条非常简单的三点曲线。使用 minimumsq 方法,遵循所有规则。但我仍然收到错误。我不明白。任何人都可以帮忙。非常感谢

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq

x = np.array([2.0,30.2,15.0])
y = np.array([45.0,56.2,30.0])

print(x)
print(y)

# model
def t(x,a,b,c):
    return a*x**2 + b*x + c

#residual fucntion
def residual_t(x,y,a,b,c):
    return y-t(x,a,b,c)


#initial parameters
g0 = np.array([0.0,0.0,0.0])

#leastsq method
coeffs, cov = leastsq(residual_t, g0, args=(x,y))
plt.plot(x,t(x,*coeffs),'r')
plt.plot(x,y,'b')
plt.show()

#finding out Rsquared and Radj squared value
absError = residual_t(y,x,*coeffs)
se = np.square(absError) # squared errors
Rsquared = 1.0 - (np.var(absError) / np.var(y))
n = len(x)
k = len(coeffs)
Radj_sq = (1-((1-Rsquared)/(n-1)))/(n-k-1)
print (f'Rsquared value: {Rsquared}   adjusted R saquared value: {Radj_sq}')

TypeError:residual_t() 缺少 2 个必需的位置参数:'b' 和 'c'

为什么? coeffs 已经是一个数组,其中包含 a、b、c 的最佳 it 值。 coeffs 也显示未定义,residual_t 也显示问题。你能帮我理解吗。

【问题讨论】:

  • 也许你的意思是residual_t(y,x,*coeffs)?。正如您定义的那样,该函数接受 5 个参数,但您只传入 3 个。
  • 函数有 5 个参数,你只传递了 3 个。为什么??
  • *coeffs 本身包含最佳拟合 a,b,c。我不能通过任何其他的
  • Esha,pault 是正确的,添加星号。在线absError = residual_t(y,x,*coeffs)
  • 仍然有同样的错误

标签: python numpy scipy least-squares


【解决方案1】:

复制并粘贴您的代码(包括 *coeffs 更改),我明白了

1135:~/mypy$ python3 stack58206395.py 
[ 2.  30.2 15. ]
[45.  56.2 30. ]
Traceback (most recent call last):
  File "stack58206395.py", line 24, in <module>
    coeffs, cov = leastsq(residual_t, g0, args=(x,y))
  File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 383, in leastsq
    shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
  File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c'

那是在leastsq调用中使用residual_t的错误。

如果我添加

residual_t(g0, x, y)

g0 定义之后,我得到了同样的错误:

1136:~/mypy$ python3 stack58206395.py 
[ 2.  30.2 15. ]
[45.  56.2 30. ]
Traceback (most recent call last):
  File "stack58206395.py", line 23, in <module>
    residual_t(g0, x, y)
TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c'

所以你需要定义residual_t 来处理这样的调用。我不会猜测你真正想要什么,所以我会留给你解决问题。

请记住,residual_t 将与 x0 一起调用,并与 args 元组拼接。这是scipy.optimize 函数的典型用法。如有必要,请查看文档。

编辑

定义函数为:

def residual_t(abc, x, y):
    a,b,c = abc
    return y-t(x,a,b,c)

运行没有错误。

【讨论】:

  • 我不明白你的回答。据我所知 scipy 函数 leastsq,它需要一个函数、初始猜测和 x,y 参数。这三件事是必须的。从数组初始猜测它获取值并替换函数的参数。如果我错了,请检查我。在我的情况下,g0 是我的猜测数组,其中包含 a、b、c 参数的值。我不明白为什么会出现类型错误,说只缺少 b 和 c 的值。
  • 在 Python 中,像 (x,y,a,b,c) 这样的参数列表是位置性的。 leastsq 正在超越 (g0, x, y),前 3 个位置。 b,c 是空缺职位。您应该定义 residual_t(abc, x, y) 以匹配 scipy 调用约定。
  • 谢谢大家..它现在正在工作。但我不明白这个概念。如果有人能解释一下,我将不胜感激。
  • leastsq 是一个需要残差函数的函数,初始猜测变量的第一个值将存储在哪里,并且 args=(x,y) 您的数据。为什么 scipy 将存储在 g0 中的值作为一个值,而在同一个 g0 中存在三个值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多