简体   繁体   English

使用Python中的Levenberg-Marquardt算法优化方程组

[英]Optimizing set of equations with Levenberg-Marquardt algorithm in Python

I wish to use the scipy.optimize.leastsq() method from scipy to optimize three parameters a,b,c . 我希望使用scipy.optimize.leastsq()方法来优化三个参数a,b,c What I do have are these two equations. 我所拥有的是这两个方程式。

1*a+2*b+3*c = x1
4*a+5*b+6*c = x2

Analytically this set of equations is underdetermined, but numerically I am trying to find a,b,c to minimize the error to given results from a measurement [2,2] : 从分析上来说,这组方程是不确定的,但在数值上,我试图找到a,b,c以将对测量[2,2]给定结果的误差最小化:

1*a+2*b+3*c - 2 = 0
4*a+5*b+6*c - 2 = 0

Therefore I have written some code. 因此,我写了一些代码。

def function(a,b,c,t):
    return np.array([1*a+2*b+3*c+t[1],4*a+5*b+6*c+t[1]])

a0 = 1
b0 = 1
c0 = 1
measdata = np.array([2,2])
t = [1,2]

def residual(x0,measdata,t):
    return measdata - function(x0[0],x0[1],x0[2],t)

erg = optimize.leastsq(func=residual,x0=(a0,b0,c0),args=(measdata,t))

It always results in: 它总是导致:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-296-ab0fc90a2253> in <module>()
     14     return result - function(x0[0],x0[1],x0[2],t)
     15 
---> 16 erg = optimize.leastsq(func = residual, x0 = (a0,b0,c0) , args=(result,t), maxfev=10000)
     17 
     18 function(erg[0][0],erg[0][1])

    //anaconda/lib/python3.5/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
        378     m = shape[0]
        379     if n > m:
    --> 380         raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
        381     if epsfcn is None:
        382         epsfcn = finfo(dtype).eps
    TypeError: Improper input: N=3 must not exceed M=2

How do I get it to find a minimum? 我如何找到最小值? I am aware it's only a local minimum, but I would be happy with it. 我知道这只是本地最低要求,但我对此感到满意。

The error is telling you what you already know, ie, the system is underdetermined, with n being the number of parameters and m the number of constraints. 错误告诉您已经知道的内容,即系统未确定,其中n是参数数量, m是约束数量。

If you fix one of the parameters so that n > m is False , the code will stop complaining. 如果您修复参数之一,使n > mFalse ,则代码将停止抱怨。 For example, change 例如改变

def residual(x0,measdata,t):
    return measdata - function(x0[0],x0[1],x0[2],t)

erg = optimize.leastsq(func=residual,x0=(a0,b0,c0),args=(measdata,t))

to

def residual(x0,measdata,t):
    # we fix the value of `c` here
    return measdata - function(x0[0],x0[1],5,t)

# only two parameters for `x0`
erg = optimize.leastsq(func=residual,x0=(a0,b0),args=(measdata,t))

To answer the question how you can do what you want, I'm not sure it can be done with scipy. 要回答如何做自己想做的问题,我不确定是否可以通过scipy完成。 I found this issue saying that scipy can't handle underdetermined systems: 我发现这个问题说scipy无法处理未确定的系统:

Interesting, I assumed the MINPACK routines would handle also m < n, but apparently not. 有趣的是,我认为MINPACK例程也可以处理m <n,但显然不能。 The reason why they don't is probably that for m < n the minimum is some manifold of points, which causes problems in the termination conditions. 之所以不这样做,可能是因为m <n的最小值是一些点的集合,这会导致终止条件出现问题。

So would be after all some interest in adding also a small-scale solver for underdetermined least-square problems. 毕竟,对于为不确定的最小二乘问题添加小型解算器也会引起一些兴趣。

Even though that post was from 3 years ago, I still can't find any evidence in the docs that scipy can do what you want. 即使该帖子来自3年前,我仍然无法在文档中找到任何证据证明scipy可以完成您想要的事情。 However, I found a SO answer that claims you can solve for an underdetermined matrix , but I haven't fully grasped the mathematics yet to make sure whether it'd be applicable for your case. 但是,我找到了一个SO答案,声称可以解决一个欠定矩阵 ,但我尚未完全掌握数学以确保它是否适用于您的情况。 Since I find it hard to summarise the post, I'll just quote what seems to be the most important section. 由于我很难对文章进行总结,因此我只引用似乎是最重要的部分。

In the case where A·x = b is underdetermined, A·x = b的情况下,

 x1, res, rnk, s = np.linalg.lstsq(A, b) 

will pick a solution x' that minimizes || 将选择最小化||的解x' x || x || L2 subject to || L2|| A·x - b || A·x - b || L2 = 0 . L2 = 0 This happens not to be the particular solution we are looking for, but we can linearly transform it to get what we want. 碰巧这不是我们要寻找的特定解决方案,但是我们可以对其进行线性转换以得到我们想要的。 In order to do that, we'll first compute the right null space of A , which characterizes the space of all possible solutions to A·x = b . 为此,我们将首先计算A正确零空间 ,该零空间表征了A·x = b的所有可能解的空间。 We can get this using a rank-revealing QR decomposition 我们可以通过显示排名的QR分解得到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Neurolab中的Levenberg-Marquardt算法替代方案 - Levenberg-Marquardt algorithm alternatives in Neurolab 具有 GPU 支持的 Levenberg-Marquardt 算法 - Levenberg-Marquardt algorithm with GPU support keras将Levenberg-Marquardt优化算法实现为自定义优化器 - keras implementation of Levenberg-Marquardt optimization algorithm as a custom optimizer 使用最小二乘法(Levenberg-Marquardt算法)将双曲线拟合到线性数据的程序不能按预期工作 - Program to fit a hyperbola to linear data using least squares (Levenberg-Marquardt algorithm) not working as expected 基于 scipy.optimize.basinhopping 中的 Levenberg-Marquardt 的自定义最小化器 - Custom minimizer based on Levenberg-Marquardt in scipy.optimize.basinhopping Levenberg-Marquardt-算法和 ODR 之间的区别 - Difference between Levenberg-Marquardt-Algorithm and ODR 在Scipy的minimum_squares函数中使用Levenberg-Marquardt方法 - Using Levenberg-Marquardt method in scipy's least_squares function 非线性最小二乘:使用 Levenberg-Marquardt 使用 Scipy.optimize.least_squares 重现 Matlabs lsqnonlin - Non linear Least Squares: Reproducing Matlabs lsqnonlin with Scipy.optimize.least_squares using Levenberg-Marquardt 高斯拟合python中的直方图数据:Trust Region v / s Levenberg Marquardt - Gaussian fit to a histogram data in python: Trust Region v/s Levenberg Marquardt 使用leastsq方法为levenberg marquardt查找参数 - Finding params using leastsq method for levenberg marquardt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM