I'm using AgglomerativeClustering to group time-series with Dynamic Time Warping (DTW) as a metric. I get distance matrix and pass it to clustering.

#dtwMetric is the callable that accepts two observations
dist_m = sklearn.metrics.pairwise.pairwise_distances(data_frame, metric=dtwMetric)

agg = AgglomerativeClustering(n_clusters=10, affinity='precomputed', 

labels = agg.fit_predict(dist_m)

Then I can get a cluster label for each observation (time-series) in my data frame.

I need to get SARIMAX forecasting later for each cluster using only one (the "most centered") time-series from cluster, so I suppose I don't want an "average" series per cluster but one of my initial observations.

How do I select the "most centered" time-series for each cluster?


Find the one with the smallest average distance to all others.

It's called the medoid, and it is used by well known algorithms such as PAM.

  • $\begingroup$ Thanks @Anony-Mousse . Since i was using LB Keogh version (faster) of DTW and this function is non-commutative, I ended up with getting smallest average distance to all others as an average of sum of distances using all possible permutations. E.g if I have [1, 2, 3] in a cluster, then the distance for 1 is calculated as an average of d(1,2) + d(1,3) + d(2,1) + d(3,1). $\endgroup$ – lavrik Apr 27 '18 at 14:25
  • 1
    $\begingroup$ The proper way is to use the lower bounding property. So max(d(1,2),d(2,1)) is still a lower bound. You can find the candidate with the smallest lower bound, computer the exact DTW, and repeat this until there is no candidate with a lower bound less than the best refined you have found. $\endgroup$ – Anony-Mousse Apr 28 '18 at 6:07
  • 1
    $\begingroup$ Do not use LB Keogh as a substitute for DTW. It is a lower bound and should be used as such. The asymmetry will also mess with the clustering before. In fact, I don't think you should use LB_Keogh with clustering at all. You will likely need all exact distances for a reliable result. $\endgroup$ – Anony-Mousse Apr 28 '18 at 6:15
  • $\begingroup$ Thanks for you comments @Anony-Mousse. Pure DTW was computed much more slower. I think windowed version of DTW may help. $\endgroup$ – lavrik May 1 '18 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.