clear all;
close all;
class_num = 40;
sample_num = 10;
row = 112;
line = 92;
row_line = min(row, line);
rowline = row * line;
load orl_face2;
disp('orl_eigenface now begin');
right_num = [];
rr_0 = [];
for count = 8
train_num = count;
train_num
train_total = train_num * class_num;
test_num = sample_num - train_num;
test_num
test_total = test_num * class_num;
%% -------- 1.calculate Sb Sw -------- %%
orl_train = zeros(rowline, train_total);
orl_m_all = zeros(rowline,1);
orl_m = zeros(rowline,class_num);
for i = 1:class_num
for j = 1:train_num
kk = (i-1)*train_num + j;
orl_temp = double(orl(:,:,j,i));
for p = 1:row
for q = 1:line
site = (p-1)*line + q;
orl_train(site, kk) = orl_temp(p,q);
end
end
orl_m_all = orl_m_all + orl_train(:,kk);
orl_m(:,i) = orl_m(:,i) + orl_train(:,kk);
end
orl_m(:,i) = orl_m(:,i)/train_num;
end
orl_m_all = orl_m_all / train_total;
orl_test = zeros(rowline, test_total);
for i = 1:class_num
for j = 1:test_num
kk = (i-1)*test_num + j;
orl_temp = double(orl(:,:,j+train_num,i));
for p = 1:row
for q = 1:line
site = (p-1)*line + q;
orl_test(site, kk) = orl_temp(p,q);
end
end
end
end
Sb = zeros(rowline,class_num);
for i = 1:class_num
Sb(:,i) = orl_m(:,i) - orl_m_all;
end
% Sb0 = (Sb * Sb') / class_num; %% [rowline, rowline]
St = zeros(rowline,train_total);
for i = 1:class_num
for j = 1:train_num
kk = (i-1)*train_num + j;
St(:,kk) = orl_train(:,kk) - orl_m_all;
end
end
% St0 = (St * St') / train_total; %% [rowline, rowline]
%% -------- 2.calculate transform of St -------- %%
ss = (St' * St) / train_total; %% [train_total, train_total]
rr = rank(ss)-class_num;
rr_0 = [rr_0; rr];
[V_St, D_St] = eig(ss);
dd = abs(eig(ss));
[dd_value, dd_site] = sort(dd);
temp2 = rank(ss)-class_num;
temp1 = train_total - temp2 + 1;
yy = [];
Dt = zeros(rr);
jj = 0;
dd_demo = zeros(temp2,1);
dd_count = 1;
for i = train_total:-1:temp1
yy = [yy; V_St(:,dd_site(i))'];
jj = jj + 1;
Dt(jj,jj) = dd_value(i)^(-0.5);
dd_demo(dd_count) = dd_value(i);
dd_count = dd_count + 1;
end
yy = yy';
zz = St * yy * Dt;
ww = zz';
figure, plot(dd_demo);
for ii = 1:20
ww_temp = ww(ii,:);
ww_temp = ww_temp - min(ww_temp);
ww_temp = ww_temp / max(ww_temp);
ww_temp = floor(ww_temp * 255);
orl_temp = zeros(row, line);
for p = 1:row
for q = 1:line
site = (p-1)*line + q;
orl_temp(p,q) = ww_temp(site);
end
end
orl_temp = uint8(orl_temp);
ss = sprintf('eigenorl_%d.bmp',ii);
imwrite(orl_temp, ss,'bmp');
end
disp('orl_eigenface is over');
%% -------- 4.calculate transform of test and train -------- %%
orl_train_new = ww * orl_train;
orl_test_new = ww * orl_test;
error = [];
for i = 1:class_num
for j = 1:test_num
%i,j
kk = (i-1)*test_num + j;
res_y = orl_test_new(:,kk);
dis = [];
for ii = 1:class_num
for jj = 1:train_num
res_x = orl_train_new(:,(ii-1)*train_num+jj);
res = (res_y - res_x).^2;
temp = sqrt(sum(res));
%res = abs(res_y - res_x);
% temp = sum(res);
dis = [dis; temp];
end
end
[value, site] = min(dis);
site_new = floor((site-1)/train_num) + 1;
if site_new ~= i
error = [error; i*test_num+j];
end
end
end
right_num = [right_num; (1.0-length(error)/(class_num*test_num))*100 ];
right_num
end
PCA(主成分分析法)和ICA(独立成分分析法)的MATLAB源程序
3星 · 超过75%的资源 需积分: 0 191 浏览量
更新于2014-12-26
16
收藏 3KB RAR 举报
PCA(主成分分析)与ICA(独立成分分析)是数据降维和特征提取中的两种重要技术,常用于图像处理等领域。MATLAB作为一种强大的数学计算软件,提供了实现这两种方法的便捷平台。
PCA是一种线性变换方法,它通过找到数据集中的主要变异方向(主成分),将原始高维数据转换为一组新的正交坐标系统,使得新坐标系中的第一轴(主成分)解释了原始数据的最大方差,第二轴解释了剩余方差中的最大部分,以此类推。PCA的主要优点在于能有效降低数据的复杂性,同时保留大部分信息。在MATLAB中,可以使用`princomp`函数进行PCA操作,该函数对数据进行中心化处理并计算出主成分。
PCAFLD(主成分分析 Fisher 分类器)是PCA与Fisher判别分析的结合,它在PCA的基础上进一步优化,寻找能最大化类间距离同时最小化类内距离的投影方向,从而提高分类性能。在MATLAB中,PCAFLD的实现可能涉及到自定义函数,如`PCAFLD.m`所示,这可能包含PCA的实现以及Fisher判别分析的步骤。
ICA则是一种非线性的盲源分离方法,它的目标是从混合信号中恢复出原始的独立源。在图像处理中,ICA常用于去除噪声或分离不同的图像特征。ICA假设源信号是统计独立的,而观测信号是这些源信号的线性组合。MATLAB中实现ICA通常会用到`fastica`函数,该函数基于快速ICA算法,可以高效地估计独立分量。
在提供的文件列表中,`pca_ica.m`可能是执行PCA和ICA过程的MATLAB脚本,它可能包含了数据预处理、模型训练和结果可视化等步骤。`PCAFLD.m`是PCA与Fisher判别分析结合的实现,适用于分类任务。`www.pudn.com.txt`可能是下载这些资源的来源说明或相关文档。
PCA和ICA在MATLAB中的应用是数据科学和图像处理领域中的核心工具。理解并掌握这两种方法的原理和MATLAB实现,对于提升数据分析能力,特别是在高维数据处理和特征选择上具有重要意义。实际应用中,用户需要根据具体的数据特性及需求,选择合适的降维和特征提取策略,以达到最佳的模型性能和解释性。