Categories Without an Invented Ranking
A roof can be glass, plastic, or mesh. Assigning those names arbitrary numbers can accidentally suggest a ranking. We need a representation that preserves category membership without inventing a numerical relationship.
12 sections · 3 questions · 2 coding challenges
The arrow keys move between sections too.
Giving Each Category Its Own Question
BOTIE
Modern preprocessing libraries document one-hot encoding as one way to create those indicators. Our roof materials are an invented example. The practical contribution we need is the representation: one fixed column per known category, with a clear policy for unfamiliar categories.
Sources for this history
Is Glass Really Twice as Much Roof as Plastic?
Botie wants to include greenhouse roof material in a temperature model. The materials are glass, plastic, and mesh. A model needs usable inputs, so assigning each name an ID seems like a reasonable start.
The IDs tell us which material a row describes. They don't measure anything about the roof. If we multiply them by one shared coefficient, however, their numerical gaps affect the prediction. We need to tell the model which material is present without making an arbitrary choice of IDs look like a measured difference.
The material names are illustrative categories with no numerical ranking.
Step 1 of 4
Start with names
The roof materials identify categories. Their order here is simply our chosen vocabulary order.
- Vocabulary
- glass
- plastic
- mesh
Step 2 of 4
Ask one question per material
Each row says whether that roof matches each vocabulary entry.
| Roof material | Glass? | Plastic? | Mesh? |
|---|---|---|---|
| glass roof | 1 | 0 | 0 |
| mesh roof | 0 | 0 | 1 |
glass roof
- Glass?
- 1
- Plastic?
- 0
- Mesh?
- 0
mesh roof
- Glass?
- 0
- Plastic?
- 0
- Mesh?
- 1
Step 3 of 4
Reuse the same column meanings
Another glass roof must receive the same representation. Changing column order changes what a fitted coefficient means.
| Roof material | Glass? | Plastic? | Mesh? |
|---|---|---|---|
| Another glass roof | 1 | 0 | 0 |
Another glass roof
- Glass?
- 1
- Plastic?
- 0
- Mesh?
- 0
Step 4 of 4
An unfamiliar roof still needs a policy
A roof made of a material absent from the vocabulary matches none of the known-material columns. An all-zero row can record that result, but it should not silently be interpreted as a known material.
| Material | Glass | Plastic | Mesh | Interpretation |
|---|---|---|---|---|
| Glass | 1 | 0 | 0 | Known category |
| Stone | 0 | 0 | 0 | No known category matched |
Glass
- Glass
- 1
- Plastic
- 0
- Mesh
- 0
- Interpretation
- Known category
Stone
- Glass
- 0
- Plastic
- 0
- Mesh
- 0
- Interpretation
- No known category matched
Indicator columns describe membership without inventing an order
Each comparison builds a separate category column. Collecting and transposing those columns puts one material's answers together in each row.
How can a roof material become numerical input?
A larger category ID would not mean a better or thicker roof. Give each material a separate membership question and inspect the resulting row for each supplied roof.
BOTIE
One indicator row per roof
| 1 | 0 | 0 |
| 0 | 0 | 1 |
| 0 | 0 | 0 |
Follow the operations
1. Roof materials
- 0glass
- 1mesh
- 2foil
2. Three training categories in fixed order
- 0glass
- 1plastic
- 2mesh
3. Set a constant
0
4. Choose an entry
glass
5. Match one category
- 01
- 10
- 20
6. Set a constant
1
7. Choose an entry
plastic
8. Match one category
- 00
- 10
- 20
9. Collect two results
1 0 0 0 0 0 10. Set a constant
2
11. Choose an entry
mesh
12. Match one category
- 00
- 11
- 20
13. Append one item
1 0 0 0 0 0 0 1 0 14. Swap rows and columns
1 0 0 0 0 1 0 0 0 15. Result
1 0 0 0 0 1 0 0 0
This construction uses exactly three vocabulary categories so the comparisons remain visible. It does not fit a model or remove redundant columns.
1. Distinguish an ID From a Quantity
A temperature difference has units and a physical interpretation. A material ID does not. Renaming glass from ID one to ID seven should not change the properties of the roof.
Some model families handle categorical IDs specially. A plain numerical coefficient does not: it will multiply the number it receives. We therefore need to consider how the model interprets the representation.
Do not conclude that every integer-coded feature is invalid. An ordered category or a categorical-aware algorithm has different assumptions. Our problem concerns unordered names passed to ordinary numerical operations.
2. Make a Column for Each Membership Question
Choose a vocabulary, then compare each material with every entry. A matching column receives one and the other columns receive zero. This is called one-hot encoding when exactly one column is active.
Column order: glass, plastic, mesh
glass → [1, 0, 0]
mesh → [0, 0, 1]The zeros do not say that a roof has no properties. They say that it is not a member of those particular categories.
3. Keep the Vocabulary Fixed After Fitting
The model's coefficients attach to column positions. If we fit with glass first and later move mesh into the first position, the arithmetic still runs while the meaning changes. Fit the vocabulary on training data and preserve its order.
An unfamiliar material needs an explicit policy. We can reserve an unknown category, reject the record for review, or use an all-zero representation with care. This workshop uses all zeros and makes that policy visible.
Vocabulary: [glass, plastic, mesh]
Unknown foil roof → [0, 0, 0]All zeros cannot identify which unfamiliar material appeared. If one known category is also represented by all zeros after dropping a reference column, unknowns become ambiguous unless handled separately.
4. Decide What the Representation Leaves Out
Separate indicators let a model learn separate contributions. They do not teach it that two materials have similar insulation or cost. If those measured properties matter, we may want additional features that describe them directly.
With an intercept in a linear model, all category columns together create a redundant description. Dropping a reference column is one way to remove that redundancy. We keep all columns here so the membership mechanism is easy to inspect.
Botie's workshop constructs the indicator table. Later modelling lessons decide how to fit coefficients to it.
5. Follow a Roof Name into a Prediction
Once the indicators exist, a linear model assigns a separate contribution to each column. The intercept supplies a shared starting prediction. Choosing a reference category removes redundant columns, but changes how the remaining coefficients should be read.
Here we retain every indicator and supply illustrative coefficients so that the routing is visible. We are applying a stated rule, not claiming these materials have measured thermal effects.
Apply contributions in the saved vocabulary order
The vocabulary is glass, plastic, mesh. Foil uses the previously declared all-zero policy.
import numpy as np
vocabulary = np.array(['glass', 'plastic', 'mesh'])
materials = np.array(['mesh', 'foil', 'glass'])
X = (materials[:, None] == vocabulary).astype(float)
weights = np.array([2., -1., -3.])
print((20 + X @ weights).tolist())[17.0, 20.0, 22.0]The unfamiliar roof receives only the intercept. That is a fallback policy, not evidence that foil behaves like an average known roof.
6. A Column with One Training Example Has Little Support
A vocabulary can contain many rare names. Giving each its own coefficient asks the model to learn effects from very few observations. Grouping rare categories into an other category or regularising the model can reduce that instability, with different tradeoffs.
Fit any frequency threshold on training data. Keep unknown and missing categories distinct if their meanings differ. If a category is a near-unique identifier, consider whether the prediction task permits remembering that identity at all, especially when evaluating on new greenhouses.
Check yourself
Questions on Parts 1 to 6
Question 1 of 3
Why are arbitrary material IDs risky for a plain numerical coefficient?
Try it with Botie
Build with Botie
Write and run Python