A model cannot multiply a weight by the word "red", so categorical features must become numbers. One-hot encoding gives each category its own column: a row holds a 1 in its category's column and 0 everywhere else. The category list must be learned from training data, and an unseen category at prediction time should be reported, not silently encoded as all zeros.
Implement two functions.
one_hot_fit(values): returns the sorted list of distinct values, which fixes the column order.one_hot_transform(values, categories): returns a float matrix of shape (len(values), len(categories)). Column j corresponds to categories[j]. A value not in categories raises ValueError.(0, len(categories)).1.one_hot_transform define the column order, even if they are not sorted.Open one at a time. Each gives away a little more than the last.
Build a dictionary from category to column index once, then look each value up.
Set one cell per row with out[row, index] = 1.0.
No results yet
Run your code to check it against the examples, or submit to run every test.