OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Polynomial Multivariate Regression Analysis 2

  • Thread starter Thread starter Patrick Flume
  • Start date Start date
P

Patrick Flume

Guest
I'm new to regression analysis in python and greatly appreciate your help:

I have a formula (Y = x1 + Ax1^2 + Bx2^2 + Cx3^2 + Constant), and a dataset to train the model. The dataset includes a column for each Y, x1, x2, x3, and the Month. See example dataset here

I'd like the regression analysis to run 12 times, once for each Month - such that I'll be able to see a table of 12 rows (one for each month) with coefficients and constants to reference for calculating predictions.

Presently I have the following code set up which does not present the modelrun coefficients and intercepts by month. Can someone help me add to the code to present a table of coefficients and constants, one row for each month?

Code:
import numpy as np
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

#Read dataset
df = pd.read_csv('StackOverdlow - Example Dataset 20240621.csv')

#Replace negative y with zero
df.y[df.y<0]=0


def _reg(df):
    df['y'] = df['y'].apply(lambda x: max(0, x))
    res = pd.DataFrame(columns=['Month', 'Intercept', 'x1', 'x1^2', 'x2', 'x2^2', 'x3', 'x3^2'])

    for month in range(1, 12 + 1):
        MD = df[df['Month'] == month]
        X = MD[['x1', 'x2', 'x3']].values
        y = MD['y'].values

        poly = PolynomialFeatures(degree=2, include_bias=False)
        X_poly = poly.fit_transform(X)

        model = LinearRegression()
        model.fit(X_poly, y)

        C, I = model.coef_, model.intercept_

        row = {
            'Month': month,
            'Intercept': I,
            'x1': C[0],
            'x1^2': C[1],
            'x2': C[2],
            'x2^2': C[3],
            'x3': C[4],
            'x3^2': C[5]
        }

        res = res._append(row, ignore_index=True)

    return res

I tried the above code with the following recommended code appended and it does not pull the actual coefficients from the model run.

Code:
D = {
    'Month': np.tile(np.arange(1, 12 + 1), 10),
    'x1': np.random.rand(120),
    'x2': np.random.rand(120),
    'x3': np.random.rand(120),
    'y': np.random.rand(120) * 100
}

print(_reg(pd.DataFrame(D)))
<p>I'm new to regression analysis in python and greatly appreciate your help:</p>
<p>I have a formula (Y = x1 + Ax1^2 + Bx2^2 + Cx3^2 + Constant), and a dataset to train the model. The dataset includes a column for each Y, x1, x2, x3, and the Month. See example dataset <a href="https://drive.google.com/file/d/12TtytNPnMXsdk1nyqonKnOZeiY-Xonrx/view?usp=sharing" rel="nofollow noreferrer">here</a></p>
<p>I'd like the regression analysis to run 12 times, once for each Month - such that I'll be able to see a table of 12 rows (one for each month) with coefficients and constants to reference for calculating predictions.</p>
<p>Presently I have the following code set up which does not present the modelrun coefficients and intercepts by month. Can someone help me add to the code to present a table of coefficients and constants, one row for each month?</p>
<pre><code>
import numpy as np
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

#Read dataset
df = pd.read_csv('StackOverdlow - Example Dataset 20240621.csv')

#Replace negative y with zero
df.y[df.y<0]=0


def _reg(df):
df['y'] = df['y'].apply(lambda x: max(0, x))
res = pd.DataFrame(columns=['Month', 'Intercept', 'x1', 'x1^2', 'x2', 'x2^2', 'x3', 'x3^2'])

for month in range(1, 12 + 1):
MD = df[df['Month'] == month]
X = MD[['x1', 'x2', 'x3']].values
y = MD['y'].values

poly = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly.fit_transform(X)

model = LinearRegression()
model.fit(X_poly, y)

C, I = model.coef_, model.intercept_

row = {
'Month': month,
'Intercept': I,
'x1': C[0],
'x1^2': C[1],
'x2': C[2],
'x2^2': C[3],
'x3': C[4],
'x3^2': C[5]
}

res = res._append(row, ignore_index=True)

return res
</code></pre>
<p>I tried the above code with the following recommended code appended and it does not pull the actual coefficients from the model run.</p>
<pre><code>
D = {
'Month': np.tile(np.arange(1, 12 + 1), 10),
'x1': np.random.rand(120),
'x2': np.random.rand(120),
'x3': np.random.rand(120),
'y': np.random.rand(120) * 100
}

print(_reg(pd.DataFrame(D)))
</code></pre>
 

Latest posts

Top