您的位置:首页 > 其它

二维数据的白化处理

2016-04-30 10:47 726 查看

二维数据的白化处理

这篇博客实现起来比较简单,首先先去下载pca_2d.zip,然后打开pca_2d.m改代码,具体代码见下面
close all

%%================================================================

%% Step 0: Load data

% We have provided the code to load data from pcaData.txt into x.

% x is a 2 * 45 matrix, where the kth column x(:,k) corresponds to

% the kth data point.Here we provide the code to load natural image data into x.

% You do not need to change the code below.

%从txt文件里面加载数据,并画出原始数据散点图

x = load('pcaData.txt','-ascii');

figure(1);

scatter(x(1, :), x(2, :));

title('Raw data');

%%================================================================

%% Step 1a: Implement PCA to obtain U

% Implement PCA to obtain the rotation matrix U, which is the eigenbasis

% sigma.

% -------------------- YOUR CODE HERE --------------------

%得到特征向量,u是特征向量,s是特征值,v是u'

u = zeros(size(x, 1)); % You need to compute this

[u,s,v]=svd(x);

% --------------------------------------------------------

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

% Now, compute xRot by projecting the data on to the basis defined

% by U. Visualize the points by performing a scatter plot.

% -------------------- YOUR CODE HERE --------------------

%计算出xRot

xRot = zeros(size(x)); % You need to compute this

xRot=u'*x;

% --------------------------------------------------------

% Visualise the covariance matrix. You should see a line across the

% diagonal against a blue background.

figure(2);

scatter(xRot(1, :), xRot(2, :));

title('xRot');

%%================================================================

%% Step 2: Reduce the number of dimensions from 2 to 1.

% Compute xRot again (this time projecting to 1 dimension).

% Then, compute xHat by projecting the xRot back onto the original axes

% to see the effect of dimension reduction

% -------------------- YOUR CODE HERE --------------------

%得到xHat,去除第二维向量

k = 1; % Use k = 1 and project the data onto the first eigenbasis

xHat = zeros(size(x)); % You need to compute this

xHat(1:k,:)=xRot(1:k,:);

xHat=u*xHat;

% --------------------------------------------------------

figure(3);

scatter(xHat(1, :), xHat(2, :));

title('xHat');

%%================================================================

%% Step 3: PCA Whitening

% Complute xPCAWhite and plot the results.

%PCA白化处理,使用epsilon是为了正则化

epsilon = 1e-5;

% -------------------- YOUR CODE HERE --------------------

xPCAWhite = zeros(size(x)); % You need to compute this

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.

%ZCA处理,使得结果更接近原始数据

% -------------------- YOUR CODE HERE --------------------

xZCAWhite = zeros(size(x)); % You need to compute this

xZCAWhite = u*xPCAWhite;

% --------------------------------------------------------

figure(5);

scatter(xZCAWhite(1, :), xZCAWhite(2, :));

title('xZCAWhite');

%% Congratulations! When you have reached this point, you are done!

% You can now move onto the next PCA exercise. :)

最终你会看到6张图片











内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: