Stanford UFLDL教程 Exercise:PCA in 2D

Exercise:PCA in 2D

Contents

[hide]

PCA, PCA whitening and ZCA whitening in 2D

In this exercise you will implement PCA, PCA whitening and ZCA whitening, as described in the earlier sections of this tutorial, and generate the images shown in the earlier sections yourself. You will build on the starter code that has been provided atpca_2d.zip. You need only write code at the places indicated by "YOUR CODE HERE" in the files. The only file you need to modify ispca_2d.m. Implementing this exercise will make the next exercise significantly easier to understand and complete.

Step 0: Load data

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

Raw images

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\textstyle u_1, \ldots, u_n made up of the principal components. As mentioned in the implementation notes, you should make use of MATLAB'ssvd function here.

Step 1a: Finding the PCA basis

Find \textstyle u_1 and\textstyle u_2, and draw two lines in your figure to show the resulting basis on top of the given data points. You may find it useful to use MATLAB's hold on and hold off functions. (After calling hold on, plotting functions such asplot will draw the new data on top of the previously existing figure rather than erasing and replacing it; andhold off turns this off.) You can use plot([x1,x2], [y1,y2], '-') to draw a line between(x1,y1) and (x2,y2). Your figure should look like this:


Pca 2d basis.png

If you are doing this in Matlab, you will probably get a plot that's identical to ours. However, eigenvectors are defined only up to a sign. I.e., instead of returning\textstyle u_1 as the first eigenvector, Matlab/Octave could just as easily have returned\textstyle -u_1, and similarly instead of\textstyle u_2 Matlab/Octave could have returned\textstyle -u_2. So if you wound up with one or both of the eigenvectors pointing in a direction opposite (180 degrees difference) from what's shown above, that's okay too.

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:

Pca xrot 2d.png

Because Matlab/Octave could have returned \textstyle -u_1 and/or\textstyle -u_2 instead of\textstyle u_1 and\textstyle u_2, it's also possible that you might have gotten a figure which is "flipped" or "reflected" along the\textstyle x- and/or\textstyle y-axis; a flipped/reflected version of this figure is also a completely correct result.

Step 2: Dimension reduce and replot

In the next step, set k, the number of components to retain, to be 1 (we have already done this for you). Compute the resultingxHat and plot the results. You should get the following (this figure shouldnot be flipped along the \textstyle x- or\textstyle y-axis):

Pca xhat 2d.png

Step 3: PCA Whitening

Implement PCA whitening using the formula from the notes. Plot xPCAWhite, and verify that it looks like the following (a figure that is flipped/reflected on either/both axes is also correct):

Pca white 2d.png

Step 4: ZCA Whitening

Implement ZCA whitening and plot the results. The results should look like the following (this should not be flipped/reflected along the\textstyle x- or\textstyle y-axis):

Zca white 2d.png

from: http://ufldl.stanford.edu/wiki/index.php/Exercise:PCA_in_2D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值