这篇文章将会详细介绍格拉姆角场 (Gramian Angular Field),并通过代码示例展示“如何将时间序列数据转换为图像”。
Gramian Angular Summation / Difference Fields (GASF / GADF)可以将时间序列转换成图像,这样我们就可以将卷积神经网络 (CNN) 用于时间序列数据
基本概念
在开始介绍之前,我认为首先我们应该熟悉一下 GASF / GADF 的基本概念。如果您已经知道,可以跳过本节。
笛卡尔坐标:笛卡尔坐标系(Cartesian coordinates,法语:les coordonnées cartésiennes)就是直角坐标系和斜坐标系的统称,相交于原点的两条数轴,构成了平面仿射坐标系 。如两条数轴上的度量单位相等,则称此仿射坐标系为笛卡尔坐标系。两条数轴互相垂直的笛卡尔坐标系,称为笛卡尔直角坐标系,否则称为笛卡尔斜角坐标系。点(或其他几何形状)的位置由一个或多个数字确定。例如,如果我们采用二维坐标系,则位置由一对数字确定,例如 (2,3)。然后在距两条参考线(称为 x 轴和 y 轴)的距离上显示该位置。



以上图片和介绍来自维基和百度百科,以上就是基本概念的简单介绍,那么咱们开始进入正题。
格拉姆角场
现在我们将朝着这篇文章的主要目标前进,即理解在图像中表示时间序列的过程。简而言之,可以通过以下三个步骤来理解该过程。

- 通过取每个 M 点的平均值来聚合时间序列以减小大小。此步骤使用分段聚合近似 ( Piecewise Aggregation Approximation / PAA)。
- 区间[0,1]中的缩放值。
- 通过将时间戳作为半径和缩放值的反余弦(arccosine)来生成极坐标。这杨可以提供角度的值。
- 生成GASF / GADF。在这一步中,将每对值相加(相减),然后取余弦值后进行求和汇总。
语言描述可能不太准确,下面使用代码详细进行解释
Python 中的示例
我在这里提供了一个 Python 示例,以演示使用格拉姆角场将时间序列转换为图像的逐步过程的状态。
导入需要的包
from pyts.approximation import PiecewiseAggregateApproximation from pyts.preprocessing import MinMaxScaler import numpy as np import matplotlib.pyplot as plt
讯享网
生成一些demo数据
讯享网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()

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

讯享网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()


field = [a+b for a in arccos_X for b in arccos_X] gram = np.cos(field).reshape(-1,4) plt.imshow(gram)

最后补充
上述步骤用于说明使用 Gramian Angular Summation / Difference Field 将时间序列转换为图像的过程。在实际使用时中可以不需要计算极坐标,这是因为以下的三角函数规则:


引用
- 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
作者:Pankaj Chejara
互联网校招研发薪资汇总
公众号:AI蜗牛车
保持谦逊、保持自律、保持进步

发送【蜗牛】获取一份《手把手AI项目》(AI蜗牛车著)
发送【AI四大名著】获取四本经典AI电子书

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/110090.html