# How to prepare interactions of categorical variables in scikit-learn?

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?

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.

• what if we want to have an empty LHS? (~var1*var2 is perfectly fine in R for constructing the RHS matrix) Aug 1, 2017 at 8:48
• (you should mention dmatrix) 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.