bhylu.com

Advertisements

Categorical Data

Categorical features need an explicit representation before many estimators can use them. Arbitrary numeric codes can invent an ordering that the categories do not have.

Understand the idea

One-hot encoding creates separate indicator columns for known categories. handle_unknown='ignore' produces zeros for an unseen category under this configuration, which is a policy you must evaluate rather than automatically accept. Fit category discovery using training data only and keep the encoder with the model so prediction columns remain aligned.

Where to try it

Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.

A worked example

Python

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
encoder.fit([["online"], ["onsite"]])
print(encoder.transform([["online"], ["hybrid"]]).tolist())

Read the result

Online maps to its learned indicator, while the unseen hybrid value maps to all zeros in this configuration.

Try one change

Print get_feature_names_out and connect each output column with its category.

For package options, see the scikit-learn documentation.