How to Create Time Features for Data Science Models
By: Matt Przybyla
Tags: Feature Engineering, Data, Data Science, Towards Data Science, Machine Learning

Table of Contents

Introduction Creating Time Features Summary References

Introduction

One of the most difficult parts of data science modeling is utilizing time. Time can be used in a variety of ways. There is time-series-specific modeling, but you can also look at time in a different way. We will be looking into that way, which is converting time into a categorical variable(s). While the order will be stripped away after conversion, the cyclical property of time will still be able to be recognized by the machine learning algorithm you are using. For this example, we will be using the emerging algorithm, CatBoost, to correctly use these time features. Normally, time as ordered, for example, would be the hours of the day ordered from 0 to 24. However, an algorithm is still intelligent enough to see that, instead of a numerical data type, it can view the feature as a bin or category — which can also be an object or categorical data type. With that being said, keep on reading below if you would like to learn a unique approach on how to convert a timestamp into a categorical feature for the use of data science modeling.

Creating Time Features

Example of code used (same time feature values). Screenshot by Author [2].

Below, we will describe the use case or general problem, so that you can see an example of where creating time features can be useful. We will show the Python code of this conversation. Lastly, we will see what our features look like after some simple code. For future work, you could also compare models by using the time features as either categorical/object or as the numerical data type, with their respect to change in the accuracy or error.

Use Case

The use case is predicting the number of people coming into a restaurant. Whereas we could use hours of the day, or half-hour of the day as a numerical feature, we can instead, for the point of this article, use these two features as categorical features that will eventually be target encoded by the CatBoost machine learning algorithm.

Code

from datetime import datetime, timedelta

import pandas as pd

import numpy as np date_today = datetime.now()

date_range = pd.date_range(date_today — timedelta(7), date_today, freq='D') df = pd.DataFrame({'date_time_stamp': date_range})

df.head(5)

In the above code snippet, we first import the necessary libraries. Next, we create random timestamp data from now to the past seven days back. Then, we can view how the timestamps look with df.head() , all part of one, main dataframe.

df['hour'] = ['{}:{:02d}'.format(r.hour, (r.minute // 60) * 60) for r in df['date_time_stamp']]

df['30_min'] = ['{}:{:02d}'.format(r.hour, (r.minute // 30) * 30) for r in df['date_time_stamp']] df[['date_time_stamp', 'hour', '30_min']].head(5)

In this code block, we see that we are creating two time features. The first, is the hour feature, which is essentially saying — what is the hour that this very specific timestamp in? The next part is similar, but more granular, and is saying — what is the half-hour that this minute in?

For example, if the minute of the timestamp was 12:46 pm, then the 30_min would be 12:30 (essentially, a bucket of values from 12:30–1:00 pm)

df.dtypes

In this last code, we see that we can view the data types of our columns. The first would return datetime64[ns] and the two new calculated features would be used in a categorical feature list, as either an object or categorical data type.

How these features would work in the algorthim

These two features, as well as others that you can further make, can be used in a categorical feature list a part of a CatBoost model that will use target encoding for — in its simplest terms, the mean target of that specific time feature bin.

Example of half-hour/ 30_min (different time feature values). Screenshot by Author [3].

You can see in the first screenshot from above that the output has the same value for both time features, and that is because the half-hour period is still within the other side of 30. If you try this at the DateTime of 30+, then you will see that value change. That is where the second screenshot is different, immediately above. For the sake of the use case, you could most likely expect that the number of people at the restaurant would have a mean target value at the highest around 12:00 and 12:30 , as well as 6:00 and 6:30 (lunch and dinner time, respectively), while there could be some new findings of perhaps maybe 9:30 , when there is a rush of people before the restaurant closes.

Summary

Time is important and should not be ignored when developing data science models. However, it can be tricky, and can be consumed in a variety of ways. The way we looked at creating a time feature was unique in that we converted an example timestamp into two categorical features. We saw that the half hour feature or 30_min , was the same as the hour feature when the time was 13:14, but different when the time was 13:32. There are more ways, of course, where time can be used in a data science model.

To summarize, we performed the following:

* created a mock dataset * isolated a timestamp * created two features from that one timestamp * realized those two features could be used as categorical features

I hope you found my article both interesting and useful. Please feel free to comment down below if you agree with this time feature creation — why or why not? What other ways do you utilize time in your machine learning algorithms? What are the main techniques and Python libraries you use?

Please feel free to check out my profile, Matt Przybyla, and other articles, as well as reach out to me on LinkedIn.

References

[1] Photo by Lukas Blazek on Unsplash, (2017)

[2] M.Przybyla, Example of code used Screenshot (same time feature values), (2021)

[3] M.Przybyla, Example of half-hour/ 30_min Screenshot (different time feature values), (2021)