liveBook · Manning

OneHotEncoder

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

OneHotEncoder is a scikit-learn transformer that converts categorical values into one binary feature per category.

OneHotEncoder is a scikit-learn transformer that converts each unique string value in a categorical feature into a separate binary feature. It provides a practical encoding choice for machine-learning models. section 6.1.3

Use one-hot encoding with these trade-offs in mind:

  • Supports both linear models and tree-based models. section 6.1.3
  • Can create memory-intensive sparse matrices when categories have high cardinality. section 6.1.3
  • Differs from OrdinalEncoder, which maps categories to ordered numeric values, and from embeddings, which learn lower-dimensional representations. section 6.1.3
Listing 6.3 Creating a high cardinality categorical feature
def bin_2_cat(feature, bins=100):
    min_value = feature.min()
    bin_size = (feature.max() - min_value) / bins
    bin_values = (feature - min_value) / bin_size
    return bin_values.astype(int)                         #1
    
data['coordinates'] = (
    bin_2_cat(data['latitude']) * 1000 
    + bin_2_cat(data['longitude']
)                                                         #2
high_card_categorical += ['coordinates']
 
print(data[high_card_categorical].nunique())              #3

Inspect how latitude and longitude bins can form a high-cardinality coordinates feature, illustrating when one-hot output may become costly. section 6.1.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines the transformation and compares trade-offs, but stops short of showing a runnable OneHotEncoder configuration.
Examples
Uses unique string categories as binary features and contrasts them with high-cardinality neighbourhood and coordinates features.
What it assumes
Assumes categorical features and machine-learning models, while distinguishing encoding from embeddings.
What you can do after
Apply it via scikit-learn to represent categorical predictors as binary machine-learning features.
How it connects
Connects categorical feature encoding with OrdinalEncoder, LabelEncoder, embeddings, linear models, tree-based models, and target encoding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.