简体   繁体   English

为什么我的python lmfit minimumsq拟合函数传递了太多参数?

[英]Why is my python lmfit leastsq fitting function being passed too many arguments?

I've tried to search for someone making the same mistake as me, but have had no joy! 我试图寻找与我犯同样错误的人,但没有喜悦! It's also my 1st post, so I apologise if it's badly explained or directed. 这也是我的第一篇文章,因此,如果对它的解释或指示不正确,我深表歉意。 Advice welcome. 欢迎咨询。

The problem I am solving is: Finding the position of a receiver of some kind by measuring the distances to a number of known reference points. 我要解决的问题是:通过测量到多个已知参考点的距离来找到某种接收器的位置。 It's basic trilateration, and I am using least squares to do this. 这是基本的三边测量,我正在使用最小二乘法进行此操作。

I have successfully used the scipy.optimize lesatsq function already to do this, so I'm pretty sure my fitting function works. 我已经成功地使用了scipy.optimize lesatsq函数来执行此操作,因此,我很确定我的fit函数可以正常工作。 However, I want to be able to use some of the extra features that the lmfit Python package offers, and I'm struggling to translate it accross. 但是,我希望能够使用lmfit Python软件包提供的一些额外功能,并且我正努力将其翻译成各种语言。 Full attempt code is here: http://pastebin.com/4xbfkaCm 完整的尝试代码在这里: http : //pastebin.com/4xbfkaCm

The lmfit minimize function sets up calls to my fitting function, (which is called residualfunct) as follows, with stationarray being a numpy array containing station locations and ranges and params being a dictionary of parameters to be used in the fitting function lmfit最小化函数按如下所示设置对我的拟合函数的调用(称为residualfunct),其中stationarray是一个包含站点位置和范围的numpy数组,而params是要在拟合函数中使用的参数的字典

position = minimize(residualfunct, params, args=(stationarray))

and my fitting function is defined as 我的拟合函数定义为

def residualfunct(params, stationarray):

    X = params['solutionX'].value
    Y = params['solutionY'].value
    Z = params['solutionZ'].value
    result = numpy.array([s[3] - linalg.norm(array((X,Y,Z))-array((s[0],s[1],s[2]))) for s in stationarray])
    print result
    return result

When I run this code I get the TypeError: 当我运行此代码时,我收到TypeError:

residualfunct() takes exactly 2 arguments (5 given). minimumfunct()正好接受2个参数(给定5个)。

as this call is made by the minimize function, I don't see what control I have over this. 由于此调用是由Minimum函数进行的,因此我看不到对此有什么控制。 I can only imagine that this is being raised because I am passing an np.array as an argument, but this seems unavoidable. 我只能想象这是因为我正在传递np.array作为参数而引起的,但这似乎是不可避免的。 Can anyone see what I am doing wrong? 谁能看到我在做什么错?

Thanks 谢谢

In the call to minimize , change minimize ,变化的呼声中

args=(stationarray)

to

args=(stationarray,)

args must be a sequence (typically a tuple) whose elements are the arguments passed to the function. args必须是一个序列(通常是一个元组),其元素是传递给函数的参数。 To create a tuple of length 1, you need the extra comma. 要创建长度为1的元组,您需要额外的逗号。 When you write args=(stationarray) (without the extra comma), the parentheses have no effect, and it is the same as writing args=stationarray . 当您编写args=(stationarray) (不带逗号)时,括号无效,并且与编写args=stationarray相同。 minimize then calls the function as residualfunct(params, stationarray[0], stationarray[1], ...) , which results in the error that you reported. minimize然后将函数调用为residualfunct(params, stationarray[0], stationarray[1], ...) ,这将导致您报告错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM