1 | %MODECLUST Clustering by mode-seeking in feature space |
---|
2 | % |
---|
3 | % [LAB,J] = MODECLUST(A,K) |
---|
4 | % |
---|
5 | % INPUT |
---|
6 | % A Dataset |
---|
7 | % K Number of neighbours to search for local mode (default: 10) |
---|
8 | % |
---|
9 | % OUTPUT |
---|
10 | % LAB Cluster assignments, 1..K |
---|
11 | % J Indices of modal samples |
---|
12 | % |
---|
13 | % DESCRIPTION |
---|
14 | % A K-NN modeseeking method is used to assign objects to their nearest mode. |
---|
15 | % The nearest neighbor implementation uses the ANN Matlab Wrapper package. |
---|
16 | % It should be in the path. If needed download it from |
---|
17 | % http://webscripts.softpedia.com/scriptDownload/ANN-MATLAB-Wrapper-Download-33976.html |
---|
18 | % |
---|
19 | % LITERATURE |
---|
20 | % Cheng, Y. "Mean shift, mode Seeking, and clustering", IEEE Transactions |
---|
21 | % on Pattern Analysis and Machine Intelligence, vol. 17, no. 8, pp. 790-799, |
---|
22 | % 1995. |
---|
23 | % |
---|
24 | % SEE ALSO |
---|
25 | % MAPPINGS, DATASETS, KMEANS, HCLUST, KCENTRES, PROXM |
---|
26 | |
---|
27 | % Copyright: R.P.W. Duin, r.p.w.duin@prtools.org |
---|
28 | % Faculty EWI, Delft University of Technology |
---|
29 | % P.O. Box 5031, 2600 GA Delft, The Netherlands |
---|
30 | |
---|
31 | |
---|
32 | function [assign,J] = modeclust(a,k) |
---|
33 | |
---|
34 | prtrace(mfilename); |
---|
35 | |
---|
36 | % ANNQUERY needs to be in the path |
---|
37 | annquerycheck; |
---|
38 | |
---|
39 | % prepare data |
---|
40 | if (nargin < 2), k = 10; end |
---|
41 | m = size(a,1); |
---|
42 | b = (+a)'; |
---|
43 | |
---|
44 | % Run the k-NN search, indices in J, distances in d |
---|
45 | [J,d]= annquery(b,b,k); |
---|
46 | |
---|
47 | % density estimate from distance to most remote neighbor |
---|
48 | f = 1./(max(d,[],1)+realmin)'; |
---|
49 | |
---|
50 | % Find local indices of local density maxima in neighbourhood. |
---|
51 | [dummy,I] = max(reshape(f(J),size(J))); |
---|
52 | |
---|
53 | % Translate back to indices in all the data. N will contain the |
---|
54 | % dataset index of their initial mode estimate in the K-neighbourhood. |
---|
55 | N = J(I+[0:k:k*(m-1)]); |
---|
56 | |
---|
57 | % Climb the mode |
---|
58 | % Re-assign samples to the sample temporily mode is assigned to. |
---|
59 | % Iterate until assignments don't change anymore. Samples that then point |
---|
60 | % to themselves are modes; all other samples point to the closest mode. |
---|
61 | |
---|
62 | M = N(N); |
---|
63 | while (any(M~=N)) |
---|
64 | N = M; M = N(N); |
---|
65 | end |
---|
66 | |
---|
67 | % Use renumlab to obtain assignments 1, 2, ... and the list of unique |
---|
68 | % assignments (the modes). |
---|
69 | [assign,J] = renumlab(M'); |
---|
70 | |
---|
71 | return |
---|