我如何做时间序列数据的K均值聚类?
我知道当输入数据是一组点时,这是如何工作的,但是我不知道如何使用1XM对时间序列进行聚类,其中M是数据长度。特别是,我不知道如何更新时间序列数据的集群的平均值。
我有一组标签的时间序列,我想使用K-means算法来检查我是否会收到类似的标签。我的X矩阵将是N×M,其中N是时间序列数,M是如上所述的数据长度。
有谁知道如何做到这一点?例如,我如何修改this k-means MATLAB code,以便它可以用于时间序列数据?此外,除了欧几里德距离之外,我还想使用不同的距离度量。
为了更好的说明我的疑虑,这里是我修改的时间序列数据的代码:
% 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