Frequency encoding replaces each category with how common it is — one column, no target involved, and no leakage to worry about.
Task: write frequency_encode(values) returning the encoding for each row, rounded to 4 decimal places.
1.0.The bet this encoding makes is that rarity itself is informative, and surprisingly often it is. A rare browser version correlates with fraud; a rare product category behaves differently from a bestseller; a rare job title in a salary model genuinely marks an unusual row. None of that needs the target to discover.
Its obvious failure mode is collisions: two categories appearing exactly the same number of times get exactly the same encoding, and the model can no longer tell them apart. In the long tail of a high-cardinality column, where thousands of categories appear once each, that's nearly everything — which is why frequency encoding is usually one signal among several rather than a replacement for the category.