【问题标题】:Clustering similar time series?聚类相似的时间序列?
【发布时间】:2020-02-09 23:26:09
【问题描述】:

我有 10-20k 个不同的时间序列(24 维数据 - 一天中每个小时的一列),我对聚集表现出大致相同活动模式的时间序列感兴趣。

我最初开始实施动态时间规整 (DTW) 是因为:

  1. 并非我的所有时间序列都完全对齐
  2. 出于我的目的,两个稍微偏移的时间序列应该被认为是相似的
  3. 形状相同但尺度不同的两个时间序列应该被认为是相似的

我在使用 DTW 时遇到的唯一问题是它似乎无法很好地扩展——fastdtw 在 500x500 距离矩阵上花费了大约 30 分钟。

还有哪些其他方法可以帮助我满足条件 2 和 3?

【问题讨论】:

  • stats.stackexchange.com 可能更合适...我希望您必须更加具体,天真地这样做不会扩展 20k**2 * (num shifts + 数量级)**2。这听起来有点像基因组学中的“序列比对”,它们的数据非常不同,但它可能会帮助您获得一些想法
  • 你用的是什么聚类算法?
  • 看看k-Shape clustering,Python 实现herehere。如果您可以/想要检查其他语言,dtwclust 中的 R 实现是多线程的。
  • 也许这里的问题是时间序列的规模,所以我建议减少你的时间序列的维度。看看SAX 它将您的时间序列解压缩为字符串字符并仍然保持行为。之后,您可以简单地使用任何类型的集群 - 当然也可以使用 DTW,它应该会更快

标签: python machine-learning time-series cluster-analysis dtw


【解决方案1】:

如果您将时间序列分解为趋势、季节性和残差,ARIMA 就可以完成这项工作。之后,使用 K-Nearest Neighbor 算法。然而,计算成本可能很昂贵,主要是由于 ARIMA。

在 ARIMA 中:

from statsmodels.tsa.arima_model import ARIMA

model0 = ARIMA(X, dates=None,order=(2,1,0))
model1 = model0.fit(disp=1)

decomposition = seasonal_decompose(np.array(X).reshape(len(X),),freq=100)
### insert your data seasonality in 'freq'

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

作为@Sushant 评论的补充,您可以分解时间序列,并可以检查以下 4 个图中的一个或全部的相似性:数据、季节性、趋势和残差。

然后是数据示例:

import numpy as np
import matplotlib.pyplot as plt
sin1=[np.sin(x)+x/7 for x in np.linspace(0,30*3,14*2,1)]
sin2=[np.sin(0.8*x)+x/5 for x in np.linspace(0,30*3,14*2,1)]
sin3=[np.sin(1.3*x)+x/5 for x in np.linspace(0,30*3,14*2,1)]
plt.plot(sin1,label='sin1')
plt.plot(sin2,label='sin2')
plt.plot(sin3,label='sin3')
plt.legend(loc=2)
plt.show()

X=np.array([sin1,sin2,sin3])

from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
distances, indices = nbrs.kneighbors(X)
distances

你会得到相似度:

array([[ 0.        , 16.39833107],
       [ 0.        ,  5.2312092 ],
       [ 0.        ,  5.2312092 ]])

【讨论】:

  • 嗨,你没有提到分解的使用。我假设您的意思是说我们可以分别计算不同数据点趋势的相似性以及季节性?
  • 是的@Sushant,检查我添加到答案中的补充。
  • 是的。谢谢。
猜你喜欢
  • 2021-01-22
  • 2015-07-22
  • 2015-04-05
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 2013-12-13
  • 2022-12-12
相关资源
最近更新 更多