gpt4 book ai didi

python - 如何执行多条曲线的联合拟合(在 Python 中)?

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:01 31 4
gpt4 key购买 nike

假设我通过简单的线性回归拟合一些数据点。现在我想对几组数据点执行几个联合线性回归。更具体地说,我希望一个参数在所有拟合中都相等,这里示意性地描绘了 y 轴交点。

y-axis intersection

在 Google 上搜索了一段时间后,我既找不到任何执行此操作的 Python (Scipy) 例程,也找不到任何一般文献来说明如何完成此操作。

理想情况下,我不仅希望在简单线性回归的情况下执行这些联合拟合,而且还希望对更一般的拟合函数(例如,幂律拟合与联合指数)执行这些联合拟合。

最佳答案

lmfit模块允许您执行此操作,如他们的 FAQ 中所述:

from lmfit import minimize, Parameters, fit_report
import numpy as np

# residual function to minimize
def fit_function(params, x=None, dat1=None, dat2=None):

model1 = params['offset'] + x * params['slope1']
model2 = params['offset'] + x * params['slope2']

resid1 = dat1 - model1
resid2 = dat2 - model2
return np.concatenate((resid1, resid2))

# setup fit parameters
params = Parameters()
params.add('slope1', value=1)
params.add('slope2', value=-1)
params.add('offset', value=0.5)

# generate sample data
x = np.arange(0, 10)
slope1, slope2, offset = 1.1, -0.9, 0.2
y1 = slope1 * x + offset
y2 = slope2 * x + offset

# fit
out = minimize(residual, params, kws={"x": x, "dat1": y1, "dat2": y2})
print(fit_report(out))
# [[Fit Statistics]]
# # fitting method = leastsq
# # function evals = 9
# # data points = 20
# # variables = 3
# chi-square = 1.4945e-31
# reduced chi-square = 8.7913e-33
# Akaike info crit = -1473.48128
# Bayesian info crit = -1470.49408
# [[Variables]]
# slope1: 1.10000000 +/- 8.2888e-18 (0.00%) (init = 1)
# slope2: -0.90000000 +/- 8.2888e-18 (0.00%) (init = -1)
# offset: 0.20000000 +/- 3.8968e-17 (0.00%) (init = 0.5)
# [[Correlations]] (unreported correlations are < 0.100)
# C(slope1, offset) = -0.742
# C(slope2, offset) = -0.742
# C(slope1, slope2) = 0.551

关于python - 如何执行多条曲线的联合拟合(在 Python 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53728682/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com