Exercise: PCA in 2D

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   資料   

 Step 0: Load data

The starter code contains code to load 45 2D data points. When plotted using the scatter function, the results should look like the following:

 

Step 1: Implement PCA

In this step, you will implement PCA to obtain xrot, the matrix in which the data is "rotated" to the basis comprising made up of the principal components

 

Step 1a: Finding the PCA basis

Find and , and draw two lines in your figure to show the resulting basis on top of the given data points.

Step 1b: Check xRot

Compute xRot, and use the scatter function to check that xRot looks as it should, which should be something like the following:

 

Step 2: Dimension reduce and replot

In the next step, set k, the number of components to retain, to be 1

Step 3: PCA Whitening

Step 4: ZCA Whitening

 

Code

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.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 = zeros(size(x, 1)); % You need to compute this[n m]=size(x);% x=x-repmat(mean(x,2),1,m);  %預先處理,均值為零 —— 2維,每一維減去該維上的均值sigma=(1.0/m)*x*x‘; % 共變數矩陣[u s v]=svd(sigma);% -------------------------------------------------------- hold onplot([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 = zeros(size(x)); % You need to compute thisxRot=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 -------------------- k = 1; % Use k = 1 and project the data onto the first eigenbasisxHat = zeros(size(x)); % You need to compute thisxHat = u*([u(:,1),zeros(n,1)]‘*x); % 降維% 使特徵點落在特徵向量所指的方向上而不是原座標繫上% -------------------------------------------------------- figure(3);scatter(xHat(1, :), xHat(2, :));title(‘xHat‘);%%================================================================%% Step 3: PCA Whitening%  Complute xPCAWhite and plot the results.epsilon = 1e-5;% -------------------- YOUR CODE HERE -------------------- xPCAWhite = zeros(size(x)); % You need to compute thisxPCAWhite = diag(1./sqrt(diag(s)+epsilon))*u‘*x;  % 每個特徵除以對應的特徵向量,以使每個特徵有一致的方差% -------------------------------------------------------- figure(4);scatter(xPCAWhite(1, :), xPCAWhite(2, :));title(‘xPCAWhite‘);%%================================================================%% Step 3: ZCA Whitening%  Complute xZCAWhite and plot the results.% -------------------- YOUR CODE HERE -------------------- xZCAWhite = zeros(size(x)); % You need to compute thisxZCAWhite = u*diag(1./sqrt(diag(s)+epsilon))*u‘*x;% -------------------------------------------------------- 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. :)

Exercise: PCA in 2D

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.