One-hot encoding turns each category into its own binary indicator. A row activates the indicator for its observed category, avoiding the artificial ordering imposed by ordinal encoding. section 8.1.2
Use one-hot encoding as an unsupervised feature-engineering step before model training:
- Choose the categorical features and specify their possible categories. section 8.1.2
- Create one binary column for each category, then concatenate the resulting indicators across features. section 8.1.2
- Fit the transformation using features rather than targets, keeping it separate from supervised target-statistics encoding. section 8.1
Color and size
For rows containing color and size, scikit-learn's OneHotEncoder produces seven indicators when color has three categories and size has four. A row such as red and M activates the red and M positions while leaving the others at zero. section 8.1.2
import numpy as np
X = np.array([['red', 'M'],
['green', 'L'],
['red', 'S'],
['blue', 'XL'],
['blue', 'S'],
['green', 'XL'],
['blue', 'M'],
['red', 'L']])
Inspect the small color-and-size input before comparing it with the one-hot matrix produced by the encoder. section 8.1.2
Encoding choice can affect model performance and interpretability. Across the reported random-forest, LightGBM, and XGBoost comparisons, one-hot encoding provides competitive balanced-accuracy and F1 results, though its relative advantage depends on the ensemble. section 8.3.3





