Exercise: PCA in 2D
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的更多相关文章
- 【DeepLearning】Exercise:PCA in 2D
Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...
- 【DeepLearning】Exercise:PCA and Whitening
Exercise:PCA and Whitening 习题链接:Exercise:PCA and Whitening pca_gen.m %%============================= ...
- Deep Learning 4_深度学习UFLDL教程:PCA in 2D_Exercise(斯坦福大学深度学习教程)
前言 本节练习的主要内容:PCA,PCA Whitening以及ZCA Whitening在2D数据上的使用,2D的数据集是45个数据点,每个数据点是2维的.要注意区别比较二维数据与二维图像的不同,特 ...
- UFLDL教程笔记及练习答案二(预处理:主成分分析和白化)
首先将本节主要内容记录下来.然后给出课后习题的答案. 笔记: :首先我想推导用SVD求解PCA的合理性. PCA原理:如果样本数据X∈Rm×n.当中m是样本数量,n是样本的维数.PCA降维的目的就是为 ...
- Deep Learning 教程(斯坦福深度学习研究团队)
http://www.zhizihua.com/blog/post/602.html 说明:本教程将阐述无监督特征学习和深度学习的主要观点.通过学习,你也将实现多个功能学习/深度学习算法,能看到它们为 ...
- [Scikit-learn] 4.3 Preprocessing data
数据分析的重难点,就这么来了,欢迎欢迎,热烈欢迎. 4. Dataset transformations 4.3. Preprocessing data 4.3.1. Standardization, ...
- UFLDL教程之(三)PCA and Whitening exercise
Exercise:PCA and Whitening 第0步:数据准备 UFLDL下载的文件中,包含数据集IMAGES_RAW,它是一个512*512*10的矩阵,也就是10幅512*512的图像 ( ...
- PCA and kmeans MATLAB实现
MATLAB基础知识 l Imread: 读取图片信息: l axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和 ...
- Deep Learning 5_深度学习UFLDL教程:PCA and Whitening_Exercise(斯坦福大学深度学习教程)
前言 本文是基于Exercise:PCA and Whitening的练习. 理论知识见:UFLDL教程. 实验内容:从10张512*512自然图像中随机选取10000个12*12的图像块(patch ...
随机推荐
- AngularJS 导航栏动态添加.active
在传统jQuery中,实现导航栏动态添加.active类的思路比较简单,就是当点击的时候,清除其他.active,然后给当前类加上.active. 但是在AngularJS中,就不能再采用这种jQue ...
- Spring MVC 核心架构图
架构图对应的DispatcherServlet核心代码如下: //前端控制器分派方法 protected void doDispatch(HttpServletRequest request, Htt ...
- ng-show ng-hide ng-if的区别
用途 ng-show ng-hide ng-if三个都可以用来控制页面DOM元素的显示与隐藏. ng-hide条件为true时,隐藏所在元素,false时显示所在元素. ng-show相反,条件为tr ...
- 【Henu ACM Round#19 A】 Vasya the Hipster
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题. 两个一起用->min(a,b); 剩下的除2加上去就好 [代码] #include <bits/stdc++. ...
- maven中使用mybatis
1.Mybatis优缺点 优点: Mybatis实现了对Dao层的封装,隔离了SQL语句,便于管理,避免了像JDBC那样操作数据集,便于扩展等等. 缺点: Mybatis属于?半自动“ORM”,比Hi ...
- android 推断是否支持闪光灯
近期在做录制视频功能,在找一些资料时发现 要推断是否支持闪关灯,在这记录下来,怕以后忘记 public static boolean isSupportCameraLedFlash(PackageMa ...
- Android布局文件的载入过程分析:Activity.setContentView()源代码分析
大家都知道在Activity的onCreate()中调用Activity.setContent()方法能够载入布局文件以设置该Activity的显示界面.本文将从setContentView()的源代 ...
- 免费WiFi初体验——个小白的WiFi旅程
说来羞愧,真正接触到WiFi还是在毕业后,此前自己封闭在一个人的世界,再加上外在学校的包围,我还成了个"山里"的孩子. 去年毕业了,也算是个90后,可自觉得心态过于成熟.了解外界太 ...
- 安装vnc出现的问题
重启vnc 命令:/sbin/service vncserver start或者vncserver VNC的启动/停止/重启 #service vncserver start/stop/restart ...
- POJ 1671 第二类斯特林数
思路: 递推出来斯特林数 求个和 if(i==j)f[i][j]=1; else f[i][j]=f[i-1][j-1]+f[i-1][j]*j; //By SiriusRen #include &l ...