-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcdprojection_syntheticlinear.m
246 lines (212 loc) · 9.27 KB
/
pcdprojection_syntheticlinear.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (C) Javier Sánchez Monedero (jsanchezm at uco dot es)
%
% This code implements the Pairwise Class Distances (PCD) projection and the
% associated PCD Ordinal Classifier (PCDOC).
%
% The code has been tested with Ubuntu 11.04 x86_64 and Matlab R2009a
%
% If you use this code, please cite the associated paper
% Code updates and citing information:
% http://www.uco.es/grupos/ayrna/neco-pairwisedistances
%
% AYRNA Research group's website:
% http://www.uco.es/ayrna
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 3
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Licence available at: http://www.gnu.org/licenses/gpl-3.0.html
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clearvars;
addpath tools/
load SyntheticLinearOrder.mat;
% Choose what to paint
% Paint patterns
paintX = true;
% Paint min distances
paintlines = true;
% Ratio of wow many distances will be painted
paintlineRatio = 1;
% Paint projection histograms
painthist = true;
% Line width and colours definitions
MarkerSizeValue = 7;
LineWidthValue = 2;
verde = [0.2,0.7,0.2];
gris = [0.7,0.7,0.7];
% Relevant points to paint. Z values of the PCD projection will be painted
% for these points
PointClass1 = [2.0125 -0.0664;-0.1414 -0.6614;-0.4608 1.1212;-3.2640 -2.7955;-2.3187 -3.7712;-2.0330 -0.6328];
PointClass2 = [3.142 0.1803;2.8559 3.0370;1.7369 3.2760; 1.3585 1.6836];
PointClass3 = [6.2040 4.9904;3.3522 5.2332;3.7801 2.4945;6.101 4.947];
PointClass4 = [8.2231 6.1212;6.1069 5.6002;7.2196 6.9221];
if paintX
figure; hold on; axis square;
% If you want to zoom into the plot, descomment the following line
%axis([-30 5 -5 30])
plot(X(Y==1,1),X(Y==1,2),'r+','MarkerSize',MarkerSizeValue)
plot(X(Y==2,1),X(Y==2,2),'b.','MarkerSize',MarkerSizeValue*2.5)
plot(X(Y==3,1),X(Y==3,2),'^','MarkerSize',MarkerSizeValue,'Color',verde)
plot(X(Y==4,1),X(Y==4,2),'k*','MarkerSize',MarkerSizeValue)
legend_handle = legend('class 1','class 2','class 3','class 4');
legend(legend_handle ,'Location','NorthWest');
end
[Z,ZC,Dmin,DminIdx,NC] = pcdprojection(X,Y,Q);
if paintlines
% PAINTING
axis square;
for i = 1:Q-1
j = i + 1;
Ni=size(X(Y==i,:),1);
Nj=size(X(Y==j,:),1);
Xsub = [X(Y==i,:);X(Y==j,:)];
temp = DminIdx{i,j};
tempdist = Dmin{i,j};
for pattern=1:Ni
if rand <= paintlineRatio
% Hack for painting some patterns. min(abs(diff)) <
% 0.01 is necesary due to different number precissions
punto = [Xsub(pattern,1) Xsub(pattern,2)];
switch(i)
case 1
diff = PointClass1-repmat(punto,size(PointClass1,1),1);
if min(abs(diff)) < 0.01
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'r-','LineWidth',LineWidthValue);
end
case 2
diff = PointClass2-repmat(punto,size(PointClass2,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'b-','LineWidth',LineWidthValue);
end
case 3
diff = PointClass3-repmat(punto,size(PointClass3,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'-','Color',verde,'LineWidth',LineWidthValue);
end
end
end
end
for pattern=Ni+1:Ni+Nj
if rand <= paintlineRatio
punto = [Xsub(pattern,1) Xsub(pattern,2)];
switch(j)
case 2
diff = PointClass2-repmat(punto,size(PointClass2,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'b--','LineWidth',LineWidthValue);
end
case 3
diff = PointClass3-repmat(punto,size(PointClass3,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'--','Color',verde,'LineWidth',LineWidthValue);
end
case 4
diff = PointClass4-repmat(punto,size(PointClass4,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot([Xsub(pattern,1) Xsub(temp(pattern),1)],...
[Xsub(pattern,2) Xsub(temp(pattern),2)],'k--','LineWidth',LineWidthValue);
end
end
end
end
end
end
poffset = 0;
for i=1:Q
temp = ZC{i,1};
if paintX
for n=1:NC(i)
punto = [X(poffset+n,1) X(poffset+n,2)];
PointClass = [PointClass1;PointClass2;PointClass3;PointClass4];
diff = PointClass-repmat(punto,size(PointClass,1),1);
if min(abs(diff)) < 0.001
plotThisPoint = true;
else
plotThisPoint = false;
end
if plotThisPoint
plot(X(poffset+n,1),X(poffset+n,2),'o','Color',gris,'MarkerSize',MarkerSizeValue*1.5);
text(X(poffset+n,1),X(poffset+n,2),num2str(temp(n,1)));
end
end
end
poffset=poffset+NC(i);
end
hold off;
% width
width = 1/Q;
% maximum Z value
maxZ = 1;
% Thressholds used for classification purposes when predicting
% new values of Z for unseen data.
thresshold = width:width:maxZ;
if painthist
%On color histogram
%histPrecision=200;
%figure;hist(Z,histPrecision);hold on;
%plot(thresshold,zeros(size(thresshold)),'+r');
%legend_handle = legend('class 1','class 2','class 3','class 4');
%legend(legend_handle ,'Location','NorthEast');
%hold off;
histPrecision=200/Q;
figure;hold on;
hist(ZC{1,1},histPrecision);
hist(ZC{2,1},histPrecision);
hist(ZC{3,1},histPrecision);
hist(ZC{4,1},histPrecision);
h = findobj(gca,'Type','patch');
set(h(4),'FaceColor','r','EdgeColor','r');
set(h(3),'FaceColor','b','EdgeColor','b');
set(h(2),'FaceColor',verde,'EdgeColor',verde);
set(h(1),'FaceColor','k','EdgeColor','k');
legend_handle = legend('class 1','class 2','class 3','class 4');
legend(legend_handle ,'Location','NorthEast');
plot(thresshold,zeros(size(thresshold)),'+r'); % puntos de corte de decisión
hold off;
end