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

时间:2020-06-22 01:44:35

标签: python non-linear-regression

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。我正在尝试为此建立模型。我从这篇文章中复制了代码,但是出现以下错误:

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

您能帮我吗?

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

1 个答案:

答案 0 :(得分:1)

要修复错误并获取结果,需要对代码的第十行进行如下修改:

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