Skip to content

Instantly share code, notes, and snippets.

@codeslord
Created April 29, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeslord/6fab16aaa0da291461c9bf5d5188b4db to your computer and use it in GitHub Desktop.
Save codeslord/6fab16aaa0da291461c9bf5d5188b4db to your computer and use it in GitHub Desktop.
# Categorical boolean mask
categorical_feature_mask = X.dtypes==object
# filter categorical columns using mask and turn it into a list
categorical_cols = X.columns[categorical_feature_mask].tolist()
# import labelencoder
from sklearn.preprocessing import LabelEncoder
# instantiate labelencoder object
le = LabelEncoder()
# apply le on categorical feature columns
X[categorical_cols] = X[categorical_cols].apply(lambda col: le.fit_transform(col))
X[categorical_cols].head(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment