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?
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.
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)))
Patrick Flume is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.