Cyclic Encoding
Cyclic Encoding
Many features are cyclic: hours of the day wrap from 23 back to 0, days of the week wrap from Sunday back to Monday, and months wrap from December back to January. Encoding these as plain integers (0-23, 0-6, 1-12) breaks the cyclic structure because the model sees 23 and 0 as far apart, when they are actually adjacent. Cyclic encoding maps each value to a (sin, cos) pair on the unit circle, preserving the cyclic distance.
Given a list of values and the period of the cycle, encode each value as a [sin, cos] pair.
Algorithm
For each value v with period P:
θ=P2π⋅vencoded=[sin(θ),cos(θ)]Examples
Input:
values = [0, 6, 12, 18], period = 24
Output:
[[0.0, 1.0], [1.0, 0.0], [0.0, -1.0], [-1.0, 0.0]]
Hours on a 24-hour clock mapped to the unit circle. Midnight (0) and 6 AM (6) are a quarter-turn apart, and midnight is correctly close to 11 PM (23).
Input:
values = [0, 1, 2, 3], period = 4
Output:
[[0.0, 1.0], [1.0, 0.0], [0.0, -1.0], [-1.0, 0.0]]
Four values evenly spaced around the circle (quarters). Value 3 is adjacent to both value 2 and value 0 (the cycle wraps).
Hint 1
For each value v, compute the angle: angle = 2 * math.pi * v / period. Then append [math.sin(angle), math.cos(angle)] to the result list.
Hint 2
Remember to use radians (math.pi), not degrees. The formula maps value 0 to angle 0 and value equal to the period back to angle 2*pi (same point on the circle).
Requirements
- Convert each value to an angle using 2 * pi * value / period
- Return [sin(angle), cos(angle)] for each value
- Return a list of [sin, cos] pairs
Constraints
- period > 0
- Values are non-negative numbers
- Return a list of [float, float] pairs
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number
Cyclic Encoding
Cyclic Encoding
Many features are cyclic: hours of the day wrap from 23 back to 0, days of the week wrap from Sunday back to Monday, and months wrap from December back to January. Encoding these as plain integers (0-23, 0-6, 1-12) breaks the cyclic structure because the model sees 23 and 0 as far apart, when they are actually adjacent. Cyclic encoding maps each value to a (sin, cos) pair on the unit circle, preserving the cyclic distance.
Given a list of values and the period of the cycle, encode each value as a [sin, cos] pair.
Algorithm
For each value v with period P:
θ=P2π⋅vencoded=[sin(θ),cos(θ)]Examples
Input:
values = [0, 6, 12, 18], period = 24
Output:
[[0.0, 1.0], [1.0, 0.0], [0.0, -1.0], [-1.0, 0.0]]
Hours on a 24-hour clock mapped to the unit circle. Midnight (0) and 6 AM (6) are a quarter-turn apart, and midnight is correctly close to 11 PM (23).
Input:
values = [0, 1, 2, 3], period = 4
Output:
[[0.0, 1.0], [1.0, 0.0], [0.0, -1.0], [-1.0, 0.0]]
Four values evenly spaced around the circle (quarters). Value 3 is adjacent to both value 2 and value 0 (the cycle wraps).
Hint 1
For each value v, compute the angle: angle = 2 * math.pi * v / period. Then append [math.sin(angle), math.cos(angle)] to the result list.
Hint 2
Remember to use radians (math.pi), not degrees. The formula maps value 0 to angle 0 and value equal to the period back to angle 2*pi (same point on the circle).
Requirements
- Convert each value to an angle using 2 * pi * value / period
- Return [sin(angle), cos(angle)] for each value
- Return a list of [sin, cos] pairs
Constraints
- period > 0
- Values are non-negative numbers
- Return a list of [float, float] pairs
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number