4
$\begingroup$

How do the choice of machine learning algorithm and preprocessing change when some of the independent variables are categorical while others are continuous? Can such data be directly applied to the algorithm with categorical data converted using one-hot encoding?

For example, 2 columns are: Age and Race, Age being continuous and Race is categorical.

Update: Ideal buckets for a continuous variable is not known.

$\endgroup$

3 Answers 3

1
$\begingroup$

To clarify, you mean mixed variables in one column? e.g. ABC123

If yes, you create two additional columns: one with categorical and one with numerical values. Afterward, you can encode them (one hot encoding not always necessary).

Detailed explanation in Chapter 11 "Feature Engineering Mixed Variables":

https://www.udemy.com/feature-engineering-for-machine-learning/learn/v4/content

$\endgroup$
1
$\begingroup$

One approach would be to "split" the continuous variable in buckets. Say, for age - 0-18yrs, 18-65yrs; 65+ yrs.

Then, you could assign the input age to a bucket and threat it as a categorical variable.

$\endgroup$
1
  • $\begingroup$ The question doesn't even focus on what you answered for! $\endgroup$ Commented Mar 4, 2019 at 6:48
1
$\begingroup$

It depends on which algorithm (and implementation) you are using.

For instance, the linear regression implemented in sklearn requires all input variables to be numeric and so encoding will be necessary whereas the linear regression implemented in statsmodels can handle categorical input variables quite easily.

If your algorithm of choice requires encoding categorical features, then there are many options available (e.g. one-hot encoding, target encoding, feature hashing,...). In your example, "Race" is probably unlikely to have high cardinality and so one-hot encoding is fine. In other instances where the categorical feature can take many different values (e.g. towns in the USA) one-hot encoding may not be the best choice as it will lead to an excessively wide dataset (although using sparse structures could mitigate the consequences of this).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.