Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feature preprocessing of both continuous and categorical variables (of integer type) with scikit-learn

The main goals are as follows:

  1. Apply StandardScaler to continuous variables

  2. Apply LabelEncoder and OnehotEncoder to categorical variables

The continuous variables need to be scaled, but at the same time, a couple of categorical variables are also of integer type. Applying StandardScaler would result in undesired effects.

On the flip side, the StandardScaler would scale the integer based categorical variables, which is also not what we want.

Since continuous variables and categorical ones are mixed in a single Pandas DataFrame, what's the recommended workflow to approach this kind of problem?

The best example to illustrate my point is the Kaggle Bike Sharing Demand dataset, where season and weather are integer categorical variables

like image 510
James Wong Avatar asked Apr 22 '17 03:04

James Wong


People also ask

Which can make classification based on both numerical and categorical variables?

Clustering datasets having both numerical and categorical variables.

Can we use StandardScaler on categorical features?

The continuous variables need to be scaled, but at the same time, a couple of categorical variables are also of integer type. Applying StandardScaler would result in undesired effects. On the flip side, the StandardScaler would scale the integer based categorical variables, which is also not what we want.

How does Scikit-learn handle categorical data?

One Hot Encoding To increase performance one can also first perform label encoding then those integer variables to binary values which will become the most desired form of machine-readable. Pandas get_dummies() converts categorical variables into dummy/indicator variables.


1 Answers

Check out the sklearn_pandas.DataFrameMapper meta-transformer. Use it as the first step in your pipeline to perform column-wise data engineering operations:

mapper = DataFrameMapper(
  [(continuous_col, StandardScaler()) for continuous_col in continuous_cols] +
  [(categorical_col, LabelBinarizer()) for categorical_col in categorical_cols]
)
pipeline = Pipeline(
  [("mapper", mapper),
  ("estimator", estimator)]
)
pipeline.fit_transform(df, df["y"])

Also, you should be using sklearn.preprocessing.LabelBinarizer instead of a list of [LabelEncoder(), OneHotEncoder()].

like image 162
user1808924 Avatar answered Oct 24 '22 22:10

user1808924