Python / curve_fit:无法通过带有初始猜测的数组 [英] Python/curve_fit: cannot pass array with init guess
问题描述
我具有此函数来计算某种多项式:
I have this function to compute some sort of polynomial:
def pipoly(df,pj):
n=np.size(pj)
p=pj[0]
for j in range(1,n):
p+=pj[j]*df**j
return p
pj
应该是一个数组包含多项式系数的初始猜测;因此,多项式的阶数由第一行中的函数本身确定。 df
是标量变量。此函数传递给scipy.optimize的 curve_fit
为
pj
is supposed to be an array that contains the initial guesses of the coefficients of the polynomial; the degree of the polynomial is hence determined by the function itself in the first line. df
is a scalar variable. This function is passed to scipy.optimize's curve_fit
as
parfit,covfig=curve_fit(pipoly,[f-f0[j] for f in f_df[if0[j]:if0[i]]],
pmode_xp[ph][if0[j]:if0[i]],
p0=([pmode0[ph][-1],(pmode_xp[ph][if0[i]]-pmode_xp[ph][if0[j]])/df]))
函数名称后的前两个参数是数组(二维数组的一维切片),我已经确认它们具有相同的长度。在 pipoly
之后的第三个参数应该是一个元组,它带有 pj
的初始猜测,我之前已经打印了出来: [0.4586590267346888,0.7419930843896957]
。那么,为什么Python会抱怨 TypeError:pipoly()接受2个位置参数,但是却给了3个
?而且,如果我删除了 p0
参数,我会被告知 pj
被视为标量,因此不能索引。我该如何清楚地暗示 pj
是一个数组?
The first two arguments after the name of the function are arrays (1D slices of 2D arrays), and I have confirmed that they have the same length. The third argument after pipoly
is supposed to be a tuple with the initial guesses for pj
, which I printed out before: [0.4586590267346888, 0.7419930843896957]
. So why is Python complaining that TypeError: pipoly() takes 2 positional arguments but 3 were given
? And if I remove the p0
argument, I'm told that the pj
is considered a scalar and can therefore not have an index. How do I make it clear to pipoly that pj
is to be an array?
推荐答案
您的语句:
pj
应该是一个数组包含
多项式的系数;
pj
is supposed to be an array that contains the coefficients of the polynomial;
是错误的。根据 curve_fit()文档:
scipy.optimize.curve_fit(f,xdata,ydata,p0 = None,sigma = None,absolute_sigma = False,check_finite = True,bounds =(-inf,inf),方法=无,jac =无,** kwargs)[源代码]使用非线性最小二乘法将函数f拟合到数据。
scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(-inf, inf), method=None, jac=None, **kwargs)[source] Use non-linear least squares to fit a function, f, to data.
假设 ydata = f(xdata,* params)+ eps
这意味着要由 curve_fit()
使用的 pipoly()
函数取数量等于您多项式的参数数量加一个(变量,这是第一个参数)的参数。
错误:
That means that your pipoly()
function, to be used by curve_fit()
must take a number of arguments equal to the number of parameters of you polynomial plus one (the variable, which is the first argument).
The error:
TypeError:pipoly()接受2个位置参数,但给了3个位置参数?
TypeError: pipoly() takes 2 positional arguments but 3 were given?
告诉您 pipoly
会收到3个参数,因为您可能正在测试线性政治学,因此这3个参数分别是自变量和2个参数( [f-f0 [ j]表示f_df [if0 [j0:if0 [i]]]
中的f是2个长度的列表)。
编写时,它需要2
is telling you that pipoly
receives 3 arguments because you probably were testing a linear polinomyal, so the three arguments were the independent variable and two parameter (the [f-f0[j] for f in f_df[if0[j]:if0[i]]]
stuff is a 2-length list).
As you write it, instead it takes 2 argument only.
您可以通过在 pj
之前添加星号来轻松解决问题:
You can easily solve your problem by adding an asterisk before pj
:
def pipoly(df,*pj):
n=len(pj) #len() is sufficient here, but np.size() works too.
p=pj[0]
for j in range(1,n):
p+=pj[j]*df**j
return p
这样,您的函数将接受可变数量的参数。 此处详细说明其含义和在Python函数参数中使用星号。
This way your function accepts a variable number of arguments. Here more on the meaning and the use of the asterisk in python function parameters.
这篇关于Python / curve_fit:无法通过带有初始猜测的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!