📄 lsda.m
字号:
function [eigvector, eigvalue] = LSDA(X, gnd, options)
% LSDA: Locality Sensitive Discriminant Analysis
%
% [eigvector, eigvalue] = LSDA(X, gnd, options)
%
% Input:
% X - Data matrix. Each row vector of fea is a data point.
%
% gnd - Label vector.
%
% options - Struct value in Matlab. The fields in options
% that can be set:
% k = 0
% Wb:
% Put an edge between two nodes if and
% only if they belong to different classes.
% Ww:
% Put an edge between two nodes if and
% only if they belong to same class.
% > 0
% Wb:
% Put an edge between two nodes if
% they belong to different classes
% and they are among the k nearst
% neighbors of each other.
% Ww:
% Put an edge between two nodes if
% they belong to same class and they
% are among the k nearst neighbors of
% each other.
% beta [0,1] Paramter to tune the weight between
% within-class graph and between-class
% graph. Default 0.1.
% beta*L_b+(1-beta)*W_w
%
% ReducedDim - The dimensionality of the reduced
% subspace. If 0, all the dimensions
% will be kept. Default is 30.
%
% Regu - 1: regularized solution,
% a* = argmax (a'X'WXa)/(a'X'DXa+alpha*I)
% 0: solve the sinularity problem by SVD
% Default: 1
%
% alpha - The regularization parameter. Valid
% when Regu==1. Default value is 0.1.
%
% ReguType - 'Ridge': Tikhonov regularization
% 'Custom': User provided
% regularization matrix
% Default: 'Ridge'
% regularizerR - (nFea x nFea) regularization
% matrix which should be provided
% if ReguType is 'Custom'. nFea is
% the feature number of data
% matrix
%
% PCARatio - The percentage of principal
% component kept in the PCA
% step. The percentage is
% calculated based on the
% eigenvalue. Default is 1
% (100%, all the non-zero
% eigenvalues will be kept.
% If PCARatio > 1, the PCA step
% will keep exactly PCARatio principle
% components (does not exceed the
% exact number of non-zero components).
%
% 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.
%
%
% Examples:
%
%
%
% fea = rand(50,70);
% gnd = [ones(10,1);ones(15,1)*2;ones(10,1)*3;ones(15,1)*4];
% options = [];
% options.k = 5;
% [eigvector, eigvalue] = LSDA(fea, gnd, options);
% Y = fea*eigvector;
%
%
%
% See also LPP, LGE
%
%Reference:
%
% Deng Cai, Xiaofei He, Kun Zhou, Jiawei Han and Hujun Bao, "Locality
% Sensitive Discriminant Analysis", IJCAI'2007
%
% Written by Deng Cai (dengcai2 AT cs.uiuc.edu), May/2006, May/2007
if (~exist('options','var'))
options = [];
end
[nSmp,nFea] = size(X);
if length(gnd) ~= nSmp
error('gnd and X mismatch!');
end
k = 0;
if isfield(options,'k') & (options.k < nSmp-1)
k = options.k;
end
beta = 0.1;
if isfield(options,'beta') & (options.beta > 0) & (options.beta < 1)
beta = options.beta;
end
Label = unique(gnd);
nLabel = length(Label);
Ww = zeros(nSmp,nSmp);
Wb = ones(nSmp,nSmp);
for idx=1:nLabel
classIdx = find(gnd==Label(idx));
Ww(classIdx,classIdx) = 1;
Wb(classIdx,classIdx) = 0;
end
if k > 0
D = EuDist2(X,[],0);
[dump idx] = sort(D,2); % sort each row
clear D dump
idx = idx(:,1:options.k+1);
G = sparse(repmat([1:nSmp]',[options.k+1,1]),idx(:),ones(prod(size(idx)),1),nSmp,nSmp);
G = max(G,G');
Ww = Ww.*G;
Wb = Wb.*G;
clear G
end
Wb = diag(sum(Wb,2)) - Wb;
D = full(sum(Ww,2));
Ww = sparse(beta*Wb+(1-beta)*Ww);
clear Wb
DToPowerHalf = D.^.5;
D_mhalf = DToPowerHalf.^-1;
tmpD_mhalf = repmat(D_mhalf,1,nSmp);
Ww = (tmpD_mhalf.*Ww).*tmpD_mhalf';
clear tmpD_mhalf;
Ww = max(Ww,Ww');
%==========================
% If X is too large, the following centering codes can be commented
%==========================
if issparse(X)
X = full(X);
end
sampleMean = mean(X);
X = (X - repmat(sampleMean,nSmp,1));
%==========================
X = repmat(DToPowerHalf,1,nFea).*X;
[eigvector, eigvalue] = LGE(X, Ww, [], options);
eigIdx = find(eigvalue < 1e-10);
eigvalue (eigIdx) = [];
eigvector(:,eigIdx) = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -