1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| %% fabrication des donnees jouet 2D : un cercle et un segment
N = 128; % 128 points
tmp = exp(i*rand(N/2,1)*2*pi)*2;
Xcercle = [real(tmp),imag(tmp)] + randn(N/2,2)*0.1;
Xtrait = rand(N/2,1)*[1,-0.5];
X = [Xcercle; Xtrait];
%% affichage en 2D
subplot(211);
plot(Xcercle(:,1),Xcercle(:,2),'+b');
hold on;
plot(Xtrait(:,1),Xtrait(:,2),'or');
hold off;
%% calcul de la matrice des produits scalaires (application de la fonction noyau)
matDist = repmat(sum(X.^2,2),1,N) + repmat(sum(X.^2,2),1,N)' - X*X';
sigma2 = 400;
K = exp(-matDist / (2*sigma2));
%% diagonalisation de K pour récupérer les points dans l'espace de resdescription K = Y * Y'
[P,Lambda] = eig(K);
% c'est pas precis, certain lambda sont < 0 !
Y = P * max(Lambda,0).^0.5;
% projection dans PCA des points de Y
PCAMat = princomp(Y);
Ypca = Y * PCAMat;
%% affichage dans l'espace kernel PCA
subplot(212);
plot3(Ypca(1:N/2,1),Ypca(1:N/2,2),Ypca(1:N/2,3),'+b');
hold on;
plot3(Ypca(N/2+1:N,1),Ypca(N/2+1:N,2),Ypca(N/2+1:N,3),'or');
hold off;
%% affichage de la matrice de similarité
figure; imagesc(K); |
Partager