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)

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.



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 
  • $\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$ – Haitao Du 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$ – Haitao Du 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.