在很多情况下,我们要处理的数据的维度很高,需要提取主要的特征进行分析这就是PCA(主成分分析),白化是为了减少各个特征之间的冗余,因为在许多自然数据中,各个特征之间往往存在着一种关联,为了减少特征之间的关联,需要用到所谓的白化(whitening).

首先下载数据pcaData.rar,下面要对这里面包含的45个2维样本点进行PAC和白化处理,数据中每一列代表一个样本点。

第一步 画出原始数据:

第二步:执行PCA,找到数据变化最大的方向:

第三步:将原始数据投射到上面找的两个方向上:

第四步:降维,此例中把数据由2维降维到1维,画出降维后的数据:

第五步:PCA白化处理:

第六步:ZCA白化处理:

下面是程序matlab源代码:

  1 close all;clear all;clc;
  2
  3 %%================================================================
  4 %% Step 0: Load data
  5 %  We have provided the code to load data from pcaData.txt into x.
  6 %  x is a 2 * 45 matrix, where the kth column x(:,k) corresponds to
  7 %  the kth data point.Here we provide the code to load natural image data into x.
  8 %  You do not need to change the code below.
  9
 10 x = load('pcaData.txt','-ascii');
 11 figure(1);
 12 scatter(x(1, :), x(2, :));
 13 title('Raw data');
 14
 15
 16 %%================================================================
 17 %% Step 1a: Implement PCA to obtain U
 18 %  Implement PCA to obtain the rotation matrix U, which is the eigenbasis
 19 %  sigma.
 20
 21 % -------------------- YOUR CODE HERE --------------------
 22 u = zeros(size(x, 1)); % You need to compute this
 23
 24 sigma = x * x'/ size(x, 2);
 25 [u,S,V] = svd(sigma);
 26
 27
 28
 29 % --------------------------------------------------------
 30 hold on
 31 plot([0 u(1,1)], [0 u(2,1)]);
 32 plot([0 u(1,2)], [0 u(2,2)]);
 33 scatter(x(1, :), x(2, :));
 34 hold off
 35
 36 %%================================================================
 37 %% Step 1b: Compute xRot, the projection on to the eigenbasis
 38 %  Now, compute xRot by projecting the data on to the basis defined
 39 %  by U. Visualize the points by performing a scatter plot.
 40
 41 % -------------------- YOUR CODE HERE --------------------
 42 xRot = zeros(size(x)); % You need to compute this
 43 xRot = u' * x;
 44
 45 % --------------------------------------------------------
 46
 47 % Visualise the covariance matrix. You should see a line across the
 48 % diagonal against a blue background.
 49 figure(2);
 50 scatter(xRot(1, :), xRot(2, :));
 51 title('xRot');
 52
 53 %%================================================================
 54 %% Step 2: Reduce the number of dimensions from 2 to 1.
 55 %  Compute xRot again (this time projecting to 1 dimension).
 56 %  Then, compute xHat by projecting the xRot back onto the original axes
 57 %  to see the effect of dimension reduction
 58
 59 % -------------------- YOUR CODE HERE --------------------
 60 k = 1; % Use k = 1 and project the data onto the first eigenbasis
 61 xHat = zeros(size(x)); % You need to compute this
 62 z = u(:, 1:k)' * x;
 63 xHat = u(:,1:k) * z;
 64
 65 % --------------------------------------------------------
 66 figure(3);
 67 scatter(xHat(1, :), xHat(2, :));
 68 title('xHat');
 69
 70
 71 %%================================================================
 72 %% Step 3: PCA Whitening
 73 %  Complute xPCAWhite and plot the results.
 74
 75 epsilon = 1e-5;
 76 % -------------------- YOUR CODE HERE --------------------
 77 xPCAWhite = zeros(size(x)); % You need to compute this
 78
 79 xPCAWhite = diag(1 ./ sqrt(diag(S) + epsilon)) * xRot;
 80
 81
 82
 83 % --------------------------------------------------------
 84 figure(4);
 85 scatter(xPCAWhite(1, :), xPCAWhite(2, :));
 86 title('xPCAWhite');
 87
 88 %%================================================================
 89 %% Step 3: ZCA Whitening
 90 %  Complute xZCAWhite and plot the results.
 91
 92 % -------------------- YOUR CODE HERE --------------------
 93 xZCAWhite = zeros(size(x)); % You need to compute this
 94
 95 xZCAWhite = u * xPCAWhite;
 96 % --------------------------------------------------------
 97 figure(5);
 98 scatter(xZCAWhite(1, :), xZCAWhite(2, :));
 99 title('xZCAWhite');
100
101 %% Congratulations! When you have reached this point, you are done!
102 %  You can now move onto the next PCA exercise. :)

转载于:https://www.cnblogs.com/90zeng/p/PCA_and_Whitening_2D.html

PCA和白化练习之处理二维数据相关推荐

  1. Deep learning:十一(PCA和whitening在二维数据中的练习)

    前言: 这节主要是练习下PCA,PCA Whitening以及ZCA Whitening在2D数据上的使用,2D的数据集是45个数据点,每个数据点是2维的.参考的资料是:Exercise:PCA in ...

  2. Python一维二维数据的格式化和处理

    本章导言 什么是数据格式化 前言: -学完本章,看待数据会有一种规范/格式化的视角 -方法论:从Python角度理解文件和数据表示 -实践能力:学会编写带有文件输入输出的程序 1. 数据组织的维度 维 ...

  3. 【数据挖掘】数据挖掘总结 ( K-Means 聚类算法 | 二维数据的 K-Means 聚类 ) ★

    文章目录 一. K-Means 聚类算法流程 二. 二维数据的 K-Means 聚类 1. 第一次迭代 2. 第二次迭代 参考博客 : [数据挖掘]聚类算法 简介 ( 基于划分的聚类方法 | 基于层次 ...

  4. 【数据挖掘】K-Means 二维数据聚类分析 ( K-Means 迭代总结 | K-Means 初始中心点选择方案 | K-Means 算法优缺点 | K-Means 算法变种 )

    文章目录 K-Means 二维数据 聚类分析 数据样本及聚类要求 二维数据曼哈顿距离计算 K-Means 算法 步骤 第一次迭代 : 步骤 ( 1 ) 中心点初始化 第一次迭代 : 步骤 ( 2 ) ...

  5. iterp2函数--------二维数据内插值

    [语法说明] 1.zi=interp1(x,y,z,xi,yi):返回矩阵zi,其元素包含对应于参量xi和yi的元素.用户可以输入行向量和列向量xi和yi,此时,输出向量zi与矩阵meshgrid(x ...

  6. 径向基函数插值(3)二维数据的插值

    二维数据的插值过程跟一维数据的过程一样, 只不过在显示二维的数据插值的过程中,需要meshgrid函数产生数据,进行显示插入数据的输出值. 产生二维数据: clear all; figure; %** ...

  7. matlab创建二叉树(二维数据)

    一.学习要点 1.注意matlab中全局变量与局部变量的区别:本文中assigned_nn为局部变量,每一次递归中的值都是不一样的,node_nubmer为全局变量,当前值的改变如递增,必回影响以后每 ...

  8. 一维二维_Excel二维数据转一维,2种方法轻松搞定

    今天是2020年1月1日,祝各位小伙伴们新年快乐,开心每一天~ 如下所示,左边是二维交叉数据表,我们希望快速转换成右边的一维数据表 如果复制粘贴,效率太低了,今天分享两种方法,实现快速转换 1.pow ...

  9. c#二维数据最大最小值_C#| 打印类型,各种数据类型的最大值和最小值

    c#二维数据最大最小值 In the below example – we are printing types, min value, max value of various data types ...

最新文章

  1. 体素科技:2018年,算法驱动下的医学影像分析进展
  2. 解除Ubuntu系统的root登录图形界面限制
  3. mysql_常用命令
  4. 公用ip地址查询_是什么使您无法更改公用IP地址并在Internet上造成严重破坏?
  5. mysql 数据备份方案_MySQL常见备份方案
  6. C#调用Matlab生成的dll方法
  7. 设计模式--适配器(Adapter)
  8. Java与微信不得不说的故事——消息的接收与发送
  9. 通信尾纤常用尾纤简介
  10. 互联网公司去年到今年的大批裁员,难道程序员没有未来了吗?
  11. oracle 日期的常用写法
  12. WormHole是一个简单、易用的api管理平台,支持dubbo服务调用
  13. linux命令之jq
  14. 基于 Word2Vec 和 SVM 的微博舆情情感演化分析 论文笔记
  15. 向量空间中的基底和基变换以及坐标变换
  16. Android清洁架构(一)
  17. 浅谈ArcGIS中的容差和分辨率
  18. 如何把无线路由器变成无线交换机使用?
  19. 计算机丢失device.dll,修复xritedevice.dll
  20. learning的反义词英文_英语反义词

热门文章

  1. keras和tensorflow 报错解决:UserWarning: Method on_batch_end() is slow compared to the batch update Check
  2. 通俗易懂:快速理解ipv4的NAT穿透原理
  3. Ubuntu安装python3.7,并将python链接指向python3.7
  4. mysql 半同步复制_Mysql半同步复制原理及问题排查
  5. redis的过期策略和淘汰策略
  6. LeetCode-笔记-57.插入区间
  7. jquery ajax 省 城市 二级菜单 源码,利用了jquery的ajax实现二级联互动菜单
  8. 超全的数据库建表/SQL/索引规范,适合贴在工位上!
  9. 修改值类型的实例方法 mutating
  10. 想转行软件测试,简历怎么包装成1年工作经验的测试工程师