# Dealing with a dataset with a mix of continuous and categorical variables

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.

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

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.

• The question doesn't even focus on what you answered for! – Jibin Mathew Mar 4 '19 at 6:48

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).