Exemples de comment appliquer un encodage one-hot sur une colonne d'une dataframe avec pandas en utilisant get_dummies() ?
Créer deux dataframes avec pandas
Créons d'abord un faux jeu de données stocké dans une dataframe pandas :
import pandas as pdimport randomimport numpy as npcategorical_data_1 = ['Paris', 'London', 'New-York', 'Berlin', 'Moscow']categorical_data_2 = ['M', 'M', 'F', 'F', 'F']data = np.arange(100)data = data.reshape((25,4))location_list = [random.choice(categorical_data_1) for i in range( data.shape[0] )]meta_list = [random.choice(categorical_data_2) for i in range( data.shape[0] )]label_list = [random.choice([0,1]) for i in range( data.shape[0] )]df = pd.DataFrame(data,columns=['A','B','C','D'])df['Location'] = location_listdf['Gender'] = meta_listdf['Label'] = label_listprint(df)
donne par exemple :
A B C D Location Gender Label0 0 1 2 3 Moscow M 11 4 5 6 7 Berlin M 12 8 9 10 11 London F 03 12 13 14 15 New-York F 14 16 17 18 19 London M 05 20 21 22 23 Paris F 06 24 25 26 27 New-York F 17 28 29 30 31 New-York F 08 32 33 34 35 Paris M 19 36 37 38 39 London M 010 40 41 42 43 Moscow F 111 44 45 46 47 Moscow F 112 48 49 50 51 Moscow M 113 52 53 54 55 New-York M 114 56 57 58 59 Berlin M 115 60 61 62 63 New-York M 116 64 65 66 67 Moscow F 017 68 69 70 71 Berlin F 018 72 73 74 75 Berlin M 019 76 77 78 79 London F 020 80 81 82 83 New-York M 121 84 85 86 87 Moscow M 022 88 89 90 91 Moscow M 123 92 93 94 95 New-York F 024 96 97 98 99 New-York M 0
Fractionner la dataframe
Séparons le dataframe en deux dataframes :
df_train = df.sample(frac=0.8, random_state=42)df_test = df.drop(df_train.index)print(df_train)print(df_test)
donne
A B C D Location Gender Label8 32 33 34 35 Paris M 116 64 65 66 67 Moscow F 00 0 1 2 3 Moscow M 123 92 93 94 95 New-York F 011 44 45 46 47 Moscow F 19 36 37 38 39 London M 013 52 53 54 55 New-York M 11 4 5 6 7 Berlin M 122 88 89 90 91 Moscow M 15 20 21 22 23 Paris F 02 8 9 10 11 London F 012 48 49 50 51 Moscow M 115 60 61 62 63 New-York M 13 12 13 14 15 New-York F 14 16 17 18 19 London M 020 80 81 82 83 New-York M 117 68 69 70 71 Berlin F 021 84 85 86 87 Moscow M 018 72 73 74 75 Berlin M 024 96 97 98 99 New-York M 0
et
A B C D Location Gender Label6 24 25 26 27 New-York F 17 28 29 30 31 New-York F 010 40 41 42 43 Moscow F 114 56 57 58 59 Berlin M 119 76 77 78 79 London F 0
respectivement
One-Hot Encoding avec pandas get_dummies()
Pour encoder des données catégorielles une solution est d'utiliser pandas.get_dummies:
df_train_e = pd.get_dummies(df_train, columns=['Location','Gender'], prefix=['Location','Gender'])print(df_train_e)
donne
A B C D Label Location_Berlin Location_London Location_Moscow \8 32 33 34 35 1 0 0 016 64 65 66 67 0 0 0 10 0 1 2 3 1 0 0 123 92 93 94 95 0 0 0 011 44 45 46 47 1 0 0 19 36 37 38 39 0 0 1 013 52 53 54 55 1 0 0 01 4 5 6 7 1 1 0 022 88 89 90 91 1 0 0 15 20 21 22 23 0 0 0 02 8 9 10 11 0 0 1 012 48 49 50 51 1 0 0 115 60 61 62 63 1 0 0 03 12 13 14 15 1 0 0 04 16 17 18 19 0 0 1 020 80 81 82 83 1 0 0 017 68 69 70 71 0 1 0 021 84 85 86 87 0 0 0 118 72 73 74 75 0 1 0 024 96 97 98 99 0 0 0 0Location_New-York Location_Paris Gender_F Gender_M8 0 1 0 116 0 0 1 00 0 0 0 123 1 0 1 011 0 0 1 09 0 0 0 113 1 0 0 11 0 0 0 122 0 0 0 15 0 1 1 02 0 0 1 012 0 0 0 115 1 0 0 13 1 0 1 04 0 0 0 120 1 0 0 117 0 0 1 021 0 0 0 118 0 0 0 124 1 0 0 1
Appliquer l'encodage à une autre dataframe
Voyons maintenant comment appliquer l'encodage à une autre dataframe(appelée df_test ici):
df_test_e = pd.get_dummies(df_test, columns=['Location','Gender'], prefix=['Location','Gender'])df_test_e = df_test_e.reindex(columns = df_train_e.columns, fill_value=0)print(df_test_e)
donne alors
A B C D Label Location_Berlin Location_London Location_Moscow \6 24 25 26 27 1 0 0 07 28 29 30 31 0 0 0 010 40 41 42 43 1 0 0 114 56 57 58 59 1 1 0 019 76 77 78 79 0 0 1 0Location_New-York Location_Paris Gender_F Gender_M6 1 0 1 07 1 0 1 010 0 0 1 014 0 0 0 119 0 0 1 0
Références
- Scikit-Learn - one-hot encoding certain columns of a pandas dataframe
- One-Hot Encoding in Python with Pandas and Scikit-Learn
- Here’s All you Need to Know About Encoding Categorical Data (with Python code)
- One-hot encoding with
get_dummies() - Encoding with Pandas get_dummies
- Easy way to apply transformation from
pandas.get_dummiesto new data? - pandas.get_dummies
