Ordinal Encoding
Ordinal Encoding
Ordinal encoding converts categorical variables into integers that preserve a meaningful order. Unlike one-hot encoding which treats all categories as equally different, ordinal encoding assigns integers that reflect the natural ranking: "low" < "medium" < "high" becomes 0 < 1 < 2. This is appropriate when the categories have an inherent order that models should leverage.
Given a list of categorical values and an ordered list defining the ranking, map each value to its position (0-indexed) in the ordering.
Algorithm
Build a mapping from the ordering list: ordering[i] maps to i. Then replace each value in the input with its mapped integer.
Examples
Input:
values = ["low", "medium", "high", "medium"], ordering = ["low", "medium", "high"]
Output:
[0, 1, 2, 1]
"low" is at index 0, "medium" at 1, "high" at 2. The encoded integers preserve the order.
Input:
values = ["S", "M", "L", "XL", "S"], ordering = ["S", "M", "L", "XL"]
Output:
[0, 1, 2, 3, 0]
Clothing sizes encoded as 0 through 3. This lets models understand that XL > L > M > S.
Hint 1
Create a dictionary mapping each element in the ordering to its index: {ordering[i]: i for i in range(len(ordering))}. Then map each value through this dictionary.
Hint 2
A dictionary comprehension with enumerate makes this a one-liner: mapping = {v: i for i, v in enumerate(ordering)}. Then return [mapping[v] for v in values].
Requirements
- Map each value to its 0-based index in the ordering list
- Every value in the input will appear in the ordering
- Preserve the order of the input list
- Return a list of integers
Constraints
- All values in the input appear in the ordering
- ordering contains unique elements
- Return a list of integers
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Ordinal Encoding
Ordinal Encoding
Ordinal encoding converts categorical variables into integers that preserve a meaningful order. Unlike one-hot encoding which treats all categories as equally different, ordinal encoding assigns integers that reflect the natural ranking: "low" < "medium" < "high" becomes 0 < 1 < 2. This is appropriate when the categories have an inherent order that models should leverage.
Given a list of categorical values and an ordered list defining the ranking, map each value to its position (0-indexed) in the ordering.
Algorithm
Build a mapping from the ordering list: ordering[i] maps to i. Then replace each value in the input with its mapped integer.
Examples
Input:
values = ["low", "medium", "high", "medium"], ordering = ["low", "medium", "high"]
Output:
[0, 1, 2, 1]
"low" is at index 0, "medium" at 1, "high" at 2. The encoded integers preserve the order.
Input:
values = ["S", "M", "L", "XL", "S"], ordering = ["S", "M", "L", "XL"]
Output:
[0, 1, 2, 3, 0]
Clothing sizes encoded as 0 through 3. This lets models understand that XL > L > M > S.
Hint 1
Create a dictionary mapping each element in the ordering to its index: {ordering[i]: i for i in range(len(ordering))}. Then map each value through this dictionary.
Hint 2
A dictionary comprehension with enumerate makes this a one-liner: mapping = {v: i for i, v in enumerate(ordering)}. Then return [mapping[v] for v in values].
Requirements
- Map each value to its 0-based index in the ordering list
- Every value in the input will appear in the ordering
- Preserve the order of the input list
- Return a list of integers
Constraints
- All values in the input appear in the ordering
- ordering contains unique elements
- Return a list of integers
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array