python - python全局拟合不同大小的数据集
问题描述
我想知道是否有一种方法可以使用 LMfit 将具有不同大小的数据集与具有一些共享和一些独立参数的相同模型拟合。
此链接是我发现的最接近类似问题的链接,但它假定所有 y 的 x 相同。
感谢所有建议和意见
解决方案
根据您的评论,一个 scipy 示例是可以的:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
y1 = np.array([ 16.00, 18.42, 20.84, 23.26])
y2 = np.array([-20.00, -25.50, -31.00, -36.50, -42.00])
comboY = np.append(y1, y2)
x1 = np.array([5.0, 6.1, 7.2, 8.3])
x2 = np.array([15.0, 16.1, 17.2, 18.3, 19.4])
comboX = np.append(x1, x2)
if len(y1) != len(x1):
raise(Exception('Unequal x1 and y1 data length'))
if len(y2) != len(x2):
raise(Exception('Unequal x2 and y2 data length'))
def function1(data, a, b, c): # not all parameters are used here, c is shared
return a * data + c
def function2(data, a, b, c): # not all parameters are used here, c is shared
return b * data + c
def combinedFunction(comboData, a, b, c):
# single data reference passed in, extract separate data
extract1 = comboData[:len(x1)] # first data
extract2 = comboData[len(x1):] # second data
result1 = function1(extract1, a, b, c)
result2 = function2(extract2, a, b, c)
return np.append(result1, result2)
# some initial parameter values
initialParameters = np.array([1.0, 1.0, 1.0])
# curve fit the combined data to the combined function
fittedParameters, pcov = curve_fit(combinedFunction, comboX, comboY, initialParameters)
# values for display of fitted function
a, b, c = fittedParameters
y_fit_1 = function1(x1, a, b, c) # first data set, first equation
y_fit_2 = function2(x2, a, b, c) # second data set, second equation
plt.plot(comboX, comboY, 'D') # plot the raw data
plt.plot(x1, y_fit_1) # plot the equation using the fitted parameters
plt.plot(x2, y_fit_2) # plot the equation using the fitted parameters
plt.show()
print('a, b, c:', fittedParameters)
推荐阅读
- elasticsearch - 从 Elastic Search 中过滤掉响应的最佳方法
- node.js - 如何根据引用的子文档值过滤 MongoDB/Mongoose 查询
- azure - Visual Studio 2019 Azure Analysis Services 表格模型部署问题
- postgresql - 如何解决重新创建 pg_logical 订阅的问题?
- scala - Scala 中的伴随对象属性初始化
- laravel - Laravel 中的提醒日期存储错误的创建日期
- java - Mongo DB 更新选项 | 在路径“Display.$[id].DpDay.$[day].note”中找不到标识符“id”的数组过滤器
- bash - VSCode:Code Runner 扩展无法在 git bash 终端上执行代码?
- java - 选择指定的 JTable 行然后播放声音?
- python - Python数组:为什么输出3