Analyzing time series can be a heavy burden for your apps. The calculations involved can vary from reasonably simple to mind-numbingly complex and doing it the wrong way can bring even a desktop machine to its knees. We’re going to show you five different ways to perform high-performance analysis on a time series to truly add enterprise-grade power to your apps in a scalable, responsive way.

**What is Time Series Analysis?**

**Time Series** is an ordered sequence of data points which spread over a period of time. Thus it is a sequence of discrete-time data. The **Time Series Data** is monitored over constant temporal intervals. This data can be in any measurable and quantifiable parameter related to the field of business, science, finance, etc (for example: Heights of ocean tides, counts of sunspots, the daily closing value of the Dow Jones Industrial Average, etc.).

**Time Series Analysis** refers to identification of the common patterns displayed by the data over a period of time. For these tasks, experts employ specific methods to study the data’s characteristics and extract meaningful statistics that eventually aid in forecasting.

**Time Series Analysis** is beneficial and commonly used for Economic Forecasting, Yield Projection, Inventory Studies, Census Analysis, Sales Forecasting, Stock Market Analysis, Budgetary Analysis, etc.

**Why use Python for Time Series Analysis?**

- Python is a general-purpose interpreted programming language (unlike R or Matlab).
- Easy to learn and use primarily because it focuses on readability.
- It is a popular language in general, consistently appearing in the top 10 programming languages in surveys on StackOverflow (for example, the 2015 survey results).
- Python is a dynamic language and very suited to interactive development and quick prototyping with the power to support the development of large applications.
- Python is also widely used for Machine Learning and Data Science because of the excellent library support (in this post, you will learn how to implement Machine Learning for Time Series tasks).
- It means that you can perform your research and development (figuring out what models to use) in the same programming language that you use in productions, greatly simplifying the transition from development to production.

Read more here, for Why use Python for Scientific Computing:

**Delphi adds Powerful GUI Features and Functionalities to Python**

In this tutorial, we’ll build Windows Apps with extensive** Time Series Analysis** capabilities by integrating Python’s Computer Vision libraries with Embarcadero’s Delphi, using Python4Delphi (P4D).

P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python Computer Vision applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.

We’re going to cover the following…

**How to use PyDSE, statsmodels, sktime, Nitime, and tslearn Python libraries to perform Time Series Analysis**

All of them would be integrated with Python4Delphi to create Windows Apps with **Time Series Analysis** capabilities.

**What do I need to perform time series analysis?**

Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.

**Time to get Started!**

First, open and run our Python GUI using project **Demo1 **from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the **Demo1** source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

**1. How do you do Time Series Analysis using Python PyDSE?**

**PyDSE **is a toolset for Dynamic System Estimation for time series inspired by DSE in R Project. Right now, it is still in a beta state and only includes ARMA (Autoregressive Moving Average) models.

An ARMA model, or Autoregressive Moving Average model, is used to describe weakly stationary stochastic time series in terms of two polynomials. The first of these polynomials is for autoregression, the second for the moving average (Petris, 2009).

This section will guide you to combine Python4Delphi with the PyDSE library, inside Delphi and C++Builder, from installing PyDSE with pip, to create a simulation for a simple ARMA model, and checking the Autocorrelation Function and Partial Autocorrelation Function.

After installing Python4Delphi properly, you can get **PyDSE** pip or easy install to your command prompt:

1 |
pip install pydse |

Don’t forget to put the path where your **PyDSE** installed, to the System Environment Variables:

**System Environment Variable Examples**

1 2 3 |
C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Lib/site-packages C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Scripts C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38 |

The following is a code example of **PyDSE** to **create a simulation for a simple ARMA model, and checking the Autocorrelation Function and Partial Autocorrelation Function** (run this inside the lower Memo of Python4Delphi Demo01 GUI):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import pandas as pd import numpy as np import matplotlib.pylab as plt from pydse.arma import ARMA # Create a Simple ARMA Model for a 2-D Output Vector with Matrices: AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5, .3]), np.array([3, 2, 2])) MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]), np.array([2, 2, 2])) arma = ARMA(A=AR, B=MA, rand_state=0) # Create Simulation: sim_data = arma.simulate(sampleT=100) sim_index = pd.date_range('1/1/2011', periods=sim_data.shape[0], freq='d') df = pd.DataFrame(data=sim_data, index=sim_index) df.plot() # Check the Autocorrelation Function and Partial Autocorrelation Function: from statsmodels.graphics.tsaplots import plot_pacf, plot_acf sim_data = arma.simulate(sampleT=3000) sim_index = pd.date_range('1/1/2011', periods=sim_data.shape[0], freq='d') df = pd.DataFrame(data=sim_data, index=sim_index) plot_acf(df[0], lags=10) plot_pacf(df[0], lags=10) plt.show() |

**Here is the time series analysis result in the Python GUI**

Autocorrelation is a mathematical term that indicates the extent of similarity between the given time series and its delayed version over a particular time. This time series refers to a set of values of a variable/entity.

Autocorrelation helps determine the relationship between current values and the past values of an entity. By using the past and current data, the analysts can identify patterns in the data, establish relations, and plan for the future.

**2. How do you do time series analysis using Python statsmodels?**

**statsmodels** is a Python module that provides classes and functions for the estimation of many different statistical models, as well as for conducting statistical tests and statistical data exploration. statsmodels provides a complement to SciPy for statistical computations including descriptive statistics and estimation and inference for statistical models.

**statsmodels** provides an extensive list of result statistics that are available for each estimator. The results are tested against existing statistical packages to ensure that they are correct.

Time series analysis is one of the statistical tools included in the statsmodels library. We will show you the demo in this section. In this example, we will perform ARMA again, but to the real dataset, the Sunspots Data.

First, here is how you can get **statsmodels** to work with Python4Delphi to create GUI with Data Visualization and Image Processing capabilities:

1 |
pip install statsmodels |

The following is a code example of** statsmodels** for ARMA modeling for Sunspots dataset (Run this inside the lower Memo of Python4Delphi Demo01 GUI):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import numpy as np from scipy import stats import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from statsmodels.graphics.api import qqplot # Sunspots Dataset Metadata print(sm.datasets.sunspots.NOTE) # Load Data dta = sm.datasets.sunspots.load_pandas().data # Plotting Sun Activity dta.index = pd.Index(pd.date_range("1700", end="2009", freq="A-DEC")) del dta["YEAR"] dta.plot(figsize=(12,4)); # Plotting Autocorrelation and Partial Correlation fig = plt.figure(figsize=(12,8)) ax1 = fig.add_subplot(211) fig = sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40, ax=ax1) ax2 = fig.add_subplot(212) fig = sm.graphics.tsa.plot_pacf(dta, lags=40, ax=ax2) # Print Model Parameters arma_mod20 = sm.tsa.statespace.SARIMAX(dta, order=(2,0,0), trend='c').fit(disp=False) print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic) print(arma_mod20.params) # Print Model Parameters arma_mod30 = sm.tsa.statespace.SARIMAX(dta, order=(3,0,0), trend='c').fit(disp=False) print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic) print(arma_mod30.params) # Does our Model Obey the Theory? ## Calculate and Plot the Residuals print(sm.stats.durbin_watson(arma_mod30.resid)) fig = plt.figure(figsize=(12,4)) ax = fig.add_subplot(111) ax = plt.plot(arma_mod30.resid) resid = arma_mod30.resid print(stats.normaltest(resid)) fig = plt.figure(figsize=(12,4)) ax = fig.add_subplot(111) fig = qqplot(resid, line='q', ax=ax, fit=True) # Show Plots plt.show() |

**Here is an example of the statsmodels for time series analysis result in the Python GUI**

**3. How do you do time series analysis using Python sktime?**

As we have learned before, Python is widely used for Machine Learning and Data Science. In this section, we will show you the demo of the application of Machine Learning on Time Series Analysis, using sktime and Python4Delphi.

Visit our articles for more information about Python implementation for Machine Learning and Data Visualization, here:

**sktime** is a unified Python toolbox for Machine Learning with Time Series. sktime provides specialized time series algorithms and scikit-learn compatible tools to build, tune and validate time series models for multiple learning problems, including:

- Forecasting
- Time series classification
- Time series regression

This section will guide you to combine Python4Delphi with the sktime library, inside Delphi and C++Builder, from installing sktime with pip until using it for Time Series Classification!

### Here is how you can get the **sktime** library

1 |
pip install sktime |

Below is the code for **Time Series Classification using sktime** (Run the following code inside the lower Memo of Python4Delphi Demo01 GUI):

1 2 3 4 5 6 7 8 9 10 11 |
from sktime.classification.interval_based import TimeSeriesForestClassifier from sktime.datasets import load_arrow_head from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score X, y = load_arrow_head(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y) classifier = TimeSeriesForestClassifier() classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) print(accuracy_score(y_test, y_pred)) |

### An example of the sktime Python4Delphi Results

**4. How do you do Time Series Analysis using Python Nitime?**

Are you looking for software to work with more domain-specific Time Series Analysis, like Neuroscience? Then Nitime is what you are looking for.

**Nitime** is a Python library of tools and algorithms for the analysis of time-series data from neuroscience experiments. It contains an implementation of numerical algorithms for time-series analysis both in the time and spectral domains, a set of container objects to represent time-series and auxiliary objects that expose a high-level interface to the numerical machinery and make common analysis tasks easy to express with compact and semantically clear code.

Nitime is a very interesting project. You can access the complete documentation here.

First, here is how you can get **Nitime:**

1 |
pip install nitime |

Next, we will show the demo of the Nitime library that is taken from an fMRI experiment in which a subject was viewing a motion stimulus, while fMRI BOLD was recorded. The time-series in this data set were extracted from motion-sensitive voxels near area MT (a region containing motion-sensitive cells) in this subject’s brain. Read more about this experiment here.

Run the following code inside the lower Memo of Python4Delphi Demo01 GUI:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import os import numpy as np import matplotlib.pyplot as plt import nitime import nitime.timeseries as ts import nitime.analysis as nta import nitime.viz as viz TR = 2. len_et = 15 # This is given in number of samples, not time! # Load Dataset data_path = os.path.join(nitime.__path__[0], 'data') fname = os.path.join(data_path, 'event_related_fmri.csv') data = np.genfromtxt(fname, dtype=float, delimiter=',', names=True) # One Time Series t1 = ts.TimeSeries(data['bold'], sampling_interval=TR) # Another Time Series t2 = ts.TimeSeries(data['events'], sampling_interval=TR) # Event-Related Analyzer E = nta.EventRelatedAnalyzer(t1, t2, len_et) # Pass the eta and ets calculations straight into the visualization function, and plots the result: fig01 = viz.plot_tseries(E.eta, ylabel='BOLD (% signal change)', yerror=E.ets) plt.show() |

**Nitime Event-related fMRI Example**

**5. How do you do Time Series Analysis using Python tslearn?**

Beside sktime, another powerful Machine Learning toolkit for Time Series Analysis in Python is **tslearn**.

Here is the summary of the workflow for using tslearn:

**1. Getting the data in the right format**

tslearn expects a time series dataset to be formatted as a 3D NumPy array. The three dimensions correspond to the number of time series, the number of measurements per time series, and the number of dimensions respectively (n_ts, max_sz, d).

**2. Preprocess and transform the data**

tslearn has several utilities to preprocess the data. To facilitate the convergence of different algorithms, you can scale the time series first. Alternatively, to speed up training times, one can resample the data or apply a piecewise transformation.

**3. Training a model**

After getting the data in the right format, a model can be trained. Depending on the use case, tslearn supports different tasks: Classification, Clustering and Regression. Browse the extensive gallery of tslearn use cases here.

**4. More analyses**

Browse all available types of analysis here.

Here is how you can get **tslearn**:

1 |
pip install tslearn |

The following is a basic code example of **tslearn** to **perform the K-Nearest Neighbors Time Series Classifier ** (run this inside the lower Memo of Python4Delphi Demo01 GUI):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from tslearn.utils import to_time_series_dataset my_first_time_series = [1, 3, 4, 2] my_second_time_series = [1, 2, 4, 2] my_third_time_series = [1, 2, 4, 2, 2] X = to_time_series_dataset([my_first_time_series, my_second_time_series, my_third_time_series]) y = [0, 1, 1] # Time Series Data Preprocessing from tslearn.preprocessing import TimeSeriesScalerMinMax X_scaled = TimeSeriesScalerMinMax().fit_transform(X) print(X_scaled) # Training a Model: K-Nearest Neighbors Time-Series Classifier from tslearn.neighbors import KNeighborsTimeSeriesClassifier knn = KNeighborsTimeSeriesClassifier(n_neighbors=1) knn.fit(X_scaled, y) print(knn.predict(X_scaled)) |

**Here is the tslearn K-Nearest Neighbors Time Series Classifier Result in the Python GUI**

**6. Conclusion**

We already demonstrate 5 powerful Python libraries for Time Series Analysis, implemented with dummy data, Sunspots data, even domain-specific use cases like Neuroscience. All of them wrapped well inside a powerful GUI provided by Python4Delphi.

**Want to know some more? Then check out** **Python4Delphi**** which easily allows you to build Python GUIs for Windows using Delphi.**

Design. Code. Compile. Deploy.

Start Free Trial Upgrade Today

Free Delphi Community Edition Free C++Builder Community Edition