3
$\begingroup$

I am using scikit-learn. I use the following to center the predictor features:

X = sklearn.preprocessing.StandardScaler().fit_transform(X)   

I will use the following code to create the polynomial features:

poly = PolynomialFeatures(degree=2)
poly.fit_transform(X)

My question is regarding if I should center the data before or after creating the polynomial features. Would it matter and how?

$\endgroup$
2
$\begingroup$

If the polynomial fit contains intercept, it will take account the centering job. If not, it is good to center data before passing it to the model.

Here is a demo in R, where red line is the model without intercept.

set.seed(3)
x=runif(10)+3
y=runif(10)
fit1=lm(y~poly(x,2))
plot(x,y,ylim=c(0,1))
lines(seq(-5,5,0.01),predict(fit1,data.frame(x=seq(-5,5,0.01))))

fit2=lm(y~poly(x,2)-1)
lines(seq(-5,5,0.01),predict(fit2,data.frame(x=seq(-5,5,0.01))),col=2)

enter image description here

In addition if you use orthogonal polynomials not only it centers for you, but also numerically stable.

Here is the demo

> set.seed(0)
> x=runif(100)
> colMeans(poly(x,2,raw=F))
            1             2 
 3.971433e-18 -4.330303e-18
> colMeans(poly(x,2,raw=T))
        1         2 
0.5207647 0.3434324 
$\endgroup$
  • $\begingroup$ Can you elaborate on this a bit If the polynomial fit contains intercept, it will take account the centering job? So, sometimes we do not prefer to have the intercept? I appreciate your help! $\endgroup$ – renakre May 5 '17 at 20:31
  • $\begingroup$ @renakre Check the data matrix $X$ after polynomial expansion, if it contains a column that is all $1$, then it contains the intercept. $\endgroup$ – hxd1011 May 5 '17 at 20:34
  • $\begingroup$ @renakre in most cases, we should fit a model with intercept. Some special cases, we want $y=0$ when $x=0$, we can force the model pass though the origin, i,e., fit a model without intercept. $\endgroup$ – hxd1011 May 5 '17 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.