-
Notifications
You must be signed in to change notification settings - Fork 10
/
cvAutolabel.m
67 lines (66 loc) · 2.37 KB
/
cvAutolabel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function ClusterClass = cvAutolabel(ProtoCluster, ProtoClass)
% cvAutolabel - Label classes for clusters
%
% Synopsis
% [ClusterClass] = cvAutolabel(ProtoCluster, ProtoClass);
%
% Description
% cvAutolabel labels classes for clusters using majority voting.
% ClusterClass = argmax (# of classes) in the cluster.
%
% Inputs ([]s are optional)
% (vector) ProtoCluster
% 1 x N vector representing integer cluster labels of
% prototypes (samles). ProtoCluster(n) is the cluster
% label of nth sample. Let the number of clusters be K.
% (vector) ProtoClass
% 1 x N vector representing integer cluster labels of
% prototypes (samles). ProtoCluster(n) is the class
% label of nth sample. Let the number of classes be C.
%
% Outputs ([]s are optional)
% (vector) ClusterClass
% 1 x K vector representing integer class labels for
% clusters. ClusterClass(k) is the class label
% for the kth cluster.
%
% Examples
% ProtoCluster = [1 1 1 2 2 2 3 3 3];
% ProtoClass = [1 1 1 1 2 2 2 2 2];
% ClusterClass = cvAutolabel(ProtoCluster, ProtoClass)
% % [1 2 2];
%
% See also
% cvKmeans
% Authors
% Naotoshi Seo <sonots(at)sonots.com>
%
% License
% The program is free to use for non-commercial academic purposes,
% but for course works, you must understand what is going inside to use.
% The program can be used, modified, or re-distributed for any purposes
% if you or one of your group understand codes (the one must come to
% court if court cases occur.) Please contact the authors if you are
% interested in using the program without meeting the above conditions.
%
% Changes
% 04/01/2006 First Edition
if length(ProtoCluster) ~= length(ProtoClass)
error('The # of samples for both ProtoCluster and ProtoClass was not met.');
end
ClusterLabel = unique(ProtoCluster);
K = length(ClusterLabel);
ClassLabel = unique(ProtoClass);
C = length(ClassLabel);
% majority voting
ClusterClass = zeros(1, K);
for k = 1:K
Idx = find(ProtoCluster == ClusterLabel(k));
InterClass = ProtoClass(Idx);
ClassCounter = zeros(1, C);
for c = 1:C
ClassCounter(c) = sum(InterClass == ClassLabel(c));
end
[maxi major] = max(ClassCounter);
ClusterClass(k) = ClassLabel(major);
end