Created
January 4, 2019 11:10
-
-
Save thomas-chauvet/3049797fbec681cec9f8d17cd61382e7 to your computer and use it in GitHub Desktop.
Get column names after one hot encoding by scikit-learn.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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