Skip to content
ML Academy

Feature engineering and preprocessing · insight-0124

Encode cyclical calendar features so December sits next to January for linear and distance models

Evidence type
practitioner heuristic
Difficulty
easy
Roles
Data scientist, ML engineer
Industries
energy, aviation

Summary: situation, action, reason, boundary

  1. Situation

    Month, weekday or hour enters a linear, kernel or distance model as an integer.

  2. Action

    Use one-hot for few levels, or sine and cosine pairs (or periodic splines) when you want smooth cyclic effects; leave integers for tree models.

  3. Reason

    Sine-cosine pairs place cyclic values on a circle so the end of the cycle is adjacent to its start.

  4. Boundary

    Linear models, k-NN, k-means and neural networks using time-of-day, weekday or month.

Symptom or risk

The model treats Sunday (6) and Monday (0) as far apart and fits a straight trend across the week.

Exceptions

Holiday and event effects are not cyclical; model them with calendar flags, such as the calendar table's holiday_name.

Trade-offs

One-hot is flexible but uses more columns; sine-cosine is compact but imposes a smooth shape.

Pitfalls

Using a single sine without its cosine, which maps two different days to the same value.

Lessons and concepts