0

例如我有数据:

x    y1    y2    y1
---------------------
1    5     8    -
2    4     -    4
3    7     7    10
4    9     4    12
5    10    -    20
6    15    -    21

其中 x 是 x 轴,y1, y2, y3 是三个不同的数据集,它们被拟合在一起。

为简单起见,以下是拟合代码的简化版本:

def gauss_dataset(params, i, x):
    """calc gaussian from params for data set i
    using simple, hardwired naming convention"""
    x = params['x_%i' % (i+1)].value
    y = params['x_%i' % (i+1)].value
    return x + y

def objective(params, x, data):
    """ calculate total residual for fits to several data sets held
    in a 2-D array, and modeled by Gaussian functions"""
    ndata, nx = data.shape
    resid = 0.0*data[:]
    # make residual per data set
    for i in range(ndata):
        resid[i, :] = data[i, :] - gauss_dataset(params, i, x)
    # now flatten this to a 1D array, as minimize() needs
    return resid.flatten()

x  = np.linspace(0, 50, 50)
data = []

...

# Rearange data
for col in range(0, data_sets):
    for row in range (0, size_rows):
        data[col][row] = intens[row][col+1]

# create 5 sets of parameters, one per data set
fit_params = Parameters()
for iy, y in enumerate(data):
    fit_params.add( 'x_%i' % (iy+1))
    fit_params.add( 'y_%i' % (iy+1))

# run the global fit to all the data sets
minimize(objective, fit_params, args=(x, data))

in minimize(objective, fit_params, args=(x, data)): datais data[y][z]: y - 数据集,z - 该数据集中的数据。并且x是 x 轴。

如何修改我的 python 脚本 lmfit 最小化以忽略丢失的数据点或重写我的脚本,以便每个数据都有自己的 x 轴?:

x1   y1    x2   y2   x3   y3
-----------------------------
1    5     1    8    2    4-
2    4     3    7    3    10
3    7     4    4    4    12
4    9               5    20
5    10              6    21
6    15

我也不能使用多个最小化拟合(脚本实际上比上面显示的更复杂),所以 first = minimum(objective, fit_params, args=(x1, y1)), second = minimize(objective, fit_params, args=(x2, y2 )) 不是有效的答案。

4

1 回答 1

0

我看不到您如何读取数据,因此无法判断缺失值是如何表示的。如果您使用类似的东西np.nan来表示缺失值,那么您可以使用np.isnan和从残差计算中屏蔽掉这些点np.where。这可能发生在结果之前或之后flatten()

于 2015-10-17T01:06:46.883 回答