我正在使用 pandas 读取 .csv 文件。然后,我从数据框中获取 x 和 y 对,并用于对数据symfit
执行全局拟合。我是熊猫数据框和symfit
. 我当前的概念验证代码适用于两个数据集,但我想以一种适用于从原始文件导入的许多数据集的方式编写它,这些数据集.csv
将始终采用相同的格式——列将总是成对的x
和y
格式的值x1, y1, x2, y2,
等。
我可以遍历数据框并提取单个数组x1, y1, x2, y2,
等吗?这是否违背了使用数据框的目的?
# creating the dataframe
from pandas import read_csv, Series, DataFrame, isnull
data_file = read_csv(filename, header=None, skiprows=2) # no data in first two rows--these contain information I use later on for plotting
# important note: data sets contain different numbers of points, so pandas reads in nan for any missing values.
X1 = Series(data_file[0]).values
X1 = x_1[~isnull(x_1)] # removes any nan values (up for any suggestions on a better way to do this. Other methods I have tried remove entire rows or columns that contain nan)
Y1 = Series(data_file[1]).values
Y1 = y_1[~isnull(y_1)]
X2 = Series(data_file[2]).values
X2 = x_2[~isnull(x_2)]
Y2 = Series(data_file[3]).values
Y2 = y_2[~isnull(y_2)]
# sample data
# X1 = [12.5, 6.7, 5, 3.1, 128, 47, 5, 3.1, 6.7, 12.5]
# Y1 = [280, 150, 127, 85, 400, 401, 110, 96, 131, 241]
# X2 = [75, 39, 10, 7.7, 19, 39, 75]
# Y2 = [296, 257, 141, 100, 181, 254, 324]
从这里我将 X 和 Y 传递给一个包含 symfit 模型和拟合函数的类。我不认为我可以连接 X 和 Y;我需要它们保持独立,因此 symfit 将为每个数据集拟合单独的曲线(具有四个共享参数)。
下面是我正在使用的模型。我可能会破坏 symfit 的语法。我仍在学习 symfit,但到目前为止一切都很好。这种拟合适用于两个数据集,我能够提取拟合参数并稍后绘制结果。
# This model assumes two data sets. I need to figure out how to fit as many as 10 data sets.
from symfit import parameters, variables, Fit, Model
fi_1 = 0 # These parameters change with each x,y pair. These will also be read from the original data file. I have them hard-coded here for ease.
fi_2 = 1
x_1, x_2, y_1, y_2 = variables('x_1, x_2, y_1, y_2')
vmax, km, evk, ev = parameters('vmax, km, evk, ev') # these are all shared
model = Model({
y_1: vmax * x_1 / (km * (1 + (fi_1 * evk)) + x_1 * (1 + (fi_1 * ev))),
y_2: vmax * x_2 / (km * (1 + (fi_2 * evk)) + x_2 * (1 + (fi_2 * ev)))})
fit = Fit(model, x_1=X1, x_2=X2, y_1=Y1, y_2=Y2)
fit_result = fit.execute()
问题总结:我可以同时安装多达 10 个 x,y 对。是否有一种干净的方法来遍历数据帧,这样我就可以避免对传递给 symfit 的 x 和 y 数组进行硬编码?