• AIPressRoom
  • Posts
  • Find out how to Encode Periodic Time Options | by Christoph Möhl | Aug, 2023

Find out how to 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 data as mannequin enter. Consider a regression mannequin to forecast lemonade gross sales of a retail firm (you may keep in mind the instance from my article about context enriched features). The demand for refreshing drinks is clearly increased in summer season leading to a periodic gross sales curve with peaks in July/August (considering of a location in Europe right here).

On this case, the time throughout the yr is clearly a beneficial seasonal data that we must always feed into the mannequin. However how ought to we try this? Dates are tough, the variety of days change relying on the month (and for February even relying to the yr) and so they exist in varied codecs:

thirteenth Jan 2023

13.01.2023

2023/03/13

To start with, we are able to 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} dangerous thought? The mannequin must find out how the Christian Gregorian Calendar works (round 30 days per 30 days, 12 months per yr, leap years and so forth.). With enough coaching knowledge, a deep-learning mannequin will positive be capable of “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 must always make studying as simple as doable for our mannequin and take this job on our shoulders (at the very least we already know the way 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 datetime
import calendar

yr = 2023
month = 12
day = 30

passed_days = (datetime(yr, month, day) - datetime(yr, 1, 1)).days + 1
nr_of_days_per_year= 366 if calendar.isleap(yr) else 365

position_within_year = passed_days / nr_of_days_per_year

The ensuing postion_within_year characteristic with a price 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.

But it surely’s nonetheless not ultimate. 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 is usually a downside 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), and so they in all probability have an analogous potential for lemonade gross sales. Nonetheless, the characteristic position_within_year doesn’t replicate this similarity for December 31 and January 1; actually, it’s as totally different as it may be.

Ideally, time factors in shut proximity to one another ought to have related time values. We by some means should design a characteristic that represents the cyclical nature of the yr. In different phrases, by December 31 we must always arrive on the place the place we began on January 1. So, in fact, it is sensible to mannequin the place throughout the yr because the place on a circle. We are able to do that by remodeling 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 distinct scale (α: 0.0¹, position_within_year: 0.01.0).

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

import math

# scale to 2pi (360 levels)
alpha = position_within_year * math.pi * 2

year_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) / 2
year_circle_y = (year_circle_y + 1) / 2

time_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 just a few traces of code, we took numerous pointless studying effort from our mannequin’s shoulders.

The unit circle mannequin could be utilized to any periodic time data equivalent to day of the month, day of the week, time of the day, minute of the hour and so forth. The idea can be expanded to cyclic options outdoors the time area:

  • Logistics/Public Transport: Relative place of a bus on its round-trip by means of the town

  • Biology: Standing of a cell throughout the cell cycle.

  • Do you could have different use instances in thoughts? You’re welcome to write down a remark!

Additional Info / Connection Factors