What is the best way to prepare interactions of categorical features before fitting with scikit-learn?

With statsmodels I could conveniently say in R-style smf.ols(formula = 'depvar ~ C(var1)*C(var2)', data=df).fit() (same in Stata with regress depvar i.var1##i.var2).

Can sklearn.preprocessing.PolynomialFeatures (in v0.15, currently dev) be used with categorical variables?


2 Answers 2


Indeed you can use Patsy with scikit-learn to obtain the same results you would obtain with R, or with the formula notation in stats models. See code below:

from patsy import dmatrices

# create dummy variables, and their interactions
y, X = dmatrices('depvar ~ C(var1)*C(var2)', df, return_type="dataframe")
# flatten y into a 1-D array so scikit-learn can understand it
y = np.ravel(y)

you can now use any model implemented in scikit-learn with the usual notations having X as independent variables, and y as dependent one.

  • $\begingroup$ what if we want to have an empty LHS? (~var1*var2 is perfectly fine in R for constructing the RHS matrix) $\endgroup$ Aug 1, 2017 at 8:48
  • $\begingroup$ (you should mention dmatrix) $\endgroup$ Aug 1, 2017 at 8:52

Use Patsy.

Patsy is one of my favourite Python libraries: it does one thing, and only one thing, really really well.


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.