Skip to content

Instantly share code, notes, and snippets.

@finnqiao
Created January 8, 2019 03:35
Show Gist options
  • Save finnqiao/c6f57c34fec8ce287be724154f005075 to your computer and use it in GitHub Desktop.
Save finnqiao/c6f57c34fec8ce287be724154f005075 to your computer and use it in GitHub Desktop.
# Label Encode instead of dummy variables
mappings = []
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
label_df = df.drop('y', axis=1)
for i, col in enumerate(label_df):
if label_df[col].dtype == 'object':
label_df[col] = label_encoder.fit_transform(np.array(label_df[col].astype(str)).reshape((-1,)))
mappings.append(dict(zip(label_encoder.classes_, range(1, len(label_encoder.classes_)+1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment