AI

# How you can Encode Periodic Time Options | by Christoph Möhl | Aug, 2023

## Considerate processing of dates, days of the week and occasions of day for deep studying and different prediction fashions.

Many prediction duties require time info as mannequin enter. Consider a regression mannequin to forecast lemonade gross sales of a retail firm (you would possibly keep in mind the instance from my article about context enriched options). The demand for refreshing drinks is clearly greater in summer time leading to a periodic gross sales curve with peaks in July/August (pondering of a location in Europe right here).

On this case, the time throughout the yr is clearly a helpful seasonal info that we should always feed into the mannequin. However how ought to we do this? Dates are troublesome, the variety of days change relying on the month (and for February even relying to the yr) they usually exist in numerous codecs:

thirteenth Jan 2023

13.01.2023

2023/03/13

To start with, we will omit the yr. To account for a seasonal impact, we simply want day and month. In a quite simple (and never very considerate) strategy, we might simply enter the month as one quantity and the day as one other quantity.

Why is {that a} unhealthy concept? The mannequin must learn the way the Christian Gregorian Calendar works (round 30 days per thirty days, 12 months per yr, leap years and so on.). With enough coaching information, a deep-learning mannequin will positive have the ability to “perceive” our calendar. “Understanding” means on this case: The mannequin can infer the relative time place throughout the yr from month and date inputs. However we should always make studying as simple as potential for our mannequin and take this job on our shoulders (at the very least we already understand how the calendar works). We make use of Python’s datetime library and calculate the relative time throughout the yr with fairly easy logic:import datetime as

`from datetime import datetimeimport calendaryr = 2023month = 12day = 30passed_days = (datetime(yr, month, day) - datetime(yr, 1, 1)).days + 1nr_of_days_per_year= 366 if calendar.isleap(yr) else 365position_within_year = passed_days / nr_of_days_per_year`

The ensuing postion_within_year characteristic with a worth vary from near 0.0 (January 1) to 1.0 (December 31) is way simpler to interpret by the mannequin than the (rattling difficult) Gregorian Calendar.

However it’s nonetheless not preferrred. The position_within_year characteristic describes a “saw-tooth” sample with a tough bounce from 1.0 to 0.0 at every flip of the yr. This sharp discontinuity could be a drawback for efficient studying. December 31 and January 1 are very related dates: They’re direct neighbors and have a lot in frequent (e.g. related climate situations), they usually most likely have an analogous potential for lemonade gross sales. Nevertheless, the characteristic position_within_year doesn’t mirror this similarity for December 31 and January 1; actually, it’s as completely different as it may be.

Ideally, time factors in shut proximity to one another ought to have related time values. We one way or the other need to design a characteristic that represents the cyclical nature of the yr. In different phrases, by December 31 we should always arrive on the place the place we began on January 1. So, in fact, it is smart to mannequin the place throughout the yr because the place on a circle. We will do that by reworking position_within_year into the x and y coordinate of a unit circle.

For this we use the sine and cosine capabilities:

sin(α) = x

cos(α) = y

the place α is the the angle utilized to the circle. If the unit circle represents the yr, α represents the time throughout the yr that has already handed.

α is thus equal to the position_within_year characteristic, the one distinction being that α has a unique scale (α: 0.0¹, position_within_year: 0.01.0).

By merely scaling position_within_year to α and calculating sine and cosine, we remodel the “saw-tooth” sample to a round illustration with delicate transitions.

`import math# scale to 2pi (360 levels)alpha = position_within_year * math.pi * 2year_circle_x = math.sin(alpha)year_circle_y = math.cos(alpha)# scale between 0 and 1 (unique unit circle positions are between -1 and 1)year_circle_x = (year_circle_x + 1) / 2year_circle_y = (year_circle_y + 1) / 2time_feature = (year_circle_x, year_circle_y) # so stunning ;)`

The ensuing time_feature is a two-element vector scaled between 0 and 1 that’s simple to digest by your prediction mannequin. With a couple of strains of code, we took a number of pointless studying effort from our mannequin’s shoulders.

The unit circle mannequin may be utilized to any periodic time info comparable to day of the month, day of the week, time of the day, minute of the hour and so on. The idea may also be expanded to cyclic options exterior the time area:

• Logistics/Public Transport: Relative place of a bus on its round-trip by the town
• Biology: Standing of a cell throughout the cell cycle.
• Do you might have different use circumstances in thoughts? You’re welcome to put in writing a remark!