2017-12-05 11 views
0

scipy.optimize.curve_fitの関数で、推定推測の推定値を入力しようとしています。 this link another linkscsi.optimize.curve_fitの引数 'x0'に複数の値がありました

によると、私はX0でそれらをdefindなければならない、しかし、私はさまざまな方法で試してみました、私は次のエラーを取得します。 (注:x0引数なしで正常に動作します)

TypeError:leastsq()は、引数 'x0'に複数の値を持っています。

私は再現例の下に提供されます。

import numpy as np 
import pandas as pd 
from sklearn.datasets import load_iris 
import scipy.optimize 
iris = load_iris() 
data1 = pd.DataFrame(data= np.c_[ iris['target'], iris['data']], columns= ['target'] + iris['feature_names']) 

def formula_nls(data, pot, sp): 
    return pot * np.tanh(data1.iloc[:,2] * sp/2) 

scipy.optimize.curve_fit(f = formula_nls, xdata= data1.iloc[:,1:], 
               ydata= data1.iloc[:,0], method = 'lm', 
              sigma = 1/data1.iloc[:,1], absolute_sigma=False, 
               x0 = np.ndarray([ 1, 2])) 

は、多分それは私が行方不明ですという単純なものです。 おかげで

+0

それは 'np.array'ない' np.ndarray'あ​​なたの助けのため – percusse

答えて

2

使用引数x0curve_fitのドキュメントは言うときなぜ:

p0 : None, scalar, or N-length sequence, optional

Initial guess for the parameters. If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).

は、これは、例えば、最小化してleast_squaresでAPIとは異なり後者について:

x0 : array_like with shape (n,) or float

Initial guess on independent variables. If float, it will be treated as a 1-d array with one element.

そして、はい、内部curve_fitに、あなたはp0becomesleast_squaresx0を与えられた:

res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs) 

x0が、私はそれが引数として扱われることを期待しcurve_fitの引数ではないので使用する:

kwargs

Keyword arguments passed to leastsq for method='lm' or least_squares otherwise.

これはiを意味しますtはからの電話番号とともにleastsqに、x0として渡されます。このような

何か:

def fun(x0, **kwargs): 
    return 1 

print(fun(1)) 
# 1 
print(fun(1, x0=3)) 
# TypeError: fun() got multiple values for argument 'x0' 
+1

おかげで、です。私は紛失しているものがあることを知っていた –

関連する問題