使用 Scipy.optimize 最小化 SSE
Minimizing SSE using Scipy.optimize minimize
我正在尝试使用 scipy.optimize
优化函数的 SSE(误差平方和)。为了测试,我创建了一个简单的问题,如下代码所示。
但是scipy输出的优化参数永远不会使SSE=0。有人可以帮助我理解,我哪里错了。
我尝试用我的代码计算的 SSE 与 excel 中计算的 SSE 进行交叉检查。它匹配。然后我使用最小化函数来最小化SSE函数,Scipy计算的与手算的不匹配。我用的函数是形式(y=ax+b)。下面是代码
import numpy as np
from scipy.optimize import minimize
e=np.array([0,2])
sig1=np.array([0,200])
k = [10,10]
#n = 0.2
coe=np.array([k[0],k[1]])
def sig2(e):
v=(k[0]*e)+ k[1]
SEzip = zip(sig1, v)
sse = 0
for y in SEzip:
sse += np.power((y[0] - y[1]),2)
return sse
print (sig2(e))
def f(coe):
print(coe)
return f
result = minimize(sig2,coe,method='Nelder-Mead',callback=(f),options={'xtol': 1e-6,'ftol':1e-06,'maxiter':50000,'disp': True,'adaptive' : True})
print(result)
你在这里打印了你的 x0
aka coe
,我编辑了你的代码并将你的 objective 函数 sig2()
缩短为一行,然后编辑了你的回调以显示测试的变量及其等效的 objective 函数值。现在你可以清楚地看到sse=0
已经达到了。
import numpy as np
from scipy.optimize import minimize
# for prettier numpy prints
np.set_printoptions(precision = 6)
# init
e = np.array([0,2])
sig1 = np.array([0,200])
k = [10, 10]
coe = np.array([k[0], k[1]])
# define objective function
def sig2(e):
return sum([np.power((y[0] - y[1]), 2) for y in zip(sig1, (k[0]*e)+ k[1])])
# define callback
def f(e):
print("e: %25s | sig2(e): %5s" % (e,round(sig2(e), 6)))
# optimize
result = minimize(sig2,
coe,
method = 'Nelder-Mead',
callback = f,
options = {'xtol': 1e-6,'ftol':1e-06,
'maxiter':50000,'disp': True,'adaptive' : True})
print(result)
输出:
...
e: [-1.000053 18.999751] | sig2(e): 6e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-0.999934 18.999981] | sig2(e): 0.0
e: [-1.000049 18.999979] | sig2(e): 0.0
e: [-1.000027 19.000044] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-1.000009 18.999993] | sig2(e): 0.0
e: [-1.000009 18.999993] | sig2(e): 0.0
e: [-0.999995 19. ] | sig2(e): 0.0
e: [-0.999995 19. ] | sig2(e): 0.0
e: [-1.000003 18.999998] | sig2(e): 0.0
e: [-1. 19.000002] | sig2(e): 0.0
e: [-0.999998 19. ] | sig2(e): 0.0
e: [-1.000001 18.999999] | sig2(e): 0.0
e: [-1. 19.000001] | sig2(e): 0.0
e: [-0.999999 19. ] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 56
Function evaluations: 110
final_simplex: (array([[-1., 19.],
[-1., 19.],
[-1., 19.]]), array([6.221143e-12, 1.914559e-11, 1.946860e-11]))
fun: 6.2211434216849394e-12
message: 'Optimization terminated successfully.'
nfev: 110
nit: 56
status: 0
success: True
x: array([-1., 19.])
我正在尝试使用 scipy.optimize
优化函数的 SSE(误差平方和)。为了测试,我创建了一个简单的问题,如下代码所示。
但是scipy输出的优化参数永远不会使SSE=0。有人可以帮助我理解,我哪里错了。
我尝试用我的代码计算的 SSE 与 excel 中计算的 SSE 进行交叉检查。它匹配。然后我使用最小化函数来最小化SSE函数,Scipy计算的与手算的不匹配。我用的函数是形式(y=ax+b)。下面是代码
import numpy as np
from scipy.optimize import minimize
e=np.array([0,2])
sig1=np.array([0,200])
k = [10,10]
#n = 0.2
coe=np.array([k[0],k[1]])
def sig2(e):
v=(k[0]*e)+ k[1]
SEzip = zip(sig1, v)
sse = 0
for y in SEzip:
sse += np.power((y[0] - y[1]),2)
return sse
print (sig2(e))
def f(coe):
print(coe)
return f
result = minimize(sig2,coe,method='Nelder-Mead',callback=(f),options={'xtol': 1e-6,'ftol':1e-06,'maxiter':50000,'disp': True,'adaptive' : True})
print(result)
你在这里打印了你的 x0
aka coe
,我编辑了你的代码并将你的 objective 函数 sig2()
缩短为一行,然后编辑了你的回调以显示测试的变量及其等效的 objective 函数值。现在你可以清楚地看到sse=0
已经达到了。
import numpy as np
from scipy.optimize import minimize
# for prettier numpy prints
np.set_printoptions(precision = 6)
# init
e = np.array([0,2])
sig1 = np.array([0,200])
k = [10, 10]
coe = np.array([k[0], k[1]])
# define objective function
def sig2(e):
return sum([np.power((y[0] - y[1]), 2) for y in zip(sig1, (k[0]*e)+ k[1])])
# define callback
def f(e):
print("e: %25s | sig2(e): %5s" % (e,round(sig2(e), 6)))
# optimize
result = minimize(sig2,
coe,
method = 'Nelder-Mead',
callback = f,
options = {'xtol': 1e-6,'ftol':1e-06,
'maxiter':50000,'disp': True,'adaptive' : True})
print(result)
输出:
...
e: [-1.000053 18.999751] | sig2(e): 6e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-1.000062 19.000109] | sig2(e): 2e-06
e: [-0.999934 18.999981] | sig2(e): 0.0
e: [-1.000049 18.999979] | sig2(e): 0.0
e: [-1.000027 19.000044] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-0.999986 18.999996] | sig2(e): 0.0
e: [-1.000009 18.999993] | sig2(e): 0.0
e: [-1.000009 18.999993] | sig2(e): 0.0
e: [-0.999995 19. ] | sig2(e): 0.0
e: [-0.999995 19. ] | sig2(e): 0.0
e: [-1.000003 18.999998] | sig2(e): 0.0
e: [-1. 19.000002] | sig2(e): 0.0
e: [-0.999998 19. ] | sig2(e): 0.0
e: [-1.000001 18.999999] | sig2(e): 0.0
e: [-1. 19.000001] | sig2(e): 0.0
e: [-0.999999 19. ] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
e: [-1. 19.] | sig2(e): 0.0
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 56
Function evaluations: 110
final_simplex: (array([[-1., 19.],
[-1., 19.],
[-1., 19.]]), array([6.221143e-12, 1.914559e-11, 1.946860e-11]))
fun: 6.2211434216849394e-12
message: 'Optimization terminated successfully.'
nfev: 110
nit: 56
status: 0
success: True
x: array([-1., 19.])