博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【DeepLearning】Exercise:PCA in 2D
阅读量:5220 次
发布时间:2019-06-14

本文共 3282 字,大约阅读时间需要 10 分钟。

Exercise:PCA in 2D

习题的链接:

 

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.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 thissigma = (x*x') ./ size(x,2);    %covariance matrix[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 eigenbasis%xHat = zeros(size(x)); % You need to compute this%Recovering an Approximation of the DataxRot(k+1:size(x,1), :) = 0;xHat = u*xRot;% -------------------------------------------------------- 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 * 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. :)

 

转载于:https://www.cnblogs.com/ganganloveu/p/4202337.html

你可能感兴趣的文章
eclipse把局部变量提为全局变量的快捷键是什么
查看>>
《研磨设计模式》——可配置的简单工厂
查看>>
为网站添加免费的访问计数统计和加入微博
查看>>
ubuntu root用户 默认密码
查看>>
对百度搜索法的分析评价
查看>>
网络知识之ipset
查看>>
Wordpress“固定链接”页面出现404原因及解决方法
查看>>
WPF控件经验小结:(1) ToolBar去掉右边箭头(扩展图标)
查看>>
Credit Memo和Debit Memo在AR以及AP中的概念比较
查看>>
在Azure上部署Sqlserver网络访问不了的问题
查看>>
关于优酷视频代码播放的若干事情……
查看>>
【Leetcode】Triangle
查看>>
hdu 1561 The more, The Better(树形dp入门)
查看>>
每天CookBook之JavaScript-060
查看>>
提高PHP性能的47个技巧
查看>>
win 7 64位 配置silverlight 32位的应用程序(sl网站)
查看>>
负载均衡配置篇(Nginx)
查看>>
软件工程 speedsnail 第二次冲刺2
查看>>
字符编码的过滤器Filter(即输入的汉字,能在页面上正常显示,不会出现乱码)...
查看>>
最小度限制生成树模板
查看>>