首页 > 解决方案 > GEKKO Python - 来自文档的简单线性回归

问题描述

通过遵循 GEKKO 文档,我使用了线性和多项式回归的示例。这只是关于简单线性回归的部分。

from gekko import GEKKO
import numpy as np

xm = np.array([0.1,0.3,0.2,0.5,0.8])
ym = np.array([0.52,0.53,0.4,0.6,1.01])
             
#### Solution
m = GEKKO()
m.options.IMODE=2
# coefficients
c = [m.FV(value=0) for i in range(2)]
c[0].STATUS=1
c[1].STATUS=1

x = m.Param(value=xm)
yd = m.Param(value=ym)

y = m.CV(value=ym)
y.FSTATUS = 1

######### uncomment ############
#y = m.Var()
#m.Minimize((y-yd)**2)
################################
# polynomial model

m.Equation(y==c[0]+c[1]*x)
# linear regression
m.solve(disp=False)
p1 = [c[1].value[0],c[0].value[0]]
p1

我只是想知道为什么取消注释行时会得到不同的结果

y = m.Var()
m.Minimize((y-yd)**2)

文档中获得的结果(线性、二次、三次)似乎不是最小二乘法。在这些情况下使用了哪些最小化标准?

最好的问候, 拉多万

标签: pythonregressiongekko

解决方案


Switch to m.options.EV_TYPE=2 to get a squared error objective.

The default in Gekko is the l1-norm objective. Here is a description of the differences:

Objective Function

The l1-norm objective (sum of the absolute error) is less sensitive to outliers and bad data.


推荐阅读