两者之间的区别西皮·莱斯特斯以及最小平方法

2024-10-04 03:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道^{}和{a2}这两种方法有什么区别?在

当我实施它们时,它们产生的chi^2差异最小:

>>> solution0 = ((p0.fun).reshape(100,100))
>>> # p0.fun are the residuals of my fit function np.ravel'ed as returned by least_squares
>>> print(np.sum(np.square(solution0))) 
0.542899505806

>>> solution1 = np.square((median-solution1))
>>> # solution1 is the solution found by least_sq, it does not yield residuals thus I have to subtract it from the median to get the residuals (my special case)
>>> print(np.sum((solution1)))
0.54402852325

有谁能详细说明一下,或者指出我在哪里可以找到其他的文档,scipy的文档有点神秘。在


Tags: thebymynpitmediansumleast
3条回答

least_squares的文档来看,leastsq似乎是一个旧的包装器。在

See also

leastsq   A legacy wrapper for the MINPACK implementation of the Levenberg-Marquadt algorithm.

所以您应该使用least_squares。似乎least_squares具有其他功能。其中最重要的是使用的默认“方法”(即算法)不同:

  • trf : Trust Region Reflective algorithm, particularly suitable for large sparse problems with bounds. Generally robust method.
  • dogbox : dogleg algorithm with rectangular trust regions, typical use case is small problems with bounds. Not recommended for problems with rank-deficient Jacobian.
  • lm : Levenberg-Marquardt algorithm as implemented in MINPACK. Doesn’t handle bounds and sparse Jacobians. Usually the most efficient method for small unconstrained problems.

Default is trf. See Notes for more information.

旧的leastsq算法只是lm方法的包装器,正如文档所说,它只适用于小的无约束问题。在

您在结果中看到的差异可能是由于所采用的算法的不同。在

编写新Scipy函数least_squares的主要原因是允许变量的上下界(也称为“框约束”)。这是一个强烈要求的功能。在

这个看似简单的添加实际上远不是琐碎的,需要全新的算法,特别是dogleg(method="dogleg"中的least_squares)和信任域反射(method="trf"),它们允许对框约束进行健壮和有效的处理(有关算法的详细信息请参阅相关Scipydocumentation )。在

同样重要的是对大规模问题和稀疏雅可比的支持。在

当不需要变量的边界,并且问题不是很大时,新Scipy函数least_squares中的算法与旧的leastsq中使用的Levenberg-Marquardt-MINPACK实现相比几乎没有优势(如果有的话)。在

但是,旧的leastsq和新的least_squares使用选项method="lm"调用相同的minpackfortran代码。由于这个原因,旧的leastsq现在已被废弃,不建议用于新代码。在

least_squares中,可以为每个变量指定上下边界

如果比较docstrings,leatsq还没有提供更多的特性

相关问题 更多 >