解決したいこと
変更禁止の関数(下記のtarget_func2
)を直接的もしくは間接的にcurve_fitに渡して推定結果を得たいのですが、test_fit2
を実行すると以下のエラーが出ます。
ValueError: callable <ufunc '? (vectorized)'> is not supported by signature
target_func2
を一切変更せずにtest_fit1
のような結果を得るにはどうすればいいでしょうか。
サンプルデータはa=3.0
にして、そこからちょっとずれるように作っています。test_fit1
の結果は
(array([ 3.00033445]), array([[ 3.35566718e-05]]))
で想定通りでした。
Python
1from scipy import optimize 2import numpy as np 3 4 5def target_func1(x, a): 6 import scipy 7 return (scipy.exp(x) - 1.0)/x + a 8 9def test_fit1(): 10 xdata = np.arange(*[0.01, 3.0, 0.01]) #とりあえず動かすため0を含めない 11 ydata = np.array([target_func1(x, 3.0) + (-1.0)**i / 10.0 for i, x in enumerate(xdata)]) 12 print(optimize.curve_fit(target_func1, xdata, ydata, maxfev=1000)) 13 14 15def target_func2(x, a): 16 if abs(x) < 1.0e-6: 17 return 1 + x/2 + x**2/6 + x**3/24 + x**4/120 + a 18 19 import math 20 return (math.exp(x) - 1.0)/x + a 21 22def test_fit2(): 23 xdata = np.arange(*[0.0, 3.0, 0.01]) # 0を含む 24 ydata = np.array([target_func2(x, 3.0) + (-1.0)**i / 10.0 for i, x in enumerate(xdata)]) 25 univ_func = np.frompyfunc(target_func2, 2, 1) 26 print(optimize.curve_fit(univ_func, xdata, ydata, maxfev=1000))
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/10 13:25