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





