Exercise:PCA in 2D 代码示例

练习参考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');





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值