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 : Load data
% We have provided the code to load data from pcaData.txt into x.
% x is a * 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();
scatter(x(, :), x(, :)); % 用圆圈绘制出数据分布
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, )); % You need to compute this
[n m]=size(x);
% x=x-repmat(mean(x,),,m); %预处理,均值为零 —— 2维,每一维减去该维上的均值
sigma=(1.0/m)*x*x'; % 协方差矩阵
[u s v]=svd(sigma); % --------------------------------------------------------
hold on
plot([ u(,)], [ u(,)]); % 画第一条线
plot([ u(,)], [ u(,)]); % 画第二条线
scatter(x(, :), x(, :));
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 this
xRot=u'*x; % -------------------------------------------------------- % Visualise the covariance matrix. You should see a line across the
% diagonal against a blue background.
figure();
scatter(xRot(, :), xRot(, :));
title('xRot'); %%================================================================
%% Step : Reduce the number of dimensions from to .
% Compute xRot again (this time projecting to dimension).
% Then, compute xHat by projecting the xRot back onto the original axes
% to see the effect of dimension reduction % -------------------- YOUR CODE HERE --------------------
k = ; % Use k = and project the data onto the first eigenbasis
xHat = zeros(size(x)); % You need to compute this
xHat = u*([u(:,),zeros(n,)]'*x); % 降维
% 使特征点落在特征向量所指的方向上而不是原坐标系上 % --------------------------------------------------------
figure();
scatter(xHat(, :), xHat(, :));
title('xHat'); %%================================================================
%% Step : PCA Whitening
% Complute xPCAWhite and plot the results. epsilon = 1e-;
% -------------------- YOUR CODE HERE --------------------
xPCAWhite = zeros(size(x)); % You need to compute this
xPCAWhite = diag(./sqrt(diag(s)+epsilon))*u'*x; % 每个特征除以对应的特征向量,以使每个特征有一致的方差
% --------------------------------------------------------
figure();
scatter(xPCAWhite(, :), xPCAWhite(, :));
title('xPCAWhite'); %%================================================================
%% Step : ZCA Whitening
% Complute xZCAWhite and plot the results. % -------------------- YOUR CODE HERE --------------------
xZCAWhite = zeros(size(x)); % You need to compute this
xZCAWhite = u*diag(./sqrt(diag(s)+epsilon))*u'*x; % --------------------------------------------------------
figure();
scatter(xZCAWhite(, :), xZCAWhite(, :));
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的更多相关文章

  1. 【DeepLearning】Exercise:PCA in 2D

    Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...

  2. 【DeepLearning】Exercise:PCA and Whitening

    Exercise:PCA and Whitening 习题链接:Exercise:PCA and Whitening pca_gen.m %%============================= ...

  3. Deep Learning 4_深度学习UFLDL教程:PCA in 2D_Exercise(斯坦福大学深度学习教程)

    前言 本节练习的主要内容:PCA,PCA Whitening以及ZCA Whitening在2D数据上的使用,2D的数据集是45个数据点,每个数据点是2维的.要注意区别比较二维数据与二维图像的不同,特 ...

  4. UFLDL教程笔记及练习答案二(预处理:主成分分析和白化)

    首先将本节主要内容记录下来.然后给出课后习题的答案. 笔记: :首先我想推导用SVD求解PCA的合理性. PCA原理:如果样本数据X∈Rm×n.当中m是样本数量,n是样本的维数.PCA降维的目的就是为 ...

  5. Deep Learning 教程(斯坦福深度学习研究团队)

    http://www.zhizihua.com/blog/post/602.html 说明:本教程将阐述无监督特征学习和深度学习的主要观点.通过学习,你也将实现多个功能学习/深度学习算法,能看到它们为 ...

  6. [Scikit-learn] 4.3 Preprocessing data

    数据分析的重难点,就这么来了,欢迎欢迎,热烈欢迎. 4. Dataset transformations 4.3. Preprocessing data 4.3.1. Standardization, ...

  7. UFLDL教程之(三)PCA and Whitening exercise

    Exercise:PCA and Whitening 第0步:数据准备 UFLDL下载的文件中,包含数据集IMAGES_RAW,它是一个512*512*10的矩阵,也就是10幅512*512的图像 ( ...

  8. PCA and kmeans MATLAB实现

    MATLAB基础知识 l  Imread:  读取图片信息: l  axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和  ...

  9. Deep Learning 5_深度学习UFLDL教程:PCA and Whitening_Exercise(斯坦福大学深度学习教程)

    前言 本文是基于Exercise:PCA and Whitening的练习. 理论知识见:UFLDL教程. 实验内容:从10张512*512自然图像中随机选取10000个12*12的图像块(patch ...

随机推荐

  1. manacherO(n)求最长回文子串 hihocoder1032

    原文地址:https://segmentfault.com/a/1190000003914228   http://blog.csdn.net/synapse7/article/details/189 ...

  2. TP5 安装

    一.官方手册: https://www.kancloud.cn/manual/thinkphp5/118003 二.Git 方式安装[最新框架下载方式] 首先克隆下载应用项目仓库 git clone ...

  3. 记intel杯比赛中各种bug与debug【其四】:基于长短时记忆神经网络的中文分词的实现

    (标题长一点就能让外行人感觉到高大上) 直接切入主题好了,这个比赛还必须一个神经网络才可以 所以我们结合主题,打算写一个神经网络的中文分词 这里主要写一下数据的收集和处理,网络的设计,代码的编写和模型 ...

  4. ECNUOJ 2575 Separate Connections

    Separate Connections Time Limit:5000MS Memory Limit:65536KBTotal Submit:421 Accepted:41 Description  ...

  5. 解决MAC下PHP连接MYSQL错误Warning: mysql_connect(): No such file or directory in conn.php

    今天在mac上用php去连接mysql数据库,出现了 mac PHP Warning:  mysql_connect(): [2002] No such file... 详细例如以下所看到的: Dir ...

  6. X的追求道路

    X的追求道路 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 X在大家的帮助下最终找到了一个妹纸,于是開始了漫漫的追求之路,那 ...

  7. 第六课 Struts的视图组件

    Struts框架的视图负责为客户提供动态网页内容. Struts的视图主要由JSP网页构成.此外还包含客户化的标签和ActionForm Bean.这些组件提供了 对国际化.接收用户输入的表单数据.表 ...

  8. 如何调试Blink?

    内容 尽管有很多工具和技巧可用于调试Blink,这个文章的重点调试Blink除布局測试之外的feature. 1 介绍 2 Linux 2.1 入门 2.2 启动Debugger 3 实用的工具 3. ...

  9. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...

  10. linux 不常用命令及命令组合

    lsof:list open files, sudo lsof | grep deleted:则列出虽然被删除,但还处于打开状态的文件.注意,这些文件占用的空间,只有在这些文件关闭时,才会被释放. m ...