I am trying to generate polynomial regressions of different degrees and save the model objects.
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error as MSE
df = pd.read_csv("kc_house_train_data(1).csv")
X = df['sqft_living'].values
Y = df['price'].values
lm = LinearRegression()
def Poly(X, degree):
poly = PolynomialFeatures(degree = degree)
poly_X = poly.fit_transform(X.reshape(-1,1))
return poly_X
models = []
for i in range(15):
poly_X = Poly(X, i 1)
model = lm.fit(poly_X, Y)
model.append(models)
Where lm
is LinearRegression() from sklearn.
I end up getting a list of models, but all the models are the 15 degree polynomial. Not sure what I'm doing wrong.
edit: output of print(df[['sqft_living', 'price']].head(10).tostring())
:
sqft_living price
0 1180 221900.0
1 2570 538000.0
2 770 180000.0
3 1960 604000.0
4 1680 510000.0
5 5420 1225000.0
6 1715 257500.0
7 1060 291850.0
8 1780 229500.0
9 1890 323000.0
10 3560 662500.0
CodePudding user response:
You have to recreate your model at each iteration. Try:
models = []
for i in range(15):
poly_X = Poly(X, i 1)
lm = LinearRegression()
model = lm.fit(poly_X, Y)
models.append(model)
>>> models[0].n_features_in_
2
>>> models[1].n_features_in_
3
>>> models[2].n_features_in_
4