This article will introduce the Gramian Angular Field in detail and show "How to Convert Time Series Data to Image" with code samples.
Gramian Angular Summation / Difference Fields (GASF / GADF) can convert time series into images so that we can use Convolutional Neural Networks (CNN) for time series data
basic concept
Before starting the introduction, I think first we should be familiar with the basic concepts of GASF / GADF. If you already know, you can skip this section.
Cartesian coordinates: Cartesian coordinates (French: les coordonnées cartésiennes) is the collective name for the Cartesian coordinate system and the oblique coordinate system. The two axes intersecting at the origin constitute a plane affine coordinate system. If the units of measure on the two number lines are equal, the affine coordinate system is called a Cartesian coordinate system. A Cartesian coordinate system with two axes perpendicular to each other is called a Cartesian Cartesian coordinate system, otherwise it is called a Cartesian oblique coordinate system. The location of a point (or other geometric shape) is determined by one or more numbers. For example, if we take a two-dimensional coordinate system, the position is determined by a pair of numbers, such as (2,3). The location is then displayed in distance from two reference lines, called the x and y axes.
Polar coordinates: It belongs to a two-dimensional coordinate system. The founder is Newton. It is mainly used in the field of mathematics. Polar coordinates refer to taking a fixed point O in the plane, called the pole, eliciting a ray Ox, called the polar axis, and then selecting a length unit and the positive direction of the angle (usually counterclockwise). Imagine a circle centered at the origin that intersects the point (2,3). We find the radius of this circle and the angle between the x-axis and the line connecting (0,0) and (2,3). In polar coordinates it would be represented as (3.6, 56.3), which is a point 3.6 units from the pole and 56.3° from the polar axis
Gram matrix: Suppose we have a set of vectors V. The Gram matrix is the inner product matrix of each pair of vectors from V. As shown in the figure below, each element in the matrix <vi, vj> is the vector product between the vectors vi and vj.
The above pictures and introductions are from Wiki and Baidu Encyclopedia. The above is a brief introduction to the basic concepts, so let's get to the point.
Gram's Corner
We will now move towards the main goal of this post, which is to understand the process of representing time series in images. In short, the process can be understood through the following three steps.
Aggregate the time series to reduce size by taking the average of each M point. This step uses Piecewise Aggregation Approximation / PAA.
A scaled value in the interval [0,1].
Polar coordinates are generated by taking the timestamp as the arccosine of the radius and scale values. This Yang can provide the value of the angle.
Generate GASF/GADF. In this step, each pair of values is added (subtracted), then cosines are taken and summed.
The language description may not be accurate, the code is used to explain in detail below
Example in Python
I've provided a Python example here to demonstrate the state of the step-by-step process of converting a time series to an image using Gram's angle fields.
import required packages
from pyts.approximation import PiecewiseAggregateApproximation
from pyts.preprocessing import MinMaxScaler
import numpy as np
import matplotlib.pyplot as plt
Generate some demo data
X = [[1,2,3,4,5,6,7,8],[23,56,52,46,34,67,70,60]]
plt.plot(X[0],X[1])
plt.title(‘Time series’)
plt.xlabel(‘timestamp’)
plt.ylabel(‘value’)
plt.show()
Piecewise Aggregate Approximation and Scaling
# PAA
transformer = PiecewiseAggregateApproximation(window_size=2)
result = transformer.transform(X)
# Scaling in interval [0,1]
scaler = MinMaxScaler()
scaled_X = scaler.transform(result)
plt.plot(scaled_X[0,:],scaled_X[1,:])
plt.title(‘After scaling’)
plt.xlabel(‘timestamp’)
plt.ylabel(‘value’)
plt.show()
Convert to polar coordinates
arccos_X = np.arccos(scaled_X[1,:])
fig, ax = plt.subplots(subplot_kw={‘projection’: ‘polar’})
ax.plot(result[0,:], arccos_X)
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks
ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line
ax.grid(True)
ax.set_title(“Polar coordinates”, va=’bottom’)
plt.show()
Gramian angular summation fields
field = [a+b for a in arccos_X for b in arccos_X]
gram = np.cos(field).reshape(-1,4)
plt.imshow(gram)
final addition
The above steps are used to illustrate the process of converting a time series to an image using Gramian Angular Summation / Difference Field. In actual use, it is not necessary to calculate polar coordinates because of the following trigonometric rules:
To calculate Cos (A + B) in the Gramian Angular Field calculation, we extend it as follows
Because we compute A and B by taking the inverse cosine of the time series values (actually on the PAA and scaled values). For other instructions, please refer to the description of GramianAngularField in the pyts library.
quote
Wang, Z., & Oates, T. (2015). Imaging time-series to improve classification and imputation. IJCAI International Joint Conference on Artificial Intelligence, 2015-January, 3939–3945.
Eamonn J Keogh and Michael J Paz- zani. Scaling up dynamic time warping for datamining applications. In Proceedings ofthe sixth ACM SIGKDD international conference on Knowledge discovery and data mining, pages 285– 289. ACM, 2000.
https://pyts.readthedocs.io/en/stable/_modules/pyts/image/gaf.html#GramianAngularField
Author: Pankaj Chejara
Edit: deephub-imba
Recommended reading:
My 2022 Internet School Recruitment Sharing
Talking about the difference between algorithm post and development post
Internet school recruitment research and development salary summary
For time series, everything you can do.
Public number: AI snail car
Stay humble, stay disciplined, stay progressive
Send [Snail] to get a copy of "Hands-on AI Project" (AI Snail Car)
Send [1222] to get a good leetcode brushing note
Send [AI Four Classics] to get four classic AI e-books