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:

  • DataScience & Machine Learning: Where to start with Python
  • Looking further into Machine Learning using Python

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.


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)



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.



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!