如何对时间序列数据进行 K 均值聚类?我理解当输入数据是一组点时这是如何工作的,但我不知道如何用 1XM 对时间序列进行聚类,其中 M 是数据长度。特别是,我不确定如何更新时间序列数据的集群平均值。
我有一组带标签的时间序列,我想使用 K-means 算法来检查我是否会得到类似的标签。我的 X 矩阵将是 NXM,其中 N 是时间序列的数量,M 是如上所述的数据长度。
有谁知道如何做到这一点?例如,我如何修改这个 k-means MATLAB 代码,使其适用于时间序列数据?此外,我希望能够使用除欧几里得距离之外的不同距离度量。
为了更好地说明我的疑问,这是我为时间序列数据修改的代码:
% Check if second input is centroids
if ~isscalar(k)
c=k;
k=size(c,1);
else
c=X(ceil(rand(k,1)*n),:); % assign centroid randomly at start
end
% allocating variables
g0=ones(n,1);
gIdx=zeros(n,1);
D=zeros(n,k);
% Main loop converge if previous partition is the same as current
while any(g0~=gIdx)
% disp(sum(g0~=gIdx))
g0=gIdx;
% Loop for each centroid
for t=1:k
% d=zeros(n,1);
% Loop for each dimension
for s=1:n
D(s,t) = sqrt(sum((X(s,:)-c(t,:)).^2));
end
end
% Partition data to closest centroids
[z,gIdx]=min(D,[],2);
% Update centroids using means of partitions
for t=1:k
% Is this how we calculate new mean of the time series?
c(t,:)=mean(X(gIdx==t,:));
end
end