A retailer's store_id has 1,000 levels. Instead of one-hot encoding, a colleague proposes binary encoding: give each store an integer code and write that code in base 2, one column per bit. How many columns does this create, and what is its main cost for a logistic regression?
Show the answer and explanation
Correct answer: D, 10 columns, since 2¹⁰ = 1,024 ≥ 1,000; unrelated stores share bit columns, so each bit's coefficient ties together arbitrary groups of stores in the model.
Explanation Encoding
Binary encoding first maps each level to an integer code and then writes the code in base 2. With 1,000 levels you need ⌈log₂ 1,000⌉ = 10 bits, because 2⁹ = 512 is too few and 2¹⁰ = 1,024 is enough. So 10 columns replace roughly 1,000 one-hot columns.
The mapping is lossless: each store gets its own bit pattern, so nothing collides. That is the difference from the hashing trick, where unrelated levels can land in the same bucket.
The cost appears in a linear model. A logistic regression now has only 10 coefficients for store_id, one per bit, and a store's effect is the sum of the coefficients of its active bits. The bit worth 8 is on for an arbitrary mix of stores (codes 8–15, 24–31, …), so the model must give those unrelated stores a shared component of their effect. It cannot represent 1,000 independent store effects, and the similarities it imposes depend only on how the codes happened to be assigned.
| Encoding | Columns for 1,000 levels | Separate effect per level in a linear model? |
|---|---|---|
| One-hot | ~1,000 | yes |
| Binary | 10 | no, bits are shared arbitrarily |
| Single integer code | 1 | no, and it implies an order |
Tree models cope better, since a path of up to 10 splits can still isolate any single store.
Interview tip: present binary encoding as a compact middle ground for tree models when one-hot is too wide, and name target encoding or embeddings as the usual alternatives for linear and neural models.