├── Indiana200.mat ├── LFDA.m ├── OMP.m ├── cdKNN.m ├── cdOMP.m ├── cdSRC.m ├── gradient_descent.m ├── lda.m ├── normalize_data.m ├── readme.txt ├── select_train_data.m └── select_train_data1.m /Indiana200.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/Indiana200.mat -------------------------------------------------------------------------------- /LFDA.m: -------------------------------------------------------------------------------- 1 | function [T,Z]=LFDA(X,Y,r,metric,kNN) 2 | % 3 | % Local Fisher Discriminant Analysis for Supervised Dimensionality Reduction 4 | % 5 | % Usage: 6 | % [T,Z]=LFDA(X,Y,r,metric) 7 | % 8 | % Input: 9 | % X: d x n matrix of original samples 10 | % d --- dimensionality of original samples 11 | % n --- the number of samples 12 | % Y: n dimensional vector of class labels 13 | % (each element takes an integer between 1 and c, where c is the number of classes) 14 | % r: dimensionality of reduced space (default: d) 15 | % metric: type of metric in the embedding space (default: 'weighted') 16 | % 'weighted' --- weighted eigenvectors 17 | % 'orthonormalized' --- orthonormalized 18 | % 'plain' --- raw eigenvectors 19 | % kNN: parameter used in local scaling method (default: 7) 20 | % 21 | % Output: 22 | % T: d x r transformation matrix (Z=T'*X) 23 | % Z: r x n matrix of dimensionality reduced samples 24 | % 25 | % (c) Masashi Sugiyama, Department of Compter Science, Tokyo Institute of Technology, Japan. 26 | % sugi@cs.titech.ac.jp, http://sugiyama-www.cs.titech.ac.jp/~sugi/software/LFDA/ 27 | 28 | if nargin<2 29 | error('Not enough input arguments.') 30 | end 31 | [d n]=size(X); 32 | 33 | if nargin<3 34 | r=d; 35 | end 36 | 37 | if nargin<4 38 | metric='weighted'; 39 | end 40 | 41 | if nargin<5 42 | kNN=7; 43 | end 44 | 45 | tSb=zeros(d,d); 46 | tSw=zeros(d,d); 47 | 48 | for c=1:max(Y) 49 | Xc=X(:,Y==c); 50 | nc=size(Xc,2); 51 | 52 | % Define classwise affinity matrix 53 | Xc2=sum(Xc.^2,1); 54 | distance2=repmat(Xc2,nc,1)+repmat(Xc2',1,nc)-2*Xc'*Xc; 55 | [sorted,index]=sort(distance2); 56 | kNNdist2=sorted(kNN+1,:); 57 | sigma=sqrt(kNNdist2); 58 | localscale=sigma'*sigma; 59 | flag=(localscale~=0); 60 | A=zeros(nc,nc); 61 | A(flag)=exp(-distance2(flag)./localscale(flag)); 62 | 63 | Xc1=sum(Xc,2); 64 | G=Xc*(repmat(sum(A,2),[1 d]).*Xc')-Xc*A*Xc'; 65 | tSb=tSb+G/n+Xc*Xc'*(1-nc/n)+Xc1*Xc1'/n; 66 | tSw=tSw+G/nc; 67 | end 68 | 69 | X1=sum(X,2); 70 | tSb=tSb-X1*X1'/n-tSw; 71 | 72 | tSb=(tSb+tSb')/2; 73 | tSw=(tSw+tSw')/2; 74 | 75 | if r==d 76 | [eigvec,eigval_matrix]=eig(tSb,tSw); 77 | else 78 | opts.disp = 0; 79 | [eigvec,eigval_matrix]=eigs(tSb,tSw,r,'la',opts); 80 | end 81 | eigval=diag(eigval_matrix); 82 | [sort_eigval,sort_eigval_index]=sort(eigval); 83 | T0=eigvec(:,sort_eigval_index(end:-1:1)); 84 | 85 | switch metric %determine the metric in the embedding space 86 | case 'weighted' 87 | T=T0.*repmat(sqrt(sort_eigval(end:-1:1))',[d,1]); 88 | case 'orthonormalized' 89 | [T,dummy]=qr(T0,0); 90 | case 'plain' 91 | T=T0; 92 | end 93 | 94 | Z=(T')*X; 95 | 96 | end 97 | 98 | -------------------------------------------------------------------------------- /OMP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/OMP.m -------------------------------------------------------------------------------- /cdKNN.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/cdKNN.m -------------------------------------------------------------------------------- /cdOMP.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/cdOMP.m -------------------------------------------------------------------------------- /cdSRC.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/cdSRC.m -------------------------------------------------------------------------------- /gradient_descent.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/gradient_descent.m -------------------------------------------------------------------------------- /lda.m: -------------------------------------------------------------------------------- 1 | function [alpha,rsamples] = lda(samples,labels,rr) 2 | % Linear discriminant analysis 3 | % Usage: 4 | % [alpha,rsamples] = lda(samples,labels,rr); 5 | % Input: 6 | % samples: n x d matrix, each row is a sample. 7 | % labels: n x 1 column vector, labels of samples. 8 | % rr: number of discriminant vector. 9 | % Output: 10 | % alpha: n x r matrix, each column denotes a discriminant vector. 11 | % rsamples: n x r matrix, new samples after projection. 12 | % 13 | % Author: Liefeng bo 14 | % School of electronic engineering, Xidian University 15 | % April, 2006 16 | 17 | [n,d] = size(samples); 18 | labelsnum = unique(labels); 19 | c = length(labelsnum); 20 | dim = min(c-1,d); 21 | % dim=d; 22 | 23 | tmean = mean(samples); 24 | bsamples = samples - repmat(tmean,n,1); 25 | tscatters = 0; 26 | for i = 1:n 27 | tscatters = tscatters + (bsamples(i,:))'*bsamples(i,:); 28 | end 29 | tscatters = (tscatters + tscatters')/2; 30 | 31 | r = rank(tscatters); 32 | 33 | % rank is not smaller than dimensionality of samples 34 | if n >= d & r == d; 35 | bscatters = zeros(d,d); 36 | for i = 1:c 37 | cindex{i} = find(labels == labelsnum(i)); 38 | tmp = mean(samples(cindex{i},:)) - tmean; 39 | bscatters = bscatters + length(cindex{i})*tmp'*tmp; 40 | end 41 | bscatters = (bscatters + bscatters')/2; 42 | 43 | options.disp = 0; 44 | [alpha,variance] = eigs(bscatters,tscatters,dim,'lm',options); 45 | % normalize the coeficients 46 | for i = 1:dim 47 | alpha(:,i) = alpha(:,i)/norm(alpha(:,i)); 48 | end 49 | rsamples = samples*alpha; 50 | 51 | % rank is smaller than dimensionality of samples 52 | else 53 | bscatters = zeros(d,d); 54 | for i = 1:c 55 | cindex{i} = find(labels == labelsnum(i)); 56 | tmp = mean(samples(cindex{i},:)) - tmean; 57 | bscatters = bscatters + length(cindex{i})*tmp'*tmp; 58 | end 59 | tscatters = tscatters + 0*eye(d); 60 | [U,variance,pc] = svd(tscatters); 61 | if nargin == 3 62 | r = rr; 63 | end 64 | P = pc(:,1:r); 65 | options.disp = 0; 66 | [dc,variance] = eigs(P'*bscatters*P,variance(1:r,1:r),dim,'lm',options); 67 | alpha = P*dc; 68 | 69 | % normalize the coeficients 70 | for i = 1:dim 71 | alpha(:,i) = alpha(:,i)/norm(alpha(:,i)); 72 | end 73 | rsamples = samples*alpha; 74 | end 75 | -------------------------------------------------------------------------------- /normalize_data.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/normalize_data.m -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/readme.txt -------------------------------------------------------------------------------- /select_train_data.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jk123vip/cdSRC_matlab_code/b37c889fc9f0421a4a0db11350bd5175e35d3180/select_train_data.m -------------------------------------------------------------------------------- /select_train_data1.m: -------------------------------------------------------------------------------- 1 | function [train_index, test_index] = select_train_data1(labels, N) 2 | % labels: data labels 3 | % N: the number of training examples each class 4 | 5 | c = max(labels); 6 | train_index = []; 7 | test_index = []; 8 | 9 | for i = 1:c 10 | ind = find(labels == i); 11 | if N < length(ind) 12 | L = length(ind); 13 | p = randperm(L); 14 | train_index = [train_index, ind(p(1:N))]; 15 | test_index = [test_index, ind(p(N+1:end))]; 16 | else 17 | train_index = [train_index, ind]; 18 | test_index = [test_index, ind]; 19 | end 20 | end 21 | 22 | end --------------------------------------------------------------------------------