PCA和白化练习之处理二维数据
2014-10-20 15:10
351 查看
在很多情况下,我们要处理的数据的维度很高,需要提取主要的特征进行分析这就是PCA(主成分分析),白化是为了减少各个特征之间的冗余,因为在许多自然数据中,各个特征之间往往存在着一种关联,为了减少特征之间的关联,需要用到所谓的白化(whitening).
首先下载数据pcaData.rar,下面要对这里面包含的45个2维样本点进行PAC和白化处理,数据中每一列代表一个样本点。
第一步 画出原始数据:
第二步:执行PCA,找到数据变化最大的方向:
第三步:将原始数据投射到上面找的两个方向上:
第四步:降维,此例中把数据由2维降维到1维,画出降维后的数据:
第五步:PCA白化处理:
第六步:ZCA白化处理:
下面是程序matlab源代码:
首先下载数据pcaData.rar,下面要对这里面包含的45个2维样本点进行PAC和白化处理,数据中每一列代表一个样本点。
第一步 画出原始数据:
第二步:执行PCA,找到数据变化最大的方向:
第三步:将原始数据投射到上面找的两个方向上:
第四步:降维,此例中把数据由2维降维到1维,画出降维后的数据:
第五步:PCA白化处理:
第六步:ZCA白化处理:
下面是程序matlab源代码:
close all;clear all;clc; %%================================================================ %% 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 this sigma = x * x'/ size(x, 2); [u,S,V] = svd(sigma); % -------------------------------------------------------- hold on plot([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 this xRot = 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 z = u(:, 1:k)' * x; xHat = u(:,1:k) * z; % -------------------------------------------------------- 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 this xPCAWhite = diag(1 ./ sqrt(diag(S) + epsilon)) * xRot; % -------------------------------------------------------- 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 this xZCAWhite = 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. :)
相关文章推荐
- 【转】PCA和白化练习之处理二维数据
- PCA和白化练习之处理图像
- Deep learning:十一(PCA和whitening在二维数据中的练习)
- 二维数据的白化处理
- Python数据处理 PCA/ZCA 白化(UFLDL教程:Exercise:PCA_in_2D&PCA_and_Whitening)
- Deep learning:十一(PCA和whitening在二维数据中的练习)
- Deep learning:十一(PCA和whitening在二维数据中的练习)
- 数据降维处理:PCA之奇异值分解(SVD)介绍
- HTML5灰度图像处理练习3:图像数据传输
- 第3章 处理数据 (程序清单、编程练习)
- 大数据处理例之STL实战练习
- 数据预处理的几个方法:白化、去均值、归一化、PCA
- (Python实现)数据PCA降维白化和L2归一化-深度学习实践常用数据预处理
- Matlab数据分析练习-平滑处理
- 数据白化处理对原始数据X做的变换的理解
- 《c primer pius》第十章第6题,编写一个程序,初始化一个二维double数组,并利用练习2中的任一函数来把这个数组复制到另一个二维数组(因为二维数组是数组的数组,所以可以使用处理一维数组的
- 数据挖掘-K-近邻分类器-Iris数据集分析-PCA降维处理后显示分类情况(三)
- #每日Linux小练习#05 awk-好用的数据处理工具
- 大数据处理中的小练习_MySql_1
- PCA处理二维情况