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.

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.

02020-01-0100:00:0020
12020-01-0101:00:003
22020-01-0102:00:0034
32020-01-0103:00:0036
42020-01-0104:00:0083
52020-01-0105:00:0082
62020-01-0106:00:0042
72020-01-0107:00:0024
82020-01-0108:00:0055
92020-01-0109:00:004
102020-01-0110:00:006
112020-01-0111:00:0027
122020-01-0112:00:0086
132020-01-0113:00:0066
142020-01-0114:00:0033
152020-01-0115:00:0036
162020-01-0116:00:0056
172020-01-0117:00:0065
182020-01-0118:00:0075
192020-01-0119:00:002
202020-01-0120:00:0013
212020-01-0121:00:0052
222020-01-0122:00:0024
232020-01-0123:00:0050
242020-01-0200:00:0015
252020-01-0201:00:0086
262020-01-0202:00:0029

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!