Last active
April 7, 2021 06:44
-
-
Save ramhiser/982ce339d5f8c9a769a0 to your computer and use it in GitHub Desktop.
Apply one-hot encoding to a pandas DataFrame
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
import pandas as pd | |
import numpy as np | |
from sklearn.feature_extraction import DictVectorizer | |
def encode_onehot(df, cols): | |
""" | |
One-hot encoding is applied to columns specified in a pandas DataFrame. | |
Modified from: https://gist.github.com/kljensen/5452382 | |
Details: | |
http://en.wikipedia.org/wiki/One-hot | |
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html | |
@param df pandas DataFrame | |
@param cols a list of columns to encode | |
@return a DataFrame with one-hot encoding | |
""" | |
vec = DictVectorizer() | |
vec_data = pd.DataFrame(vec.fit_transform(df[cols].to_dict(outtype='records')).toarray()) | |
vec_data.columns = vec.get_feature_names() | |
vec_data.index = df.index | |
df = df.drop(cols, axis=1) | |
df = df.join(vec_data) | |
return df | |
def main(): | |
np.random.seed(42) | |
df = pd.DataFrame(np.random.randn(25, 3), columns=['a', 'b', 'c']) | |
# Make some random categorical columns | |
df['e'] = [random.choice(('Chicago', 'Boston', 'New York')) for i in range(df.shape[0])] | |
df['f'] = [random.choice(('Chrome', 'Firefox', 'Opera', "Safari")) for i in range(df.shape[0])] | |
# Vectorize the categorical columns: e & f | |
df = encode_onehot(df, cols=['e', 'f']) | |
print df.head() | |
if __name__ == '__main__': | |
main() |
From a machine learning perspective is there a preferred option between get_dummies
and factorize
. I have read that since factorize
produces unequal distances between categorical values, that the vectorized output of get_dummies
is preferred.
E.g.:
red = 0
blue = 1
green = 2
=> green = 2* blue
which obviously makes no sense.
hi there, I found encode_onehot(df, cols)
can only encode columns all of strings. When apply to df = pd.DataFrame({'category':[6,7,8,6,7,8], 'number':[1,2,3,4,5,6]})
the method vec.get_feature_names()
will only return ['category'] and the encoding will fail.
add the method below will work well on both of the cases above:
def one_hot(df, cols):
for each in cols:
dummies = pd.get_dummies(df[each], prefix=each, drop_first=False)
df = pd.concat([df, dummies], axis=1)
return df
What is the best way to use sklearn's OneHotEncoder with pandas dataframe?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After you create new columns using get_dummies, consider you get e.Chicago and f.Chicago.
Now as you just want to know if Chicago appears at all irrespective of which column, just apply OR condition on both columns and create a new column and then drop the initial 2 columns. Not sure if there is a short cut for this.