繁体   English   中英

不使用 GEKKO 的多元非线性回归

[英]Multivariate Nonlinear Regression without using GEKKO

import numpy as np
import pandas as pd

#import the excel file
dataset = pd.read_excel('data.xlsx')

#set all the independent variable as 'x', 'y', and 'z' and the dependent variable as 'sy'
x = dataset.iloc[:,0]
y = dataset.iloc[:,1]
z = dataset.iloc[:,2]
sy = dataset.iloc[:,3]

A = np.column_stack([np.ones(len(x)), x, x**2, y, y**2, z, z**2])

#"B" will be "sy" array
B = sy

#Solving Ax = B
a, b, c, d, e, f, g = np.linalg.lstsq(A,B)
#result, _, _, _, _, _ = np.linalg.lstsq(A,B)
#a, b, c, d, e, f, g = result

所以,我有三列独立变量xyz 一个因变量sy 我正在尝试为此安装 model。 我从这篇文章中复制了代码,但出现以下错误:

ValueError: not enough values to unpack (expected 7, got 4)

你能帮我解决这个问题吗?

我要拟合的 model 是sy = a + b*x + c*x^2 + d*y + e*y^2 + f*z + g*z^2 有什么方法可以得到 model 的调整后的 R 平方值?

要修复错误并获得结果,您的代码的第 10 行需要修改如下:

a, b, c, d, e, f, g = np.linalg.lstsq(A,B)[0]

其他方法可用于回归。 幸运的是,这个问题是线性回归,所以在 Python 中有很多方法(参见Jupyter Notebook机器学习在线课程)。

线性回归

如本示例问题所示, Gekko 擅长非线性回归。 它还展示了如何从任何回归中获取R^2值。

from scipy import stats
slope, intercept, r_value, p_value, \
       std_err = stats.linregress(ym,y)
r2 = r_value**2 
cR2 = "R^2 correlation = " + str(r_value**2)

非线性回归示例

# Energy price non-linear regression
# solve for oil sales price (outcome)
# using 3 predictors of WTI Oil Price, 
#   Henry Hub Price and MB Propane Spot Price
import numpy as np
# use 'pip install gekko' to get package
from gekko import GEKKO
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# data file from URL address
data = 'https://apmonitor.com/me575/uploads/Main/oil_data.txt'
df = pd.read_csv(data)

xm1 = np.array(df["WTI_PRICE"]) # WTI Oil Price
xm2 = np.array(df["HH_PRICE"])  # Henry Hub Gas Price
xm3 = np.array(df["NGL_PRICE"]) # MB Propane Spot Price 
ym = np.array(df["BEST_PRICE"]) # oil sales price 

# GEKKO model
m = GEKKO()
a = m.FV(lb=-100.0,ub=100.0)
b = m.FV(lb=-100.0,ub=100.0)
c = m.FV(lb=-100.0,ub=100.0)
d = m.FV(lb=-100.0,ub=100.0)
x1 = m.Param(value=xm1)
x2 = m.Param(value=xm2)
x3 = m.Param(value=xm3)
z = m.Param(value=ym)
y = m.Var()
m.Equation(y==a*(x1**b)*(x2**c)*(x3**d))
m.Minimize(((y-z)/z)**2)
# Options
a.STATUS = 1
b.STATUS = 1
c.STATUS = 1
d.STATUS = 1
m.options.IMODE = 2
m.options.SOLVER = 1
# Solve
m.solve()

print('a: ', a.value[0])
print('b: ', b.value[0])
print('c: ', c.value[0])
print('d: ', d.value[0])

cFormula = "Formula is : " + "\n" + \
           r"$A * WTI^B * HH^C * PROPANE^D$"

from scipy import stats
slope, intercept, r_value, p_value, \
       std_err = stats.linregress(ym,y)

r2 = r_value**2 
cR2 = "R^2 correlation = " + str(r_value**2)
print(cR2)

# plot solution
plt.figure(1)
plt.plot([20,140],[20,140],'k-',label='Measured')
plt.plot(ym,y,'ro',label='Predicted')
plt.xlabel('Measured Outcome (YM)')
plt.ylabel('Predicted Outcome (Y)')
plt.legend(loc='best')
plt.text(25,115,'a =' + str(a.value[0]))
plt.text(25,110,'b =' + str(b.value[0]))
plt.text(25,105,'c =' + str(c.value[0]))
plt.text(25,100,'d =' + str(d.value[0]))
plt.text(25,90,r'$R^2$ =' + str(r_value**2))
plt.text(80,40,cFormula)
plt.grid(True)
plt.show()

也感谢您提出这个问题: GEKKO 多元非线性回归

暂无
暂无

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

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