📄 isop.m
字号:
function [eigvector, eigvalue, elapse] = IsoP(options, data)
% IsoP: Isometric Projection
%
% [eigvector, eigvalue, elapse] = 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
%
INFratio = 1000;
nBlock = 4000;
bGlobal = 0;
if ~exist('data','var')
bGlobal = 1;
global data;
end
if (~exist('options','var'))
options = [];
end
if ~isfield(options,'NeighborMode')
options.NeighborMode = 'KNN';
end
if ~isfield(options,'k')
options.k = 5;
end
[nSmp,nFea] = size(data);
if options.k >= nSmp
error('k is too large!');
end
tmp_T = cputime;
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 length(infIdx) > 0
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
H = sumS'*ones(1,nSmp)/nSmp;
TauDg = -.5*(S - H - H' + sum(sumS)/(nSmp^2));
TauDg = max(TauDg,TauDg');
timeW = cputime - tmp_T;
%==========================
% 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);
end
%==========================
if bGlobal & isfield(options,'keepMean') & options.keepMean
[eigvector, eigvalue, elapse] = LGE(TauDg, [], options);
else
[eigvector, eigvalue, elapse] = LGE(TauDg, [], options, data);
end
elapse.timeW = timeW;
elapse.timeAll = elapse.timeAll + elapse.timeW;
eigIdx = find(eigvalue < 1e-3);
eigvalue (eigIdx) = [];
eigvector(:,eigIdx) = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -