Search code examples
pythonnon-linear-regressiongekko

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

So, I have three columns of independent varibale x,y, and z. One dependent variable sy. I am trying to fit a model to this. I copied the codes from this post, but I get the following error:

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

Could you help me with this?

The model I am trying to fit is sy = a + b*x + c*x^2 + d*y + e*y^2 + f*z + g*z^2. Is there any way to get the Adjusted R-Squared value of the model?


Solution

  • To fix the error and get the results, Line ten of your code needs to be revised as follows:

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