liveBook · Manning

One-hot encoding

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

One-hot encoding represents each category of a categorical variable as a separate binary feature.

One-hot encoding converts a categorical feature into binary features: one feature for each category value. It lets tabular machine-learning pipelines use low-cardinality categories alongside numeric columns, without treating category labels as continuous quantities. section 4.2.2

Choose it after inspecting distinct values and deciding whether a field is categorical. In an auto dataset, `origin` and possibly `cylinders` can be treated as categories; domain knowledge may instead make `cylinders` ordinal and `model_year` a representation of time progress. section 2.4.2

Encoding Airbnb categories in a pipeline

Create `OneHotEncoder(handle_unknown='ignore')`, then place it in a `ColumnTransformer` for low-cardinality categorical columns while imputing numeric columns separately. The transformed Airbnb data becomes a float NumPy array with 13 columns because each categorical value becomes its own feature. Unseen values are ignored rather than causing a failure. section 4.2.2

Listing 4.2 Setting up building blocks for tabular learning pipelines
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import OrdinalEncoder
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
 
categorical_onehot_encoding = OneHotEncoder(
       handle_unknown='ignore')                       #1
numeric_passthrough = SimpleImputer(
       strategy="constant", fill_value=0)             #2
numeric_standardization = Pipeline([
       ("imputation", SimpleImputer(strategy="constant", fill_value=0)),
       ("standardizing", StandardScaler())
       ])                                             #3

This reusable setup pairs one-hot encoding for categorical features with imputation and standardization options for numeric features. section 4.2.2

Use alternative representations when the feature or model calls for them:

  • Treat genuinely ordered fields as ordinal features when domain meaning supports an order, rather than automatically expanding them into binaries. section 2.4.2
  • Consider embeddings for categorical variables when continuous numerical representations are appropriate. section 2.4.2
  • Use an encoding layer in a neural network when categorical values should be converted directly during training; linear models commonly use one-hot encoding. section 12.1

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

How deep it goes
Defines the binary transformation and implements it with `OneHotEncoder` and `ColumnTransformer`, without detailing its internal algorithm.
Examples
Uses Auto MPG fields such as origin and cylinders, plus an Airbnb pipeline producing 13 transformed columns.
What it assumes
Assumes ability to inspect tabular feature values and use scikit-learn pipelines while explaining the categorical-versus-ordinal decision.
What you can do after
Apply it via scikit-learn `OneHotEncoder` and `ColumnTransformer`, including safe handling of previously unseen values.
How it connects
Connects categorical variables with ordinal features, embeddings, missing-value imputation, standardization, linear models, and neural networks.

Also explained in

Compare how 6 books explain one-hot 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.