-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMILIS_Train.m
150 lines (90 loc) · 3.16 KB
/
MILIS_Train.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function [ IPs, W, IterIPs,IterW,lambda ] = MILIS_Train( TrainDataSF,TestDataSF,beta,lambda )
Data=TrainDataSF;
LandMineNbInstPerBag=5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All negative instances
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NInsts=[];
for i=1:Data.NbBags
if Data.Bags(i).Label==-1
NInsts=[NInsts;Data.Bags(i).Insts];
end
end
Insts=[];
for i=1:Data.NbBags
Insts=[Insts;Data.Bags(i).Insts];
end
PInsts=[];
for i=1:Data.NbBags
if Data.Bags(i).Label==1
PInsts=[PInsts;Data.Bags(i).Insts];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Select IPs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IPs=ones(Data.NbBags,1);
for i=1:Data.NbBags
if Data.Bags(i).Label==1
mn=1e6;
for j=1:Data.Bags(i).NbInst
Pr=kde(Data.Bags(i).Insts(j,:),NInsts,beta);
if Pr<mn
mn=Pr;
ip=j;
end
end
else
mx=0;
for j=1:Data.Bags(i).NbInst
Pr=kde(Data.Bags(i).Insts(j,:),NInsts,beta);
if Pr>mx
mx=Pr;
ip=j;
end
end
end
IPs(i)=ip;
end
Dists=zeros(Data.NbBags);
for i=1:Data.NbBags
Dists(:,i)=DataMinHaussDorff(Insts,Data.Bags(i).Insts(IPs(i,1),:),LandMineNbInstPerBag);
end
SortedDists=sort(Dists,2);
Best_Sig=mean(SortedDists(:,5))
%lambda=1/(Best_Sig^2);
S=exp(-lambda*(Dists.^2));
figure, scatter(PInsts(:,1),PInsts(:,2),'r')
hold on, scatter(NInsts(:,1),NInsts(:,2),'c')
for i=1: Data.NbBags
hold on; scatter (Data.Bags(i).Insts(IPs(i,1),1),Data.Bags(i).Insts(IPs(i,1),2),100,'k*');
end
NbIter=6;
IterCounter=0;
Labels = [Data.Bags(:).Label];
ChangedIPs=[];
IterIPs=[];
IterW=[];
while IterCounter<NbIter
OldIPs=IPs;
Mdl = fitcsvm(S,Labels);
W = Mdl.Beta;
IterW=[IterW W];
[S,IPs]=InstUpdate(Data,S,IPs,W,lambda,Insts);
figure, scatter(PInsts(:,1),PInsts(:,2),'r')
hold on, scatter(NInsts(:,1),NInsts(:,2),'c')
for i=1: Data.NbBags
hold on; scatter (Data.Bags(i).Insts(IPs(i,1),1),Data.Bags(i).Insts(IPs(i,1),2),100,'k*');
end
IterIPs=[IterIPs IPs];
IterCounter = IterCounter+1;
%find(IPs~=OldIPs)'
ChangedIPs=[ChangedIPs find(IPs~=OldIPs)'];
%TestCurrModel( TestDataSF,TrainDataSF,IPs,W,lambda,IterCounter);
[res1,lbls]=MILIS_Test(TestDataSF,TrainDataSF,IPs,W,lambda);
res1
if isequal(IPs,OldIPs)
break;
end
end
end