2017-12-18 11 views
-3

저는 함수 Y = a + b * (x)^c와 x와 y의 실험 값리스트를 가지고 있습니다. 파이썬에서 어떻게 커브 피팅을 수행하고 매개 변수 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

당신은 lmfit (https://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 

이 같은 음모 생산 :

from lmfit.models import ExpressionModel 
model = ExpressionModel('offset + scale*x**decay') 
모든 사람과

상기와 같은 :

enter image description here

FWIW,이 모델의 기능은와 같이 표현의 문자열에서 모델을 구축하기 위해 lmfit ExpressionModel을 사용할 수있을만큼 간단합니다 .