← Volver a Academia
An instructor and a learner use pictures of vehicles to complete a column chart on paper.
Conocimiento de IANúcleo2 nov 2026· 3 min

Encoding Categories for Machine Learning

A ferry trip appears in a survey that only offered bus, walk and train. Ordinal codes imply false distances, while one-hot columns treat all categories equally but need a plan for unknown labels.

categorical encodingone-hot encodingdata literacy

Fuentes

Resumen completo

A ferry pulls into a new terminal and a passenger taps a card that records the trip as ferry. The survey that later asks how people travel to school has always offered bus, walk and train. A model trained on those three words cannot compare ferry to anything it has seen, because words must become numbers before a model can do arithmetic. Ordinal encoding assigns an integer to each label, perhaps bus = 0, walk = 1 and train = 2. The numbers are compact, but they imply a distance. A model reading 0, 1 and 2 may infer that train is twice as far from bus as walk is, even though the labels were never ranked. Nothing about walking places it numerically between bus and train.

One-hot encoding avoids that false geometry. It creates one indicator column per known category, marks the observed category with 1 and leaves the others at 0. A bus trip becomes [1, 0, 0], a walk [0, 1, 0] and a train ride [0, 0, 1]. Every pair of categories now sits at the same distance, and no arithmetic accident suggests an ordering. With the full indicator set, three categories take three columns and forty categories take forty. In any single row, most of those columns hold zeros.

Suppose a ferry answer appears in the test data. If the encoder was fitted only on the training categories of bus, walk and train, ferry has no column. A common setting maps unknown values to all zeros, [0, 0, 0]. That row means no known category was observed. It is not a fourth mode of transport and not a missing value. A model may read it as unlike every training example, which is honest but unhelpful. Fitting the mapping on training data alone keeps evaluation fair, because the test set should represent new observations the model has not seen.

A survey asking for agreement, from strongly disagree to strongly agree, is different. Here the labels genuinely are ordered, so integer codes 0 to 4 preserve real structure and one column suffices. Applying one-hot encoding would discard that order and add four extra columns. Even an ordered scale need not have equal intervals, but the direction is meaningful. The question to ask is whether the labels have a meaningful sequence. Names of browsers or regions do not. Ratings on a five-point scale do.

In a classroom, give each student a card with one commute label and three empty boxes labelled bus, walk and train. Students mark their row, then tape rows to the board. Add a ferry card and ask where its row goes. Record predictions before revealing the all-zero row. Ask pairs to explain in one sentence what that row claims. Collect the sentences and sort them into unknown category and missing answer piles. The visible rows make students' reasoning inspectable.

Encoding is a design choice with consequences for distance, width and new labels. Ordinal codes suit genuinely ordered answers. One-hot columns suit unordered names. Fitting categories on training data and documenting unknown handling keeps the numbers faithful to the survey. Ask students to name one question where each method would be the better fit, and to defend the choice using the label list in front of them.