简体   繁体   English

Scipy:optimize.fmin 和 optimize.leastsq 之间的区别

[英]Scipy: difference between optimize.fmin and optimize.leastsq

What's the difference between scipy's optimize.fmin and optimize.leastsq? scipy的optimize.fmin和optimize.leastsq有什么区别? They seem to be used in pretty much the same way in this example page .本示例页面中,它们的使用方式几乎相同。 The only difference I can see is that leastsq actually calculates the sum of squares on its own (as its name would suggest) while when using fmin one has to do this manually.我能看到的唯一区别是,leastsq 实际上是自己计算平方和(正如它的名字所暗示的那样),而在使用 fmin 时,必须手动执行此操作。 Other than that, are the two functions equivalent?除此之外,这两个功能是否等效?

Different algorithms underneath.下面有不同的算法。

fmin is using the simplex method; fmin 使用单纯形法; leastsq is using least squares fitting. leastsq 正在使用最小二乘拟合。

Just to add some information, I am developing a module to fit a biexponential function and the time difference between leastsq and minimize seems to be almost 100 times.只是为了添加一些信息,我正在开发一个适合双指数 function 的模块,并且最小平方和最小化之间的时间差似乎几乎是 100 倍。 Have a look at the code below for more details.请查看下面的代码以获取更多详细信息。

I used a biexponential curve which is a sum of two exponents and the model function has 4 parameters to fit.我使用了一个双指数曲线,它是两个指数之和,model function 有 4 个参数可以拟合。 S, f, D_star and D. S、f、D_star 和 D。

All default parameters for fitting were used使用了所有默认的拟合参数

S [fe^(-x * D_star) + (1 - f) e^(-x * D)] S [fe^(-x * D_star) + (1 - f) e^(-x * D)]

('Time taken for minimize:', 0.011617898941040039)
('Time taken for leastsq :', 0.0003180503845214844)

The code used:使用的代码:

import numpy as np
from scipy.optimize import minimize, leastsq
from time import time


def ivim_function(params, bvals):
    """The Intravoxel incoherent motion (IVIM) model function.

        S(b) = S_0[f*e^{(-b*D\*)} + (1-f)e^{(-b*D)}]

        S_0, f, D\* and D are the IVIM parameters.

    Parameters
    ----------
        params : array
                parameters S0, f, D_star and D of the model

        bvals : array
                bvalues

    References
    ----------
    .. [1] Le Bihan, Denis, et al. "Separation of diffusion
               and perfusion in intravoxel incoherent motion MR
               imaging." Radiology 168.2 (1988): 497-505.
    .. [2] Federau, Christian, et al. "Quantitative measurement
               of brain perfusion with intravoxel incoherent motion
               MR imaging." Radiology 265.3 (2012): 874-881.
    """
    S0, f, D_star, D = params
    S = S0 * (f * np.exp(-bvals * D_star) + (1 - f) * np.exp(-bvals * D))
    return S


def _ivim_error(params, bvals, signal):
    """Error function to be used in fitting the IVIM model
    """
    return (signal - ivim_function(params, bvals))


def sum_sq(params, bvals, signal):
    """Sum of squares of the errors. This function is minimized"""
    return np.sum(_ivim_error(params, bvals, signal)**2)

x0 = np.array([100., 0.20, 0.008, 0.0009])
bvals = np.array([0., 10., 20., 30., 40., 60., 80., 100.,
                  120., 140., 160., 180., 200., 220., 240.,
                  260., 280., 300., 350., 400., 500., 600.,
                  700., 800., 900., 1000.])
data = ivim_function(x0, bvals)

optstart = time()
opt = minimize(sum_sq, x0, args=(bvals, data))
optend = time()
time_taken = optend - optstart
print("Time taken for opt:", time_taken)


lstart = time()
lst = leastsq(_ivim_error,
              x0,
              args=(bvals, data),)
lend = time()
time_taken = lend - lstart
print("Time taken for leastsq :", time_taken)

print('Parameters estimated using minimize :', opt.x)
print('Parameters estimated using leastsq :', lst[0])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM