Creating a Time-Series Analysis Using Python

Posted by Dhiraj Sharan
Dhiraj Sharan

Python is an incredibly powerful programming language. It is not only for small school projects but instead, also used for Google AI in photo recognition and other monumental projects.

Python has proved able to get the job done, be it machine learning algorithms or network protocols or user interfaces or just about anything.

In this article series, we walk through how to use Python for data use cases that are a precursor to real AI or machine learning. In the last couple of articles, we walked through how to get started:

Pocket watch and business concepts on digital background

Time-series in Python

In our previous blogs, we looked at NumPy and pandas. We saw how we could use incredibly powerful off the shelf components to do advanced mathematical operations with few lines of code. In this blog, we get a handle on coding with Python by going through a time-series example.

 

Keras 

One essential python library which we ignored in previous articles was Keras. You can find Keras in several machine learning applications, just like sklearn, which we talked about in an earlier article. Whereas NumPy and pandas have other applications outside of data science, Keras is predominantly in AI and ML.

Keras takes care of deciding models, layers, and other high-level activities associated with advanced real-life AI. You can train Keras on a single GPU or use multiple GPUs at once. Because Keras has built-in support for data parallelism, it can process large volumes of data and speed up the time needed to train.

Typically Python can directly do most things for you without having to resort to third-party toolkits like keras. For instance, simple string manipulations, integer arithmetic, and float exponentiations, to name a few, can be done directly using Python. But using toolkits make it more efficient, and also at times, functions better than written code (eliminates the possibility of bugs found in code).

Keras has created a name for itself in several NLP applications, which we will jump into more detail in coming blogs.

For now, let us dive into a real-life Python mathematics code so that we can get started on simple AI.

 

Code samples for common uses

In this code, we created a sample time series in Python, using the pandas toolkit.

#!/usr/bin/env python

import pandas as pd  

import numpy as np

times = pd.date_range('2010-10-01', periods=289, freq='5min')

gap = np.array(times)

print(gap)

~

The data frame concept in pandas is powerful, allowing us to do rolling window computations. That means we can easily calculate the mean or max of an interval of four days over two years using very little code.

Computing similar calculations by code, are usually tricky and result in buggy code if written by hand.

Of course, one thing that Python shines is in its intrinsic efficiency and economy of expression. But we must know a little bit of mathematics to extract more out of it.

 

Creating time-series with Python

import pandas as pd
from datetime import datetime
import numpy as np
date_rng = pd.date_range(start='1/1/2020', end='1/08/2020', freq='H')
df = pd.DataFrame(date_rng, columns=['date'])
df['data'] = np.random.randint(0,100,size=(len(date_rng)))
print(df)

The sample code above creates fantastic time-series data that can be used for drawing graphs or for extracting data frames for mathematical computations.

Here is a sample output.

0 2020-01-01 00:00:00 20
1 2020-01-01 01:00:00 3
2 2020-01-01 02:00:00 34
3 2020-01-01 03:00:00 36
4 2020-01-01 04:00:00 83
5 2020-01-01 05:00:00 82
6 2020-01-01 06:00:00 42
7 2020-01-01 07:00:00 24
8 2020-01-01 08:00:00 55
9 2020-01-01 09:00:00 4
10 2020-01-01 10:00:00 6
11 2020-01-01 11:00:00 27
12 2020-01-01 12:00:00 86
13 2020-01-01 13:00:00 66
14 2020-01-01 14:00:00 33
15 2020-01-01 15:00:00 36
16 2020-01-01 16:00:00 56
17 2020-01-01 17:00:00 65
18 2020-01-01 18:00:00 75
19 2020-01-01 19:00:00 2
20 2020-01-01 20:00:00 13
21 2020-01-01 21:00:00 52
22 2020-01-01 22:00:00 24
23 2020-01-01 23:00:00 50
24 2020-01-02 00:00:00 15
25 2020-01-02   01:00:00 86
26 2020-01-02 02:00:00 29

Conclusion

What is the purpose of machine learning? Machine learning allows us to analyze and understand our world better, whether it be predicting outcomes or understanding why something happened the way it did. This article gives us a glimpse into time-series using Python, which we will use to further our knowledge in the next blog.

Did you enjoy this content? Follow our linkedin page!

Looking for similar content?

Dhiraj Sharan

Written by Dhiraj Sharan

Dhiraj is the founder and CEO of Query.AI. He is an innovator and expert developer with 18 years of problem solving and solutions development in cybersecurity including over 10 patents. He has lead engineering for companies like ArcSight, HPE, Niara and Aruba.