-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathftsvmplot.m
More file actions
160 lines (135 loc) · 5.21 KB
/
ftsvmplot.m
File metadata and controls
160 lines (135 loc) · 5.21 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
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
151
152
153
154
155
156
157
158
159
160
function ftsvmplot(ftsvm_struct,Traindata,Trainlabel)
% Function: visualizing
% Input:
% ftsvm_struct,Traindata,Trainlabel
% Output:
% Author: Bin-BinGaa (csgaobb@gmail.com)
% Created on 2014.10.10
% Last modified on 2017.04.14
if (nargin <1|| nargin > 3) % check correct number of arguments
help ftsvmplot
else
%%
minX = min(Traindata(:, 1));
maxX = max(Traindata(:, 1));
minY = min(Traindata(:, 2));
maxY = max(Traindata(:, 2));
gridX = (maxX - minX) ./ 200;
gridY = (maxY - minY) ./ 200;
minX = minX - 10 * gridX;
maxX = maxX + 10 * gridX;
minY = minY - 10 * gridY;
maxY = maxY + 10 * gridY;
[bigX, bigY] = meshgrid(minX:gridX:maxX, minY:gridY:maxY);
%%
ntest=size(bigX, 1) * size(bigX, 2);
test_Traindata=[reshape(bigX, ntest, 1), reshape(bigY, ntest, 1)];
test_label = zeros(size(test_Traindata,1), 1);
[~,outclass,~,f1,f2]= ftsvmclass(ftsvm_struct, test_Traindata);
Z = f1+f2;
bigC = reshape(outclass, size(bigX, 1), size(bigX, 2));
bigZ = reshape(Z, size(bigX, 1), size(bigX, 2));
bigf1 = reshape(f1, size(bigX, 1), size(bigX, 2));
bigf2 = reshape(f2, size(bigX, 1), size(bigX, 2));
alpha=ftsvm_struct.alpha;
beta =ftsvm_struct.beta;
[groupIndex, groupString] = grp2idx(Trainlabel);
groupIndex = 1 - (2* (groupIndex-1));
xp=Traindata(groupIndex==1,:);
xn=Traindata(groupIndex==-1,:);
ln=size(alpha,1);
lp=size(beta,1);
nsvIndex = alpha > (ftsvm_struct.Parameter.CC*1e-4);%& alpha< (ftsvm_struct.Parameter.CC*ftsvm_struct.sn-sqrt(eps));
psvIndex = beta > (ftsvm_struct.Parameter.CC*1e-4);%& beta< (ftsvm_struct.Parameter.CC*ftsvm_struct.sp-sqrt(eps));
psv = xp(psvIndex,:);
nsv = xn(nsvIndex,:);
if ~size(psv,1)
psvIndex = beta > eps;
psv = xp(psvIndex,:);
end
if ~size(nsv,1)
nsvIndex = alpha > eps;
nsv = xn(nsvIndex,:);
end
figure
sp=mapminmax(ftsvm_struct.sp',0.1,1)';
[xq,yq] = meshgrid(linspace(min(xp(:, 1)), max(xp(:, 1)), 100), linspace(min(xp(:, 2)), max(xp(:, 2)), 100));
vq = griddata(xp(:, 1), xp(:, 2),sp,xq,yq,'v4');
% vq(find(vq<0))=0;
contourf(xq,yq,vq,100,'LineStyle','none')
colormap(jet);
hold on
plot(xp(:, 1), xp(:, 2),'r+','LineWidth',2);
colorbar
hold off
line=['CDFTSVM with ' num2str(ftsvm_struct.Parameter.ker),' kernel' ];
title(line,'FontSize',12);
figure
sn=mapminmax(ftsvm_struct.sn',0.1,1)';
[xq,yq] = meshgrid(linspace(min(xn(:, 1)), max(xn(:, 1)), 100), linspace(min(xn(:, 2)), max(xn(:, 2)), 100));
vq = griddata(xn(:, 1), xn(:, 2),sn,xq,yq,'v4');
% vq(find(vq<0))=0;
contourf(xq,yq,vq,100,'LineStyle','none');
colormap(jet);
hold on
plot(xn(:, 1), xn(:, 2),'bx','LineWidth',1.5);
colorbar
hold off
line=['CDFTSVM with ' num2str(ftsvm_struct.Parameter.ker),' kernel' ];
title(line,'FontSize',12);
figure
h1 = plot(xp(:, 1), xp(:, 2), 'r+','LineWidth',1.5);
text(xp(:,1),xp(:,2),num2str(round(100*sp)/100),'color','b','FontSize',4)%��(x,y)��дstring
hold on
h2 = plot(xn(:, 1), xn(:, 2), 'bx','LineWidth',1.5);
text(xn(:,1),xn(:,2),num2str(round(100*sn)/100),'color','r','FontSize',4)%��(x,y)��дstring
if ~isempty(ftsvm_struct.NXpv)|~isempty(ftsvm_struct.NXnv)
if ~isempty(ftsvm_struct.NXpv)
h3 = plot(xp(ftsvm_struct.NXpv,1),xp(ftsvm_struct.NXpv,2),'gs','MarkerSize',7);
end
if ~isempty(ftsvm_struct.NXnv)
h3 = plot(xn(ftsvm_struct.NXnv,1),xn(ftsvm_struct.NXnv,2),'gs','MarkerSize',7);
end
if ~isempty(ftsvm_struct.NXpv)&&~isempty(ftsvm_struct.NXnv)
h3 = plot(xp(ftsvm_struct.NXpv,1),xp(ftsvm_struct.NXpv,2),'gs','MarkerSize',7);
h3 = plot(xn(ftsvm_struct.NXnv,1),xn(ftsvm_struct.NXnv,2),'gs','MarkerSize',7);
end
end
h4=legend([h1,h2,h3],'class +','class -','Outliers');
set(h4,'EdgeColor','w');
line=['CDFTSVM with ' num2str(ftsvm_struct.Parameter.ker),' kernel' ];
title(line,'FontSize',12);
%%
figure;
clf;
set(gca,'XLim',[minX maxX],'YLim',[minY maxY]);
hold on;
h1 = plot(xp(:, 1), xp(:, 2), 'r+','LineWidth',1.5);
h2 = plot(xn(:, 1), xn(:, 2), 'bx','LineWidth',1.5 );
h3 = plot(psv(:,1),psv(:,2),'ko','MarkerSize',7 );
h3 = plot(nsv(:,1),nsv(:,2),'ko','MarkerSize',7 );
if ~isempty(ftsvm_struct.NXpv)|~isempty(ftsvm_struct.NXnv)
if ~isempty(ftsvm_struct.NXpv)
h4 = plot(xp(ftsvm_struct.NXpv,1),xp(ftsvm_struct.NXpv,2),'gs','MarkerSize',7);
end
if ~isempty(ftsvm_struct.NXnv)
h4 = plot(xn(ftsvm_struct.NXnv,1),xn(ftsvm_struct.NXnv,2),'gs','MarkerSize',7);
end
if ~isempty(ftsvm_struct.NXpv)&&~isempty(ftsvm_struct.NXnv)
h4 = plot(xp(ftsvm_struct.NXpv,1),xp(ftsvm_struct.NXpv,2),'gs','MarkerSize',7);
h4 = plot(xn(ftsvm_struct.NXnv,1),xn(ftsvm_struct.NXnv,2),'gs','MarkerSize',7);
end
end
h4=legend([h1,h2,h3,h4],'class +','class -','Support Vectors','Outliers');
set(h4,'EdgeColor','w');
[C,h] = contour(bigX, bigY, bigZ,[0 0],'k','LineWidth',1.5);
text_handle=clabel(C,h,'Color','k');
[C,h] = contour(bigX, bigY, bigf1,[0 0],'b:','LineWidth',1.5);
text_handle=clabel(C,h,'Color','b');
[C,h] = contour(bigX, bigY, bigf2,[0 0],'r:','LineWidth',1.5);
text_handle=clabel(C,h,'Color','r');
xlabel('demension1','FontSize',12);
ylabel('demension2','FontSize',12);
line=['CDFTSVM with ' num2str(ftsvm_struct.Parameter.ker),' kernel' ];
title(line,'FontSize',12);
end