Python 多变量最小二乘法

New*_*wtt 6 python optimization scipy

我有一个需要在 python 中解决的优化问题。总体结构是

def foo(a, b, c, d, e):
    # do something and return one value


def bar(a, b, c, d, e, f, g, h, i, j):
    # do something and return one value


def func():
    return foo(a, b, c, d, e) - bar(a, b, c, d, e, f, g, h, i, j)
Run Code Online (Sandbox Code Playgroud)

我想使用least_squares最小化并将 的值作为列表返回,其中平方差是和f, g, h, i and j之间的最小值。我不知道如何使用这个。foobarleast_squares

我试过这个:

# Initial values f, g, h, i, j
x0 =[0.5,0.5,0.5,0.05,0.5]

# Constraints
lb = [0,0,0,0,-0.9]
ub = [1, 100, 1, 0.5, 0.9]

x = least_squares(func, x0, lb, ub)
Run Code Online (Sandbox Code Playgroud)

如何获得最小值x列表的返回值?f, g, h, i and j

Cle*_*leb 4

您当前定义问题的方式相当于最大化bar(假设您传递func给最小化函数)。a由于您不改变的参数e,基本上是常数和可以调整func的结果之间的差异;bar由于负号,它将被尝试最大化,因为这会最小化整个函数。

我认为你真正想要最小化的是两个函数之间的绝对值或平方差。我使用一个简单的示例进行说明,其中我假设函数仅返回参数的总和:

from scipy.optimize import minimize

def foo(a, b, c, d, e):
    # do something and return one value

    return a + b + c + d + e

def bar(a, b, c, d, e, f, g, h, i, j):
    # do something and return one value
    return a + b + c + d + e + f + g + h + i + j

def func1(x):
    # your definition, the total difference
    return foo(x[0], x[1], x[2], x[3], x[4]) - bar(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9])

def func2(x):
    # quadratic difference
    return (foo(x[0], x[1], x[2], x[3], x[4]) - bar(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9]))**2

# Initial values for all variables
x0 = (0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.05, 0.5)

# Constraints
# lb = [0,0,0,0,-0.9]
# ub = [1, 100, 1, 0.5, 0.9]
# for illustration, a, b, c, d, e are fixed to 0; that should of course be changed
bnds = ((0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0., 1), (0., 100.), (0, 1), (0, 0.5), (-0.9, 0.9))

res1 = minimize(func1, x0, method='SLSQP', bounds=bnds)
res2 = minimize(func2, x0, method='SLSQP', bounds=bnds)
Run Code Online (Sandbox Code Playgroud)

然后你得到:

print res1.x
array([   0. ,    0. ,    0. ,    0. ,    0. ,    1. ,  100. ,    1. ,
          0.5,    0.9])
Run Code Online (Sandbox Code Playgroud)

print res1.fun
-103.4
Run Code Online (Sandbox Code Playgroud)

如上所述,所有参数都将达到上限以最大化,bar从而最小化func

对于调整后的函数func2,您将收到:

res2.fun
5.7408853312979541e-19  # which is basically 0

res2.x
array([ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.15254237,  0.15254237,  0.15254237,  0.01525424, -0.47288136])
Run Code Online (Sandbox Code Playgroud)

因此,正如预期的那样,对于这个简单的情况,我们可以选择参数,使这两个函数之间的差值变为 0。显然,参数的结果不是唯一的,它们也可能全为 0。

我希望这有助于使您的实际功能发挥作用。

编辑:

正如您所要求的least_square,这也可以正常工作(使用上面的函数定义);那么总差就可以了:

from scipy.optimize import least_squares

lb = [0,0,0,0,0,0,0,0,0,-0.9]
ub = [0.1,0.1,0.1,0.1,0.1,1, 100, 1, 0.5, 0.9]
res_lsq = least_squares(func1, x0, bounds=(lb, ub))
Run Code Online (Sandbox Code Playgroud)

然后您会收到与上面相同的结果:

res_lsq.x
array([  1.00000000e-10,   1.00000000e-10,   1.00000000e-10,
         1.00000000e-10,   1.00000000e-10,   1.52542373e-01,
         1.52542373e-01,   1.52542373e-01,   1.52542373e-02,
        -4.72881356e-01])

res_lsq.fun
array([ -6.88463034e-11])  # basically 0
Run Code Online (Sandbox Code Playgroud)

由于在此问题中 5 个参数不会发生变化,因此我会将它们固定为某个值,并且不会将它们传递给优化调用。