Use Gram's Angle Field (GAF) to convert time series data to images

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

0ffc4c144088052e53ef2277a9a572ed.gif

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.

41179a9d086dc28fd2e798cf7cf55616.png

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

a6acdd3edde83b062207b0a5e63f3c77.png

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.

c69fb99a312b3d8941fdbc8f4bb40849.png

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.

7681e8acf3150d23dcec756de1fe47df.png

  1. Aggregate the time series to reduce size by taking the average of each M point. This step uses Piecewise Aggregation Approximation / PAA.

  2. A scaled value in the interval [0,1].

  3. 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.

  4. 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()

4366d56a432436bcdba198770a9f333e.png

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()

395d1057f9aa7ce6e4a6508e6f527300.png

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()

3eb2171b4a98e37521724b93d66c482a.png

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)

a3c5be7d92ff83ab78a16e5782107205.png

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:

6e1736a145495401a58218199d0de552.png

To calculate Cos (A + B) in the Gramian Angular Field calculation, we extend it as follows

2ac995e24d0f8ab2ac8673bcdb60caa2.png

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

  1. Wang, Z., & Oates, T. (2015). Imaging time-series to improve classification and imputation. IJCAI International Joint Conference on Artificial Intelligence, 2015-January, 3939–3945.

  2. 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.

  3. 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

My 2021 Summary

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.

What is the spatiotemporal sequence problem? Which models are mainly used for such problems? What are the main applications?

Public number: AI snail car

Stay humble, stay disciplined, stay progressive

93311f87e1eb2688d2888e2fc8a8eeb7.png

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

Guess you like

Origin blog.csdn.net/qq_33431368/article/details/123700477