Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thomas-chauvet/3049797fbec681cec9f8d17cd61382e7 to your computer and use it in GitHub Desktop.
Save thomas-chauvet/3049797fbec681cec9f8d17cd61382e7 to your computer and use it in GitHub Desktop.
Get column names after one hot encoding by scikit-learn.
"""
Get column names after one hot encoding by scikit-learn.
"""
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
titanic_url = ('https://raw.githubusercontent.com/amueller/'
'scipy-2017-sklearn/091d371/notebooks/datasets/titanic3.csv')
data = pd.read_csv(titanic_url).loc[:,["sex", "embarked", "pclass"]].dropna()
data.head()
enc = OneHotEncoder(handle_unknown='ignore')
pd.DataFrame(enc.fit_transform(data).toarray(), columns=enc.get_feature_names(data.columns)).head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment