Gérer les variables catégorielles

Ingénierie des caractéristiques pour le Machine Learning en Python

Robert O'Callaghan

Director of Data Science, Ordergroove

Encoder des caractéristiques catégorielles

Ingénierie des caractéristiques pour le Machine Learning en Python

Encoder des caractéristiques catégorielles

Ingénierie des caractéristiques pour le Machine Learning en Python

Encoder des caractéristiques catégorielles

  • Encodage one-hot
  • Encodage fictif
Ingénierie des caractéristiques pour le Machine Learning en Python

Encodage one-hot

pd.get_dummies(df, columns=['Country'], 
               prefix='C')
    C_France    C_India    C_UK    C_USA
0          0          1       0        0
1          0          0       0        1
2          0          0       1        0
3          0          0       1        0
4          1          0       0        0
Ingénierie des caractéristiques pour le Machine Learning en Python

Encodage fictif

pd.get_dummies(df, columns=['Country'],
               drop_first=True, prefix='C')
     C_India    C_UK    C_USA
0          1       0        0
1          0       0        1
2          0       1        0
3          0       1        0
4          0       0        0
Ingénierie des caractéristiques pour le Machine Learning en Python

One-hot vs encodage fictif

  • Encodage one-hot : caractéristiques explicites
  • Encodage fictif : information essentielle sans redondance
Ingénierie des caractéristiques pour le Machine Learning en Python
Index Sexe
0 Homme
1 Femme
2 Homme
Index Homme Femme
0 1 0
1 0 1
2 1 0
Index Homme
0 1
1 0
2 1
Ingénierie des caractéristiques pour le Machine Learning en Python

Limiter le nombre de colonnes

counts = df['Country'].value_counts()
print(counts)
'USA'      8
'UK'       6
'India'    2
'France'   1
Name: Country, dtype: object
Ingénierie des caractéristiques pour le Machine Learning en Python

Limiter le nombre de colonnes

mask = df['Country'].isin(counts[counts < 5].index)

df['Country'][mask] = 'Other'
print(pd.value_counts(colors))
'USA'      8
'UK'       6
'Other'    3
Name: Country, dtype: object
Ingénierie des caractéristiques pour le Machine Learning en Python

À vous de gérer les variables catégorielles

Ingénierie des caractéristiques pour le Machine Learning en Python

Preparing Video For Download...