-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsoP.m
237 lines (202 loc) · 6.99 KB
/
IsoP.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
function [eigvector, eigvalue] = IsoP(options, data)
% IsoP: Isometric Projection
%
% [eigvector, eigvalue] = IsoP(options, data)
%
% Input:
% data - Data matrix. Each row vector of data is a data point.
%
% options - Struct value in Matlab. The fields in options
% that can be set:
%
% NeighborMode - Indicates how to construct the graph. Choices
% are:
% 'KNN' - Put an edge between two nodes if and
% only if they are among the k nearst
% neighbors of each other. Default
% option.
% 'Supervised' - Two variations:
% 1. k=0, Put an edge between two nodes
% if and only if they belong to
% same class.
% 2. k>0, The distance between two nodes
% in the same class will be smaller than
% two nodes have diff. labels
% The label information 'gnd' should be
% provided.
%
% k - The number of neighbors.
% Default k = 5;
% gnd - The parameter needed under 'Supervised'
% NeighborMode. Colunm vector of the label
% information for each data point.
%
% Please see LGE.m for other options.
%
%
% Output:
% eigvector - Each column is an embedding function, for a new
% data point (row vector) x, y = x*eigvector
% will be the embedding result of x.
% eigvalue - The eigvalue of LPP eigen-problem. sorted from
% smallest to largest.
% elapse - Time spent on different steps
%
%
% Examples:
%
%
%
% fea = rand(50,70);
% gnd = [ones(10,1);ones(15,1)*2;ones(10,1)*3;ones(15,1)*4];
% options = [];
% options.k = 0;
% options.NeighborMode = 'Supervised';
% options.gnd = gnd;
% [eigvector, eigvalue] = IsoP(options, fea);
% Y = fea*eigvector;
%
%
%
% See also LPP, LGE
%
%Reference:
%
% Deng Cai, Xiaofei He, and Jiawei Han, "Isometric Projection",
% Twenty-Second Conference on Artificial Intelligence (AAAI-07), 2007
%
% Deng Cai, Xiaofei He and Jiawei Han, "Isometric Projection", Technical
% report, Computer Science Department, UIUC, UIUCDCS-R-2006-2747, July 2006
%
% Joshua B. Tenenbaum, Vin de Silva, and John C. Langford. "A Global
% Geometric Framework for Nonlinear Dimensionality Reduction", Science,
% v.290 no.5500 , Dec.22, 2000. pp.2319-2323.
%
%
% version 2.1 --June/2007
% version 2.0 --May/2007
% version 1.1 --May/2006
% version 1.0 --Nov/2005
%
% Written by Deng Cai (dengcai2 AT cs.uiuc.edu)
INFratio = 1000;
if (~exist('options','var'))
options = [];
end
if ~isfield(options,'NeighborMode')
options.NeighborMode = 'KNN';
end
if ~isfield(options,'k')
options.k = 5;
end
nSmp = size(data,1);
if options.k >= nSmp
error('k is too large!');
end
if options.k <= 0 % Always supervised!
if ~isfield(options,'gnd')
error('gnd should be provided!');
end
if length(options.gnd) ~= nSmp
error('gnd and data mismatch!');
end
Label = unique(options.gnd);
nLabel = length(Label);
G = zeros(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D = EuDist2(data(classIdx,:),[],1);
G(classIdx,classIdx) = D;
end
maxD = max(max(G));
INF = maxD*INFratio; % effectively infinite distance
D = INF*ones(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D(classIdx,classIdx) = G(classIdx,classIdx);
end
clear G
else
switch lower(options.NeighborMode)
case {lower('KNN')}
D = EuDist2(data);
maxD = max(max(D));
INF = maxD*INFratio; % effectively infinite distance
[dump,iidx] = sort(D,2);
iidx = iidx(:,(2+options.k):end);
for i=1:nSmp
D(i,iidx(i,:)) = 0;
end
D = max(D,D');
D = sparse(D);
D = dijkstra(D, 1:nSmp);
D = reshape(D,nSmp*nSmp,1);
infIdx = find(D==inf);
if ~isempty(infIdx)
D(infIdx) = INF;
end
D = reshape(D,nSmp,nSmp);
case {lower('Supervised')}
if ~isfield(options,'gnd')
error('gnd should be provided!');
end
if length(options.gnd) ~= nSmp
error('gnd and data mismatch!');
end
Label = unique(options.gnd);
nLabel = length(Label);
G = zeros(nSmp,nSmp);
maxD = 0;
for idx=1:nLabel
classIdx = find(options.gnd==Label(idx));
nSmpClass = length(classIdx);
D = EuDist2(data(classIdx,:),[],1);
if maxD < max(max(D))
maxD = max(max(D));
end
if options.k >= nSmpClass
G(classIdx,classIdx) = D;
else
[dump,iidx] = sort(D,2);
iidx = iidx(:,(2+options.k):end);
for i=1:nSmpClass
D(i,iidx(i,:)) = 0;
end
D = max(D,D');
D = sparse(D);
D = dijkstra(D, 1:nSmpClass);
G(classIdx,classIdx) = D;
end
end
INF = maxD*INFratio; % effectively infinite distance
D = INF*ones(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D(classIdx,classIdx) = G(classIdx,classIdx);
end
clear G
otherwise
error('NeighborMode does not exist!');
end
end
S = D.^2;
sumS = sum(S);
H = sumS'*ones(1,nSmp)/nSmp;
TauDg = -.5*(S - H - H' + sum(sumS)/(nSmp^2));
TauDg = max(TauDg,TauDg');
%==========================
% If data is too large, the following centering codes can be commented
%==========================
if isfield(options,'keepMean') && options.keepMean
else
if issparse(data)
data = full(data);
end
sampleMean = mean(data);
data = (data - repmat(sampleMean,nSmp,1));
end
%==========================
[eigvector, eigvalue] = LGE(TauDg, [], options, data);
eigIdx = find(eigvalue < 1e-3);
eigvalue (eigIdx) = [];
eigvector(:,eigIdx) = [];