用scipy拟合多条参数曲线 [英] fit multiple parametric curves with scipy

查看:261
本文介绍了用scipy拟合多条参数曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组(至少3条)曲线(xy数据)。对于每条曲线,参数E和T都是恒定的,但不同。我正在搜索系数a,n和m,以找到所有曲线上的最佳拟合。

I have a set (at least 3) of curves (xy-data). For each curve the parameters E and T are constant but different. I'm searching the coefficients a,n and m for the best fit over all curves.

y= x/E + (a/n+1)*T^(n+1)*x^m

我尝试了curve_fit ,但我不知道如何将参数E和T放入函数f的
中(请参见curve_fit文档)。此外,我不确定我是否正确理解xdata。
Doc说:一个M长度序列或一个(k,M)形数组,用于带有
k个预测变量的函数。什么是预测因子?
因为ydata只有一个维度,所以我显然无法将多条曲线馈入例程。

I tried curve_fit, but I have no idea how to get the parameters E and T into the function f (see curve_fit documentation). Furthermore I'm not sure if I understand xdata correctly. Doc says: An M-length sequence or an (k,M)-shaped array for functions with k predictors. What's a predictor? As ydata has only one dimension I obviously can't feed multiple curves into the routine.

因此,curve_fit可能是错误的方法,但我什至不知道知道搜索合适词的魔力。我不能成为第一个处理此问题的人。

So curve_fit might be the wrong approach but I don't even know the magic words to search for the right one. I can't be the first one dealing with this problem.

推荐答案

一种解决方法是使用 scipy.optimize.leastsq 代替( curve_fit leastsq 的便捷包装)。

One way to do this is use scipy.optimize.leastsq instead (curve_fit is a convenience wrapper around leastsq).

x 数据一维堆叠; y 数据的同上。 3个独立数据集的长度甚至无关紧要;我们称它们为 n1 n2 n3 新的 x y 的形状为(n1 + n2 + n3,)

Stack the x data in one dimension; ditto for the y data. The lengths of the 3 individual datasets don't even matter; let's call them n1, n2 and n3, so your new x and y will have a shape (n1+n2+n3,).

在要优化的功能内,您可以方便地拆分数据。它将不是最好的函数,但这可能会起作用:

Inside the function to optimize, you can split up the data at your convenience. It will not be the nicest function, but this could work:

def function(x, E, T, a, n, m):
    return x/E + (a/n+1)*T^(n+1)*x^m

def leastsq_function(params, *args):
    a = params[0]
    n = params[1]
    m = params[2]
    x = args[0]
    y = args[1]
    E = args[2]
    T = args[3]
    n1, n2 = args[2]

    yfit = np.empty(x.shape)
    yfit[:n1] = function(x[:n1], E[0], T[0], a, n, m)
    yfit[n1:n2] = function(x[n1:n2], E[1], T[1], a, n, m)
    yfit[n2:] = function(x[n2:], E[2], T[2], a, n, m)

    return y - yfit


params0 = [a0, n0, m0]
args = (x, y, (E0, E1, E2), (T0, T1, T2), (n1, n1+n2))
result = scipy.optimize.leastsq(leastsq_function, params0, args=args)

我没有测试过,但这是原理。现在,您将数据分为3个不同的调用,分别在要优化的功能内

I have not tested this, but this is the principle. You're now splitting up the data into 3 different calls inside the function that is to be optimized.

请注意, scipy.optimize.leastsq 仅需要一个函数,该函数返回要最小化的任何值,在这种情况下,您的实际 y 数据和拟合函数数据之间的差。 leastsq 中的实际重要变量是您要适合的参数,而不是 x y 数据。后者作为额外的参数以及三个独立数据集的大小传递(我没有使用n3,我已经对 n1 + n2 进行了一些处理方便;请记住, leastsq_function n1 n2 >是局部变量,而不是原始变量。

Note that scipy.optimize.leastsq simply requires a function that returns whatever value you'd like to be minized, in this case the difference between your actual y data and the fitted function data. The actual important variables in leastsq are the parameters you want to fit for, not the x and y data. The latter are passed as extra arguments, together with the sizes of three separate datasets (I'm not using n3, and I've done some juggling with the n1+n2 for convenience; keep in mind that the n1 and n2 inside leastsq_function are local variables, not the original ones).

由于这是一个难以适应的函数(例如,它可能没有平滑的导数),因此

Since this is an awkward function to fit (it probably won't have a smooth derivative, for example), it is quite essential to


  • 提供良好的初始值( params0 所有 ... 0 值)。

没有跨订单的数据或参数数量级。一切都越接近1(肯定可以几个数量级),就越好。

don't have data or parameters which span orders of magnitude. The closer everything is around 1 (a few orders of magnitude is certainly ok), the better.

这篇关于用scipy拟合多条参数曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆