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

17 ビュー (過去 30 日間)
Konrad Warner
Konrad Warner 2020 年 11 月 1 日
コメント済み: John Kablan 2023 年 10 月 10 日
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.

回答 (2 件)

Edward Barnard
Edward Barnard 2021 年 7 月 8 日
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 2020 年 11 月 20 日
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!

Translated by