Repository navigation
Expand file tree
/
Copy pathncm_train.m
More file actions
99 lines (75 loc) · 2.47 KB
/
Copy pathncm_train.m
File metadata and controls
99 lines (75 loc) · 2.47 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%%% Author: Ruobai Feng, Shuan Guo
function[QLx_, QRx_, par_, entropyQL_,entropyQR_,PQL_,PQR_, split_found] = ...
ncm_train(data, QX, entropy, clmax)
%% NCM Train
% Combining NCM and Random Forest
% INPUT : data: whole data QX: sampled data
% entropy: entropy of parent tree
% clmax : number of total classes
% OUTPUT : par_ : parameters of the best NCM classifier for each node
% (include assigned class & assigned label (1 or 0))
% QLx_, QRx_ : index of data for left and right child nodes
% entropyQL_, entropyQR_ : entropy of left and right child nodes
% PQL_, PQR_ : distributions of posterior probabilities for left and right
% child nodes
th = 0.001; % threshold
BestGain = th;
%mahalDistance = 1; % 1-yes 0-no
QLx_ = 0; % left node
QRx_ = 0; % right node
par_ = 0; % parameter(c_means)
entropyQL_ = 0; % left node - entropy
entropyQR_ = 0; % right node - entropy
PQL_ = 0; % left node - distribution
PQR_ = 0; % right node - distribution
% Initialization
X = data(QX,1:(size(data,2) - 1));
class_label = data(QX,size(data,2));
magnitude = length(QX);
% Calculation of centroids : c_means
c_mean = zeros(clmax,size(X,2));
for j = 1:clmax
c_data = X(class_label == j,:);
c_mean(j,:) = sum(c_data,1)/size(c_data,1);
end
% predict classes for each datapoint
Pred_class = zeros(magnitude,1);
distance = zeros(clmax,1);
for m =1: magnitude
for n = 1:clmax
distance(n) = mean(dist2(X(m,:), c_mean(n,:)));
end
[minvalue, minidx] = min(distance);
Pred_class(m) = minidx;
end
for i=1:20
Pred_label = zeros(magnitude,1);
assign_label = ones(clmax,1);
rand_idx = randperm(clmax, round(clmax * 0.5));
assign_label(rand_idx) = 0;
for m =1: magnitude
Pred_label(m) = assign_label(Pred_class(m));
end
new_Par = [c_mean assign_label];
QLx = QX(:,(Pred_label == 1));
QRx = QX(:,(Pred_label == 0));
[Gain,PQL,PQR,entropyQL,entropyQR] = gain_entropy(entropy,QLx,QRx,data,clmax);
% Determine if it is the best gain
if(BestGain < Gain)
QLx_ = QLx;
QRx_ = QRx;
par_ = new_Par;
entropyQL_ = entropyQL;
entropyQR_ = entropyQR;
PQL_ = PQL;
PQR_ = PQR;
BestGain = Gain;
end
end
% Determine if split is found
if (BestGain == th)
split_found = 0;
else
split_found = 1;
end
end