I'm having some trouble getting reasonable responses from a polynomial regression. When I used linear regression I got a reasonable error, but when I switched to Polynomial regressions the error jumped up by almost seven times. It could be because it's just a bad model, but I suspect that I'm doing something wrong. Here's the code that I'm using (I've left a little off the beginning which gets the data and does some processing):
feature_columns = list(train)
feature_columns.remove('SalePrice')
X = train[feature_columns]
y = train['SalePrice']
poly = PolynomialFeatures(degree=2)
polyX = poly.fit_transform(X)
polyTest = poly.fit_transform(test[feature_columns])
lm = LinearRegression()
lm.fit(polyX, y)
import numpy
predictions = numpy.absolute(lm.predict(polyTest).round(decimals = 2))
pandas.DataFrame({'Id': pandas.to_numeric(test.Id, downcast = 'integer'), 'SalePrice':predictions}).to_csv('2017-07-02-2.csv', index = False)
I'm not sure if it matters but there are more than 250 features that are being processed. Another issue, which might be related is that if I try to use a degree larger than 2 then it takes too long to run. What am I doing wrong here?
PolynomialFeatures
also creates all second order interactions, so you're probably overfitting as a consequence. To get all second degree univariate features, you can use aFeatureUnion
after applyingPolynomialFeatures
to each feature in turn. I usually wrap this all in my own class. $\endgroup$FeatureUnion
. Can you elaborate with an application using the code, or link to somewhere that might explain it this relationship betweenFeatureUnion
andPolynomialFeatures
? $\endgroup$