2017-12-18 12 views
-3

私は関数Y = a + b *(x)^ cとxとyの実験値のリストを持っています。どうすればPythonでカーブフィッティングを行い、パラメータa、b、cの値を調べることができますか?Pythonでパラメータを見つけるための非線形回帰

x   y 
5.107  3.57 
15.593 4.09 
178.942 9.19 
351.23 14.3 
523.172 19.41 
1039.449 32.17 
+0

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html – Pavel

答えて

0

あなたはlmfithttps://lmfit.github.io/lmfit-py/)を使用し、モデル関数を定義することができ、

import numpy as np 
from lmfit import Model 
import matplotlib.pyplot as plt 

x = np.array((5.107, 15.593, 178.942, 351.23, 523.172, 1039.449)) 
y = np.array((3.57, 4.09, 9.19, 14.3, 19.41, 32.17)) 

def expfunc(x, scale, decay, offset): 
    "model exponential decay with offset" 
    return offset + scale*x**decay 

# create model from the above model function 
model = Model(expfunc) 

# create parameters with initial values, 
# using names from model function 
params = model.make_params(offset=0, scale=1, decay=1) 

# fit data 'y' to model with params 
result = model.fit(y, params, x=x) 

# print and plot result 
print(result.fit_report()) 
result.plot_fit() 
plt.show() 

として、このフィット感のためのフィットレポートはフィッティングの統計とのための最高のフィット値が、不確実性、および相関を与えるだろうと言いますパラメータ:

[[Model]] 
    Model(expfunc) 
[[Fit Statistics]] 
    # fitting method = leastsq 
    # function evals = 27 
    # data points  = 6 
    # variables  = 3 
    chi-square   = 0.181 
    reduced chi-square = 0.060 
    Akaike info crit = -15.015 
    Bayesian info crit = -15.640 
[[Variables]] 
    offset: 3.29036599 +/- 0.200622 (6.10%) (init= 0) 
    scale: 0.06290220 +/- 0.008912 (14.17%) (init= 1) 
    decay: 0.88280026 +/- 0.020216 (2.29%) (init= 1) 
[[Correlations]] (unreported correlations are < 0.100) 
    C(scale, decay)    = -0.997 
    C(offset, scale)    = -0.722 
    C(offset, decay)    = 0.686 

と、このようなプロットを生成:

enter image description here

FWIW、このモデル関数は、あなたがのように、式の文字列からモデルを構築するlmfit ExpressionModelを使用することができることを十分に単純です:

from lmfit.models import ExpressionModel 
model = ExpressionModel('offset + scale*x**decay') 

他のすべてと、上記と同じ。