One-Hot Encoding a Categorical Feature in Machine Learning
A categorical feature names a group, so a number standing in for it carries no size or order unless the groups truly have one. James, Witten, Hastie, and Tibshirani describe the standard fix in Section 3.3.1 of An Introduction to Statistical Learning. For a qualitative predictor, "also known as a factor," with two levels, "we simply create an indicator or dummy variable that takes on two possible numerical values," such as 1 if a person owns a house and 0 if not. A footnote adds: "In the machine learning community, the creation of dummy variables to handle qualitative predictors is known as 'one-hot encoding.'"
With more than two levels, one dummy cannot cover them all. They write that "a single dummy variable cannot represent all possible values," so for a region with three levels they build two dummies, one for South and one for West, and "there will always be one fewer dummy variable than the number of levels." The level with no dummy, East, is the baseline. The intercept is the average for the baseline, and each dummy's coefficient is the difference between its group and the baseline. Which level serves as the baseline is a choice: the "level selected as the baseline category is arbitrary, and the final predictions for each group will be the same regardless of this choice."
Try it by hand. Take three materials with two samples each, an invented measurement for each sample: wood 9 and 11, steel 29 and 31, glass 19 and 21. The group means are 10, 30, and 20. First code the materials as integers, wood 1, steel 2, glass 3, and fit a straight line y = b0 + b1 × code by least squares. The fit is y = 10 + 5 × code, which predicts 15 for wood, 20 for steel, and 25 for glass. Training mean squared error is 51, because the line cannot give steel its high mean of 30. Now keep the same data and relabel: wood 1, glass 2, steel 3. The line becomes y = 0 + 10 × code, predicting 10, 20, and 30, and the error drops to 1. Nothing about the materials changed, only the arbitrary codes. A fit that depends on which material you happened to call 2 is measuring the labels, not the materials. With dummies, the error is 1 under both labelings. With wood as the baseline, the fit is 10 + 20 × steel + 10 × glass. With steel as the baseline, it is 30 − 20 × wood − 10 × glass. The coefficients differ, as the book says they will, but both give 10, 30, and 20. All fits were computed in exact fractions, and the data are invented.
The trap: treating an integer code as a measurement. A model that multiplies the code by a weight assumes that equal steps in the code mean equal steps in the outcome, and for unordered groups nothing supports that. A caution of my own, not from the section I read: ordered categories such as small, medium, and large are different, since an integer code at least respects their order, but it still assumes the gaps are equal, and that needs a reason before you do arithmetic on it.
Prof Rod's rule: give each category its own indicator, and in an unregularized linear regression with an intercept leave one level out as the baseline, so the labels you picked do not decide the answer. With a penalty on the coefficients, keeping every column or dropping one can change the fit, so check what your library does.