Cyclical Feature Engineering
...explained with usage and code.
Scrape any website’s DNA with Firecrawl Branding Format
You can now extract complete brand DNA from any website, including color schemes, logos, frameworks, and more in one API call to Firecrawl.
Perfect for coding agents to clone or match existing site aesthetics.
Try it out now on the playground or API today →
Thanks to Firecrawl for partnering today!
Cyclical feature encoding
Features in typical ML datasets include:
Numerical features like age, income, transaction amt, etc.
Categorical features like t-shirt size, income groups, age groups, etc.
In addition to this, datasets also have cyclical features, i.e., features with a recurring pattern.
Unlike other features that progress continuously (or have no inherent order), cyclical features exhibit periodic behavior.
For instance, here are some common examples of cyclical features.
hour of a day (0 -> 1 -> .. -> 23 -> repeats from 0)
day of the week (M -> T -> .... -> S -> S -> repeats from M)
month of the year
If we don’t utilize appropriate feature engineering techniques for such features, we will lose some really critical information.
To understand better, consider the ‘hour of the day’ feature.
Realistically speaking, the ideal feature representation must satisfy two properties:
The values “23” and “0” must be close to each other.
Moreover, the distance between “0” and “1” must be the same as that between “23” and “0”.
However, the standard linear representation does not fulfill these properties because:
The value “23” is far from “0”
And the distance property isn’t satisfied either.
One of the most common techniques to encode such a feature is using trigonometric functions, specifically, sine and cosine.
They are helpful because both are periodic, bounded, and defined for all real values.
For instance, when representing the linear ‘hour of a day’ feature as a cyclical feature, the central angle (2π) will denote 24 hours.
And the feature can be encoded as shown in the image below:
This way, the engineered feature satisfies both the properties we discussed earlier.
The value “23” is close to “0”
The distance between “0” and “1” is the same as that between “23” and “0”
As depicted above, the distance between the cyclical feature representation of “23” and “0” is the same as the distance between “0” and “1”.
Had it been the day-of-the-week instead, the central angle (2π) must have represented 7 days.
The same idea can be extended to all sorts of cyclical features you may find in your datasets, like:
Wind direction: N, NE, E, SE, S, SW, W, NW, and then back to N.
Phases of the moon: new moon, first quarter, full moon, and last quarter, can be represented as categories with a cyclical order.
Seasons: spring, summer, fall, and winter, are categorical features with a cyclical pattern since they repeat annually.
This way, the model will find it easier to utilize the engineered features for modeling.
We covered 11 more techniques to supercharge ML models here →
👉 Over to you: What are some other ways to handle such features?
Thanks for reading!









