1
$\begingroup$

So, sklearn doesn't support categorical data in its models. Is there a known alternative for categorical data modeling (such as random forests, etc.) for Python?

$\endgroup$
  • $\begingroup$ Why not processing / encoding your categorical features ? $\endgroup$ – Theudbald Jan 7 '18 at 15:25
  • $\begingroup$ Are you suggesting one-hot? It seems like a too much of a workaround just because the library doesn't support it $\endgroup$ – shakedzy Jan 7 '18 at 15:53
  • $\begingroup$ Sklearn provides features processing tools including one Hot encoding. See scikit-learn.org/stable/modules/… $\endgroup$ – Theudbald Jan 7 '18 at 16:03
  • $\begingroup$ Still, seems like a workaround simply because this specific library doesn't support it. Is there nothing else? $\endgroup$ – shakedzy Jan 7 '18 at 17:19
2
$\begingroup$

There are definitely ways to process your data to make categorical data compatible with sklearn (e.g one-hot encoding). An alternative you can look into is h2o, which supports categorical features natively (although it doesn't offer the breadth of models of sklearn).

$\endgroup$
1
$\begingroup$

statsmodel supports Fitting models using R-style formulas:

In [5]: df = sm.datasets.get_rdataset("Guerry", "HistData").data

In [6]: df = df[['Lottery', 'Literacy', 'Wealth', 'Region']].dropna()

In [7]: df.head()
Out[7]: 
   Lottery  Literacy  Wealth Region
0       41        37      73      E
1       38        51      22      N
2       66        13      61      C
3       80        46      76      E
4       79        69      83      E

You can fit models without processing categorical data

In [11]: res = smf.ols(formula='Lottery ~ Literacy + Wealth + C(Region)', data=df).fit()

In [12]: print(res.params)
Intercept         38.651655
C(Region)[T.E]   -15.427785
C(Region)[T.N]   -10.016961
C(Region)[T.S]    -4.548257
C(Region)[T.W]   -10.091276
Literacy          -0.185819
Wealth             0.451475
dtype: float64
$\endgroup$

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.