How to performe k-medoids with pre-calculated distance matrix

19 views (last 30 days)
I want to performe k-mediods clustering with dtw distance on time series data and compare the cluster performance with the elbow-method.
The following script is doing fine:
klist = 2 : 20;
totSum = NaN;
for i = klist
[~,~,sumd] = kmedoids(X,i,'Distance',@dtw_dist);
totSum(i) = sum(sumd);
disp('done')
end
figure
plot(1:length(totSum),totSum) % plot of totals versus number (same as index)
except it takes a lot of time, because the distance matrix is large (23725x23725 double) and used 19 times .
So I pre-calculate the matrix ones (~12h):
dtwD = pdist(X,@(Xi,Xj) dtw_dist(Xi,Xj));
function d = dtw_dist(Xi, Xj, varargin)
[m,~] = size(Xj);
% preallocate
d = zeros(m,1);
for j=1:m
d(j) = dtw(Xi, Xj(j,:), varargin{:});
end
But is it usefull at all to speed up the clustering? Can I use the distance matrix with k-medoids? Maybe over linkage
clustTree = linkage(dtwD,'average');
that gives me a custertree? I couldnt figuere it out.

Answers (2)

Edward Barnard
Edward Barnard on 8 Jul 2021
It does seem to be possible to use a distance matrix with kmedoids with this little hack.
As far as I am aware, the values passed in X are only used as arguments to the distance function and therefore we can just pass a list of indices in X which are then used to index into the distance matrix.
N = 100;
D = zeros(100, 100); % This is the distance matrix.
K = 7;
[idx, C, sumd] = kmedoids((1:N)', K, 'Distance', @(ZI, ZJ) D(ZJ, ZI));

Aditya Patil
Aditya Patil on 20 Nov 2020
Currently, it's not possible to pass distance matrix to kmedoids. I have brought this issue to the notice of concerned people.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!