您的位置:首页 > 编程语言

Exercise:PCA in 2D 代码示例

2014-12-22 14:51 316 查看
练习参考PCA in 2D

实现主成分分析和白化的过程是:





pca_2d.m中代码如下:

close all

%% Step 0: Load data
x = load('pcaData.txt','-ascii');
figure(1);
scatter(x(1, :), x(2, :));
title('Raw data');

%% Step 1a: Implement PCA to obtain U
sigma = x * x' / size(x, 2);
[u,s,v] = svd(sigma);

hold on
plot([0 u(1,1)], [0 u(2,1)]);
plot([0 u(1,2)], [0 u(2,2)]);
scatter(x(1, :), x(2, :));
hold off

%% Step 1b: Compute xRot, the projection on to the eigenbasis
xRot = u' * x;
figure(2);
scatter(xRot(1, :), xRot(2, :));
title('xRot');

%% Step 2: Reduce the number of dimensions from 2 to 1.
k = 1; % Use k = 1 and project the data onto the first eigenbasis
xde = u(:,1:k)' * x;
xHat = u(:,1:k) * xde;
figure(3);
scatter(xHat(1, :), xHat(2, :));
title('xHat');

%% Step 3: PCA Whitening
% Complute xPCAWhite and plot the results.
epsilon = 1e-5;
xPCAWhite = zeros(size(x));
xPCAWhite = diag(1./sqrt((diag(s) + epsilon))) * xRot;
figure(4);
scatter(xPCAWhite(1, :), xPCAWhite(2, :));
title('xPCAWhite');

%% Step 3: ZCA Whitening
% Complute xZCAWhite and plot the results.
xZCAWhite = zeros(size(x));
xZCAWhite = u * xPCAWhite;
figure(5);
scatter(xZCAWhite(1, :), xZCAWhite(2, :));
title('xZCAWhite');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  深度学习