# Calculating the polynomial features after or before centering the data?

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?

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)


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

• 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! – renakre May 5 '17 at 20:31
• @renakre Check the data matrix $X$ after polynomial expansion, if it contains a column that is all $1$, then it contains the intercept. – hxd1011 May 5 '17 at 20:34
• @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. – hxd1011 May 5 '17 at 20:36