Lesson 45 of 60 · python
Encoding Categorical Variables for Machine Learning
Duration: 15 minutes
Encoding Categorical Variables
Machine learning models require numeric input, so we must convert categorical variables.
One‑Hot Encoding (low cardinality)
df_onehot = pd.get_dummies(df, columns=['city', 'gender'], drop_first=True)
Label Encoding with scikit‑learn
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['dept_code'] = le.fit_transform(df['department'])
Frequency Encoding
freq = df['category'].value_counts() / len(df)
df['category_freq'] = df['category'].map(freq)
Target Encoding (mean encoding) – risk of leakage
target_means = df.groupby('city')['target'].mean()
df['city_target_enc'] = df['city'].map(target_means)
Choosing the right encoder
| Cardinality | Recommended Encoder |
|---|---|
| < 10 | One‑Hot |
| 10‑100 | Frequency / Target |
| > 100 | Hashing or Embedding |