Borrar filtros
Borrar filtros

Clustering Time Series with DTW

81 visualizaciones (últimos 30 días)
Manash Sahoo
Manash Sahoo el 10 de Feb. de 2021
Respondida: Amila el 27 de Mzo. de 2023
Hi everyone.
I have ~161 time series of heart rates taken during a vocalization. I would like to sort these using the DTW algorithm. I have tried using the following to do this:
[idx,c,sumd,d] = kmedoids(dat,nclust,'Distance',@dtw);
But I end up with the following errors. I have done this before... with the exact same code. Does anyone know what I could be doing wrong?
Error using pdist (line 371)
Error evaluating distance function 'dtw'.
Error in internal.stats.kmedoidsDistObj/pdist (line 65)
out = pdist(X,distObj.distance);
Error in kmedoids>precalcDistance (line 575)
distVec = distObj.pdist(X);
Error in kmedoids>loopBody (line 546)
xDist = precalcDistance(X,distObj);
Error in kmedoids (line 362)
oneRun = loopBody(algorithm,initialize,X,k,distObj,pneighbors,numsamples,options,display,usePool,onlinePhase);
Error in dtwclassifier (line 9)
[idx,c,sumd,d] = kmedoids(dat,nclust,'Distance',@dtw);
Caused by:
Error using dtw (line 115)
The number of rows between X and Y must be equal when X and Y are matrices
Would it be correct to compute my own distance matrix using DTW, and perform K-Means clustering on that?
Any help would be absolutely amazing! Thank you!

Respuesta aceptada

Srivardhan Gadila
Srivardhan Gadila el 17 de Feb. de 2021
I think the error is due the reason that dtw function operates on 2 signals only and the output is always a scalar.
As per the description of 'Distance' Name Value Pair Argument of the kmedoids function:
"See pdist for the definition of each distance metric. kmedoids supports all distance metrics supported by pdist."
And as per the description of Distance input argument of the pdist function for custom distance:
"@distfun
Custom distance function handle. A distance function has the form
function D2 = distfun(ZI,ZJ)
% calculation of distance
...
where
  • ZI is a 1-by-n vector containing a single observation.
  • ZJ is an m2-by-n matrix containing multiple observations. distfun must accept a matrix ZJ with an arbitrary number of observations.
  • D2 is an m2-by-1 vector of distances, and D2(k) is the distance between observations ZI and ZJ(k,:)."
Hence you can't use the dtw function handle directly and you can use it as follows:
data = rand(161,20);
[idx,c,sumd,d] = kmedoids(data,10,'Distance',@dtwf);
function dist = dtwf(x,y)
% n = numel(x);
m2 = size(y,1);
dist = zeros(m2,1);
for i=1:m2
dist(i) = dtw(x,y(i,:));
end
end
  2 comentarios
Manash Sahoo
Manash Sahoo el 17 de Feb. de 2021
This is fantastic! Thank you so much.
Amila
Amila el 20 de Mzo. de 2023
Dear Srivardhan Gadila,
Thank you for giving helping, this was helped mee too
I'm trying to use same kind of test the problem is my time series (raw) are not same length i alined them with a time and make a matrics somthing like data = rand(161,20); but the problem is due to un even size (time), some raws has NaN at the 'end' and the 'beginning;
could you kindly helped me ...
my dat is look like this
NaN NaN NaN 0.5652 ... 0.9338 NaN NaN NaN
NaN NaN 0.6514 0.6486 ... 0.8719 NaN NaN NaN
NaN NaN 0.4987 0.7981 ... 0.3011 0.8229 NaN NaN
NaN NaN 0.2845 0.2204 ... 0.2360 0.6886 0.8708 NaN
NaN NaN 0.8306 0.8579 ... 0.8316 0.6039 0.3528 0.7837
NaN 0.1909 0.8184 0.9047 ... 0.4378 0.3870 0.4002 0.6733
0.7114 0.4286 0.9382 0.2920 ... 0.5071 0.0655 0.5979 0.8407
0.7834 0.0145 3.2610e-04 0.7259 ... 0.1855 0.9986 0.9115 0.4209
0.6239 0.3253 0.6404 0.3394 ... 0.1515 0.6610 0.1330 0.7037
... ... ... ... ... ... ... ... ... ...
When I run the codes all the raws that have NaN value going to omit how i can avoid this
please kindly help me

Iniciar sesión para comentar.

Más respuestas (1)

Amila
Amila el 27 de Mzo. de 2023
If anyone has any idea on how to do it please give me some advice !
Thank you

Categorías

Más información sobre Measurements and Feature Extraction en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by