资源简介
KPCA+SVM源代码,用matlab仿真实现,很实用
代码片段和文件信息
function rate = KPCA_SVM
%KPCA_SVM Kernel Principle Component Analysis & Support Vector Machine
%
% Using the ORL database to perform a face recognition method
% which empolys KPCA as features extractor and LSVM as classifier
%
% Outputs
% Pattern : The Pattern of the test data
% rate : The recognition rate
% Inputs
% Train : N x d matrix with the inputs of the training data
% N is the number of training points d is the dimension of data
% Test : N x d matrix with the inputs of the test data
% N is the number of test points d is the dimension
% d : polynomial degree
% max_ev : Number of eigenvalues
%
% parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load S_train
Tr = S_train;
Tr_n = size(Tr 1);
d = 1;
max_ev = 120;
% test set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load S_test
Te = S_test;
Te_n = size(Te1);
tic % set clock for training
% carry out Kernel PCA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute the kernel matrix of training set
K_train = feval(‘poly_kernel‘Tr Tr d);
l = ones(Tr_nTr_n)/Tr_n;
K_train = K_train - l*K_train - K_train*l + l*K_train*l; % centering in feature space
% compute eigenvectors and eigenvalues
[evecsevals] = eig(K_train/Tr_n);
[evalsind] = sort(diag(evals)); % diagonalize and sort eigenvalues
Lambda = evals(end:-1:1);
Ind = ind(end:-1:1);
% normalize eigenvector expansion coefficients
for i = 1:max_ev
Alpha(: i) = evecs(: Ind(i))/sqrt(Lambda(i)/Tr_n);
end
Train_Features = K_train * Alpha(: 1:max_ev); % compute features of training set
% compute kernel matrix of test set
K_test = feval(‘poly_kernel‘ Te Tr d);
ll = ones(Te_n Tr_n)/Tr_n;
K_test = K_test - ll*K_train - K_test*l + ll*K_train*l; % centering in feature space
Test_Features = K_test * Alpha(: 1:max_ev); % extract features
% carry out LSVM & NN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute Lagrange multipliers and bias
for i = 1:40
% construct training targets
Y(: i) = ones(Tr_n 1)*(-1);
Y(5*i-4:5*i i) = Y(5*i-4:5*i i)*(-1);
% compute coefficients through solving quadratic optimizaton with C=1e-8
[alpha(:i) B(:i)] = lsvctrain(Train_Features Y(: i) 1e-8);
end
toc % stop watch for training
tic % set watch for classification
% construct the linear test matrix
d = 2;
K_test_svm = feval(‘poly_kernel‘Test_Features Train_Features d);
% mulitply the linear matrix
t = K_test_svm*alpha .* Y + repmat(B Te_n 1);
% classify with decision function
C = sign(t); %+ repmat(B size(t1) 1));
% recognize patterns by the classifiers
Pattern = zeros(Te_n 1);
for i = 1:Te_n
Re =find(C(i :) > 0 ); % find the classsifiers with output ‘1‘
len(i) = length(Re);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4130 2008-03-11 16:59 KPCA_SVM_Train\KPCA_SVM_Train.m
目录 0 2008-09-18 09:53 KPCA_SVM_Train
----------- --------- ---------- ----- ----
4130 2
- 上一篇:simuli
nk三相整流仿真 - 下一篇:RSSI定位算法MATLAB代码
评论
共有 条评论