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');
实现主成分分析和白化的过程是:
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');
相关文章推荐
- Stanford UFLDL教程 Exercise:PCA in 2D
- Python数据处理 PCA/ZCA 白化(UFLDL教程:Exercise:PCA_in_2D&PCA_and_Whitening)
- Exercise: PCA in 2D
- UFLDL教程答案(3):Exercise:PCA_in_2D&PCA_and_Whitening
- Exercise:PCA and Whitening 代码示例
- UFLDL教程Exercise答案(3.1):PCA in 2D
- 【DeepLearning】Exercise:PCA in 2D
- UFLDL教程:Exercise:PCA in 2D & PCA and Whitening
- UFLDL——Exercise: PCA in 2D 主成分分析
- Convolutional neural networks(CNN) (五) PCA in 2D Exercise
- Deep Learning 4_深度学习UFLDL教程:PCA in 2D_Exercise(斯坦福大学深度学习教程)
- 推荐个找代码示例的VS 插件 All-In-One Code Framework Sample Browser
- PCA 降维算法详解 以及代码示例
- UFLDL Exercise:PCA in 2D
- Creating a 2D Active Shape Model in ITK Using PCA
- think in java interview-高级开发人员面试宝典代码示例
- 推荐个找代码示例的VS 插件 All-In-One Code Framework Sample Browser(转)
- think in java interview-高级开发人员面试宝典代码示例
- 本系列love2d示例代码错误集中整理
- Exercise:Sparse Autoencoder 代码示例