Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Ordinal encoding is a simple way to turn ordered categorical values (like sizes) into numbers so models can use them. You’ll implement an ordinal encoder that maps each category to an integer based on a provided ordering.
Implement the function
Rules:
ordered_categories, encode it as unknown_value.Output:
| Argument | Type |
|---|---|
| values | np.ndarray |
| unknown_value | int |
| ordered_categories | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Return NumPy array
No scikit-learn or prebuilt encoders
Single function implementation only
Start by thinking: for each value, you need its position in ordered_categories (or a fallback).
Precompute a lookup dict {category: index} using enumerate(ordered_categories) to avoid repeated .index() calls.
Use dict.get(value, unknown_value) inside a list comprehension to produce the final list[int] in one pass.