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 using the python windows GUI builder in a scalable, responsive way.
Table of Contents
What is Time Series Analysis?
Time Series is an ordered sequence of data points that 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 the 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:
[crayon-676e16a3a7969239151025/]Don’t forget to put the path where your PyDSE installed, to the System Environment Variables:
System Environment Variable Examples
[crayon-676e16a3a796f069035163/]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):
[crayon-676e16a3a7970633424260/]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:
[crayon-676e16a3a7972657298608/]The following is a code example of statsmodels for ARMA modeling for Sunspots dataset (Run this inside the lower Memo of Python4Delphi Demo01 GUI):
[crayon-676e16a3a7973418145441/]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
[crayon-676e16a3a7975511867961/]Below is the code for Time Series Classification using sktime (Run the following code inside the lower Memo of Python4Delphi Demo01 GUI):
[crayon-676e16a3a7976658326942/]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:
[crayon-676e16a3a7977727860284/]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:
[crayon-676e16a3a7978274412721/]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:
[crayon-676e16a3a7979531383865/]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):
[crayon-676e16a3a797a210196162/]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.