-
Notifications
You must be signed in to change notification settings - Fork 0
/
preGrouping.m
83 lines (67 loc) · 2.24 KB
/
preGrouping.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
% This function is used to pick out samples where each category label
% kurtosis accounts for the Ratio=30%.
function [new_features,new_labels,new_logic_labels,new_kappa,cataWeights] = preGrouping(features,labels,Ratio,LowerBound,method,isWeighted)
% Read Parameters
if (~exist('method','var'))
method = 'descend';
end
if (~exist('isWeighted','var'))
isWeighted = true;
end
if (~exist('Ratio','var'))
Ratio = 0.5;
end
if (~exist('LowerBound','var'))
LowerBound = 1;
end
[rowLen,colLen] = size(labels);
groups = cell(colLen,1);
new_groups = cell(colLen,1);
kurtosis_class = cell(colLen,1);
% calculate kurtosis of labels
kappa = kurtosis(labels,1,2);
% get the index of first degree of every sample
[~,classId] = max(labels,[],2);
% 'group' stores the sample rowID that are classified into different categories
for i = 1:colLen
groups{i}=[];
end
for row = 1:rowLen
groups{classId(row)}= [groups{classId(row)};row];
end
% get the selected samples
new_features=[];
new_labels = [];
new_kappa =[];
for i = 1:colLen
rowId = groups{i};
kurtosis_class{i} = kappa(rowId);
[~,I] = sort(kurtosis_class{i},method);
new_groups{i} = [];
for j = 1:length(I) * Ratio
if kurtosis_class{i}(I(j)) >= LowerBound
new_groups{i}=[new_groups{i};rowId(I(j))];
end
end
new_features = [new_features; features(new_groups{i},:)]; %#ok<*AGROW>
new_labels = [new_labels; labels(new_groups{i},:)];
new_kappa = [new_kappa; kappa(new_groups{i},:)];
end
if isWeighted
weight = ones(colLen,1);
cataWeights = [];
for i = 1:colLen
weight(i) =1.0/ (length(new_groups{i})/length(groups{i}));
for j = 1:length(new_groups{i})
cataWeights =[cataWeights; weight(i)];
end
end
else
cataWeights = ones(size(new_labels,1),1);
end
[~,classId] = max(new_labels,[],2);
new_logic_labels = zeros(size(new_labels));
for i = 1:length(classId)
new_logic_labels(i,classId(i))=1;
end
end