One-Hot Encoding
One-Hot Encoding
Encode a categorical column while preserving an ordered vocabulary. Categories in one fitting column are hashable and mutually sortable (for example, all strings or all integers).
Functions to implement
get_categories(column): return the sorted unique categories.encode_value(value,categories): return one coordinate per supplied category. A known value has exactly one 1. An unknown value returns all zeros; do not change the vocabulary.one_hot_encode(column): fit categories withget_categories, encode each row, and return(encoded_matrix,categories). Empty input returns([],[]).decode(one_hot_row,categories): return the category at the 1's index, or None for an all-zero row. Inputs have the correct width and at most one 1; other malformed rows are outside this exercise.
Fit and reuse
matrix, categories = one_hot_encode(["cat","dog","cat","bird"])
# categories = ["bird","cat","dog"]
# matrix = [[0,1,0],[0,0,1],[0,1,0],[1,0,0]]
decode([0,1,0],categories) # "cat"
# Transform new values with the saved vocabulary:
[encode_value(v,categories) for v in ["dog","fox"]]
# [[0,0,1],[0,0,0]]
decode([0,0,0],categories) # None
Do not call one_hot_encode again to transform a held-out batch: that fits new categories and can change both width and column meaning. Unknown encodings retain the saved width but cannot recover the original unseen value.
Run tests to see results
No issues detected