clc;
clear all;
close all;
%%
opt.make_new_data = 0;
opt.plot_data = 0;
if opt.make_new_data
kpca.d = 2; % 데이터의 dimension
kpca.k = 3; % 클러스터의 수
kpca.N = 100; % 한 클러스터의 데이터의 수
kpca.Total = kpca.k*kpca.N;
% stage를 만든다.
kpca.stage.xmin = 0;
kpca.stage.xmax = 5;
kpca.stage.xsize = kpca.stage.xmax-kpca.stage.xmin;
kpca.stage.xres = 20;
kpca.stage.ymin = 0;
kpca.stage.ymax = 5;
kpca.stage.ysize = kpca.stage.ymax-kpca.stage.ymin;
kpca.stage.yres = 20;
kpca.stage.xgrid = linspace(kpca.stage.xmin, kpca.stage.xmax, kpca.stage.xres);
kpca.stage.ygrid = linspace(kpca.stage.ymin, kpca.stage.ymax, kpca.stage.yres);
[mesh_X, mesh_Y] = meshgrid(kpca.stage.xgrid, kpca.stage.ygrid);
kpca.stage.points = [mesh_X(:) mesh_Y(:)];
clear('mesh_X', 'mesh_Y');
kpca.stage.nr_points = length(kpca.stage.points);
% stage 안에 데이터를 만든다.
kpca.data = [];
for i = 1:kpca.k
% center를 만들고,
margin = 1;
kpca.cluster(i).center ...
= [kpca.stage.xmin+margin + (kpca.stage.xsize-2*margin)*rand() ...
, kpca.stage.ymin+margin + (kpca.stage.ysize-2*margin)*rand() ];
% center주변으로 데이터를 뿌린다.
concentrate_factor = 20; % 이 값이 클수록 데이터가 center에 몰린다.
kpca.cluster(i).data = repmat(kpca.cluster(i).center, kpca.N, 1) ...
+ [kpca.stage.xsize/concentrate_factor*randn(kpca.N, 1) ...
kpca.stage.ysize/concentrate_factor*randn(kpca.N, 1)];
kpca.data = [kpca.data ; kpca.cluster(i).data];
% etc
kpca.cluster(i).color = rand(1,3);
end
save('kpca_stage.mat', 'kpca');
else
load kpca_stage.mat;
end
% 데이터들을 그려보자.
if opt.plot_data
hold on;
for i = 1:kpca.k
plot(kpca.cluster(i).data(:,1), kpca.cluster(i).data(:,2)...
,'o', 'Color', kpca.cluster(i).color);
end
hold off;
axis([kpca.stage.xmin kpca.stage.xmax kpca.stage.ymin kpca.stage.ymax]);
end
%% kernel pca 를 한다.
kpca.opt_kernel.useSE_K = 1;
kpca.opt_kernel.sigma2f = 1;
kpca.opt_kernel.sigma2w = 0;
kpca.opt_kernel.sigma2x = 5E-1;
kpca.K = kpca_kernel(kpca.data, kpca.data, kpca.opt_kernel);
unit_N = 1/kpca.Total*ones(kpca.Total, kpca.Total);
kpca.Ktilt = kpca.K - unit_N*kpca.K - kpca.K*unit_N + unit_N*kpca.K*unit_N;
% Kernel의 eigenvalue를 구하자.
[kpca.evecs, kpca.evals] = eig(kpca.Ktilt);
kpca.evals = real(diag(kpca.evals));
for i = 1:kpca.Total,
kpca.evecs(:,i) = kpca.evecs(:,i)/(sqrt(kpca.evals(i)));
end
% Option. eig가 아니라 svd로 문제를 푼다.
% [kpca.evecs, kpca.evals] = svd(kpca.Ktilt);
%% grid의 각 점에 대해서 test를 한다.
kpca.Ktest = kpca_kernel(kpca.stage.points, kpca.data, kpca.opt_kernel);
unit_Ntest = 1/kpca.Total*ones(kpca.stage.nr_points, kpca.Total);
kpca.Ktest_tilt = kpca.Ktest - unit_Ntest*kpca.K - kpca.Ktest*unit_N + unit_Ntest*kpca.K*unit_N;
% feature를 뽑는다.
kpca.nr_features = 6;
kpca.test_features = kpca.Ktest_tilt * kpca.evecs(:,1:kpca.nr_features);
%% plot한다.
% 1. 총 6개의 feature에 대해서 어떻게 분류되나 확인
figure( 'Position' , [100 200 800 1000]);
for n = 1:kpca.nr_features,
subplot(3, 2, n);
axis([kpca.stage.xmin kpca.stage.xmax kpca.stage.ymin kpca.stage.ymax]);
kpca.imag = reshape(kpca.test_features(:,n), kpca.stage.yres, kpca.stage.xres);
axis('xy');
colormap(gray);
hold on;
pcolor(kpca.stage.xgrid, kpca.stage.ygrid, kpca.imag);
shading interp
contour(kpca.stage.xgrid, kpca.stage.ygrid, kpca.imag, 9, 'b');
plot(kpca.data(:,1), kpca.data(:,2), 'r.')
%title(sprintf('Eigenvalue=%4.3f', kpca.evals(n)));
title(sprintf(' %d th eigenvalue = %4.3f ', n, kpca.evals(n) ));
hold off; colorbar;
end
% 2. 세 개의 가장 큰 eigenvector로 분류된 feature의 값을 차례로 RGB에 넣고 plot
figure( 'Position' , [1000 200 1200 1000]);
rgb_field = zeros(kpca.stage.yres, kpca.stage.xres, 3);
for n = 1:3
kpca.imag = reshape(kpca.test_features(:,n), kpca.stage.yres, kpca.stage.xres);
rgb_field(:, :, n) = kpca.imag;
end
min_rgb = min(min(min(rgb_field)));
max_rgb = max(max(max(rgb_field)));
rgb_field = (rgb_field - min_rgb)/(max_rgb-min_rgb);
subplot(221);
imagesc(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field); colorbar;
hold on;plot(kpca.data(:,1), kpca.data(:,2), 'k.');hold off;
set(gca,'YDir','normal'); axis('xy');
title('RGB values by 3 features');
countour_step = 30;
subplot(222);
pcolor(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,1));shading interp; colorbar;
hold on;plot(kpca.data(:,1), kpca.data(:,2), 'k.');
contour(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,1), countour_step, 'Color', [.5 .5 .5]);hold off;
title('1st eigenvector');
subplot(223);
pcolor(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,2));shading interp; colorbar;
hold on;plot(kpca.data(:,1), kpca.data(:,2), 'k.');
contour(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,2), countour_step, 'Color', [.5 .5 .5]);hold off;
title('2nd eigenvector');
subplot(224);
pcolor(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,3));shading interp; colorbar;
hold on;plot(kpca.data(:,1), kpca.data(:,2), 'k.');
contour(kpca.stage.xgrid, kpca.stage.ygrid, rgb_field(:,:,3), countour_step, 'Color', [.5 .5 .5]);hold off;
title('3rd eigenvector');