IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr_dist.m
Go to the documentation of this file.
1 %> @brief Least-Distance-to-Class-Mean Classifier
2 %>
3 %> Implements the Least-Distance-to-Class-Mean Classifier ("Euclidean Distance classifier" as in [1])
4 %>
5 %> With option do weigh the points using Plamen's potential function [2].
6 %>
7 %> @sa uip_clssr_dist.m
8 %>
9 %> <h3>References</h3>
10 %> [1] Raudys SJ, Jain AK. Sample Size Effects in Statistical Pattern Recognition: Recommendations for Practitioners.
11 %%> IEEE Transactions on Pattern Analysis and Machine Intelligence. 1991;13(3).
12 %>
13 %> [2] Angelov PP, Filev DP. An approach to online identification of Takagi-Sugeno fuzzy models. IEEE transactions on
14 %> systems, man, and cybernetics. Part B, Cybernetics : a publication of the IEEE Systems, Man, and Cybernetics Society.
15 %> 2004;34(1):484-98. Available at: http://www.ncbi.nlm.nih.gov/pubmed/15369087.
16 classdef clssr_dist < clssr
17  properties
18  normtype = 'euclidean'; % available options are: 'euclidean', (none else for the moment)
19  flag_pr = 0; % Whether or not to weight the points by the potential function when calculating the per-class mean
20  %> =1. Whether or not to use priors. If set, will calculate the prior class-conditional probabilities based on the amount of
21  %> training data for each class.
22  flag_use_priors = 1;
23  end;
24 
25  properties(SetAccess=protected)
26  classmeans;
27  classlabels_train;
28  pr_coeffs;
29 
30  % Probability of belonging to each class - calculated from training data
31  priors;
32  end;
33 
34 
35  methods
36  function o = clssr_dist(o)
37  o.classtitle = 'Least-Distance-to-Class-Mean';
38  o.short = 'Dist';
39  end;
40  end;
41 
42  methods(Access=protected)
43 
44  %> Calculates @c classmeans
45  function o = do_train(o, data)
46  o.classlabels = data.classlabels;
47 
48  t = tic; % Starts time counting
49 
50 % pieces = data_split_classes(data);
51  nc = max(data.classes)+1;
52  o.classmeans = zeros(data.nf, nc);
53 
54 
55  if ~o.flag_pr
56  for i = 1:nc
57  o.classmeans(:, i) = mean(data.X(data.classes == i-1, :), 1)';
58  end;
59  else
60  % Plamen's weighted class means where the weights are the per-class potentials
61 
62  A = 1; % Scaling factor for the potential formula like 1/(1+A*sum(...))
63  % This is just an internal experience
64 
65 
66  for i = 1:nc
67  Xi = data.X(data.classes == i-1, :);
68 
69  no = size(Xi, 1);
70 
71  % Recursive calculation of beta and xi for the potential formula
72  beta = 0;
73  xi = 0;
74  for j = 1:no
75  x = Xi(j, :);
76  beta = beta+sum(x.^2);
77  xi = xi+x;
78  end;
79 
80 
81  % Storing these but probably won't use them.
82  o.pr_coeffs(i).beta = beta;
83  o.pr_coeffs(i).xi = xi;
84 
85  % Again, but this time is to calculate the weighted mean
86  % mean = 1/sum(p_j)*sum(x_j*p_j)
87  sum_xp = 0;
88  sum_p = 0;
89  for j = 1:no
90  x = Xi(j, :);
91 
92  p = no/(no*(A*x*x'+1)+A*beta-2*A*x*xi');
93 
94  sum_xp = sum_xp+p*x;
95  sum_p = sum_p+p;
96  end;
97 
98  o.classmeans(:, i) = sum_xp/sum_p;
99  end;
100  end;
101 
102  % Calculates priors
103  if ~o.flag_use_priors
104  o.priors = ones(1, data.nc);
105  else
106  for k = data.nc:-1:1 % Backwards for allocation
107  o.priors(k) = sum(data.classes == k-1);
108  end;
109  end;
110  o.priors = o.priors/sum(o.priors);
111 
112 
113  o.classlabels_train = data.classlabels;
114 
115  o.time_train = toc(t);
116  end;
117 
118 
119 
120  %> Calculates @c est.X using the "softmax" transform, which is
121  %> X(i, j) = (1/dist_of_point_i_to_class_j^2)/sum(all numerators in j)
122  %>
123  function est = do_use(o, data)
124 
125  est = estimato();
126  est.classlabels = o.classlabels;
127  est = est.copy_from_data(data);
128 
129 
130  tic();
131 
132  nc = size(o.classmeans, 2);
133  X = data.X;
134 
135  Y = zeros(data.no, nc);
136  for i = 1:nc
137  X_subtract = repmat(o.classmeans(:, i)', data.no, 1);
138  X_ = X-X_subtract;
139  Y(:, i) = 1./exp(sum(X_.^2, 2)); % distance is Euclidean
140  end;
141  Y(Y == Inf) = realmax();
142 
143  % Class posterior probabilities are weighted by their respective priors
144  if o.flag_use_priors
145  Y = Y.*repmat(o.priors, data.no, 1);
146  end;
147 
148  Y = Y./repmat(sum(Y, 2), 1, nc); % normalization
149 
150  o.time_use = toc();
151 
152  est.X = Y;
153 % est.classes = renumber_classes(est.classes, o.classlabels_train, o.classlabels);
154  end;
155  end;
156 end
Dataset representing estimation.
Definition: estimato.m:4
function renumber_classes(in classes_orig, in classlabels_orig, in classlabels_ref)
Analysis Session (AS) base class.
Definition: as.m:6