IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clus_hca.m
Go to the documentation of this file.
1 %> @brief Hierarchical Clustering
2 %>
3 %> Clustering generates another dataset whose variables will be cluster indexes. Each variable is a different scheme
4 %> corresponding to a split considering a different number of clusters. Once the distance matrix is built and the
5 %> dendrogram is derived, it is easy to generate different dendrogram cuts.
6 %>
7 %> @sa uip_clus_hca.m, pdist, linkage (MATLAB functions)
8 classdef clus_hca < clus
9  properties
10  %> Minimum number of clusters
11  nc_min = 2;
12  %> Maximum number of clusters
13  nc_max = 100;
14  %> ='euclidean'. Distance type. See help for @c pdist() for possible types.
15  distancetype = 'euclidean';
16  %> ='ward'. Linkage type. Default is set to the famous "Ward". See help for @c linkage() for possible types.
17  linkagetype = 'ward';
18  end;
19 
20  methods
21  function o = clus_hca(o)
22  o.classtitle = 'Hierarchical Cluster Analysis';
23  o.short = 'HCA';
24  end;
25  end;
26 
27  methods(Access=protected)
28  function dout = do_use(o, data)
29 
30 
31  % 1) Distance matrix
32  ipro = progress2_open('HCA - distance matrix ...', [], 0, 2);
33  irverbose(sprintf('Starting calculation of the distance matrix with %d observations...', data.no));
34  t = tic();
35 
36  b_obsidxs = data.classes >= 0;
37  no_new = sum(b_obsidxs);
38 
39  Y = pdist(single(data.X(b_obsidxs, :)), o.distancetype);
40  irverbose(sprintf('...finished (took %g seconds)', toc(t)));
41 
42 
43  % 2) Linkage
44  ipro = progress2_change(ipro, 'HCA - linkage ...', [], 1);
45  Z = linkage(Y, o.linkagetype);
46 
47 
48  % 2.5) Organizing into dataset
49  Xnew = ones(data.no, o.nc_max-o.nc_min+1)*-3; % default value is -3: "refuse-to-cluster"
50  for i = o.nc_min:o.nc_max
51  Xnew(b_obsidxs, i-o.nc_min+1) = cluster(Z, 'MaxClust', i)-1;
52  end;
53 
54  dout = irdata_clus();
55  dout = dout.import_from_struct(data);
56 
57  dout.X = double(Xnew);
58  dout.fea_x = o.nc_min:o.nc_max;
59  dout.title = [dout.title, ' - HCA'];
60  dout.xname = 'Number of clusters';
61  dout.xunit = '';
62  dout.yname = 'Cluster number';
63  dout.yunit = '';
64 
65  ipro = progress2_change(ipro, 'HCA - finished!', [], 2);
66  progress2_close(ipro);
67  end;
68  end;
69 end
function irverbose(in s, in level)
Clustering base class.
Definition: clus.m:2
Dataset class - cluster data.
Definition: irdata_clus.m:6
function progress2_change(in prgrss, in title, in perc, in i, in n)
function progress2_open(in title, in perc, in i, in n)
Hierarchical Clustering.
Definition: clus_hca.m:8
function progress2_close(in prgrss)