Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving multiple different polynomial regression objects in python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading