MATLAB实例:PCA(主成成分分析)详解

摘要:
霍特林的T平方统计量是每个观测值的标准化分数的平方和,作为列向量返回。解释:各主要成分解释的总方差百分比,以及各主要成分贡献的比例。
MATLAB实例:PCA(主成成分分析)详解

作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

1. 主成成分分析

MATLAB实例:PCA(主成成分分析)详解第1张

MATLAB实例:PCA(主成成分分析)详解第2张

 MATLAB实例:PCA(主成成分分析)详解第3张

MATLAB实例:PCA(主成成分分析)详解第4张

MATLAB实例:PCA(主成成分分析)详解第5张

2. MATLAB解释

详细信息请看:Principal component analysis of raw data - mathworks

[coeff,score,latent,tsquared,explained,mu] = pca(X)

coeff = pca(X) returns the principal component coefficients, also known as loadings, for the n-by-p data matrix X. Rows of X correspond to observations and columns correspond to variables.

The coefficient matrix is p-by-p.

Each column of coeff contains coefficients for one principal component, and the columns are in descending order of component variance.

By default, pca centers the data and uses the singular value decomposition (SVD) algorithm.

coeff = pca(X,Name,Value) returns any of the output arguments in the previous syntaxes using additional options for computation and handling of special data types, specified by one or more Name,Value pair arguments.

For example, you can specify the number of principal components pca returns or an algorithm other than SVD to use.

[coeff,score,latent] = pca(___) also returns the principal component scores in score and the principal component variances in latent.

You can use any of the input arguments in the previous syntaxes.

Principal component scores are the representations of X in the principal component space. Rows of score correspond to observations, and columns correspond to components.

The principal component variances are the eigenvalues of the covariance matrix of X.

[coeff,score,latent,tsquared] = pca(___) also returns the Hotelling's T-squared statistic for each observation in X.

[coeff,score,latent,tsquared,explained,mu] = pca(___) also returns explained, the percentage of the total variance explained by each principal component and mu, the estimated mean of each variable in X.

coeff: X矩阵所对应的协方差矩阵的所有特征向量组成的矩阵,即变换矩阵或投影矩阵,coeff每列代表一个特征值所对应的特征向量,列的排列方式对应着特征值从大到小排序。

source: 表示原数据在各主成分向量上的投影。但注意:是原数据经过中心化后在主成分向量上的投影。

latent: 是一个列向量,主成分方差,也就是各特征向量对应的特征值,按照从大到小进行排列。

tsquared: X中每个观察值的Hotelling的T平方统计量。Hotelling的T平方统计量(T-Squared Statistic)是每个观察值的标准化分数的平方和,以列向量的形式返回。

explained: 由每个主成分解释的总方差的百分比,每一个主成分所贡献的比例。explained = 100*latent/sum(latent)。

mu: 每个变量X的估计平均值。

3. MATLAB程序

3.1 方法一:指定降维后低维空间的维度k

function [data_PCA, COEFF, sum_explained]=pca_demo_1(data,k)
% k:前k个主成分
data=zscore(data);  %归一化数据
[COEFF,SCORE,latent,tsquared,explained,mu]=pca(data);
latent1=100*latent/sum(latent);%将latent特征值总和统一为100,便于观察贡献率
data= bsxfun(@minus,data,mean(data,1));
data_PCA=data*COEFF(:,1:k);
pareto(latent1);%调用matla画图 pareto仅绘制累积分布的前95%,因此y中的部分元素并未显示
xlabel('Principal Component');
ylabel('Variance Explained (%)');
% 图中的线表示的累积变量解释程度
print(gcf,'-dpng','PCA.png');
sum_explained=sum(explained(1:k));

3.2 方法二:指定贡献率percent_threshold

function [data_PCA, COEFF, sum_explained, n]=pca_demo_2(data)
%用percent_threshold决定保留xx%的贡献率
percent_threshold=95;   %百分比阈值,用于决定保留的主成分个数;
data=zscore(data);  %归一化数据
[COEFF,SCORE,latent,tsquared,explained,mu]=pca(data);
latent1=100*latent/sum(latent);%将latent特征值总和统一为100,便于观察贡献率
A=length(latent1);
percents=0;                          %累积百分比
for n=1:A
    percents=percents+latent1(n);
    if percents>percent_threshold
        break;
    end
end
data= bsxfun(@minus,data,mean(data,1));
data_PCA=data*COEFF(:,1:n);
pareto(latent1);%调用matla画图 pareto仅绘制累积分布的前95%,因此y中的部分元素并未显示
xlabel('Principal Component');
ylabel('Variance Explained (%)');
% 图中的线表示的累积变量解释程度
print(gcf,'-dpng','PCA.png');
sum_explained=sum(explained(1:n));

4. 结果

数据来源于MATLAB自带的数据集hald

>> load hald
>> [data_PCA, COEFF, sum_explained]=pca_demo_1(ingredients,2)

data_PCA =

  -1.467237802258083  -1.903035708425560
  -2.135828746398875  -0.238353702721984
   1.129870473833422  -0.183877154192583
  -0.659895489750766  -1.576774209965747
   0.358764556470351  -0.483537878558994
   0.966639639692207  -0.169944028103651
   0.930705117077330   2.134816511997477
  -2.232137996884836   0.691670682875924
  -0.351515595975561   1.432245069443404
   1.662543014130206  -1.828096643220118
  -1.640179952926685   1.295112751426928
   1.692594091826333   0.392248821530480
   1.745678691164958   0.437525487914425


COEFF =

   0.475955172748970  -0.508979384806410   0.675500187964285   0.241052184051094
   0.563870242191994   0.413931487136985  -0.314420442819292   0.641756074427213
  -0.394066533909303   0.604969078471439   0.637691091806566   0.268466110294533
  -0.547931191260863  -0.451235109330016  -0.195420962611708   0.676734019481284


sum_explained =

  95.294252628439153

>> [data_PCA, COEFF, sum_explained, n]=pca_demo_2(ingredients)

data_PCA =

  -1.467237802258083  -1.903035708425560
  -2.135828746398875  -0.238353702721984
   1.129870473833422  -0.183877154192583
  -0.659895489750766  -1.576774209965747
   0.358764556470351  -0.483537878558994
   0.966639639692207  -0.169944028103651
   0.930705117077330   2.134816511997477
  -2.232137996884836   0.691670682875924
  -0.351515595975561   1.432245069443404
   1.662543014130206  -1.828096643220118
  -1.640179952926685   1.295112751426928
   1.692594091826333   0.392248821530480
   1.745678691164958   0.437525487914425


COEFF =

   0.475955172748970  -0.508979384806410   0.675500187964285   0.241052184051094
   0.563870242191994   0.413931487136985  -0.314420442819292   0.641756074427213
  -0.394066533909303   0.604969078471439   0.637691091806566   0.268466110294533
  -0.547931191260863  -0.451235109330016  -0.195420962611708   0.676734019481284


sum_explained =

  95.294252628439153


n =

     2

MATLAB实例:PCA(主成成分分析)详解第6张

5. 参考

[1] 周志华,《机器学习》.

[2] MATLAB实例:PCA降维

免责声明:文章转载自《MATLAB实例:PCA(主成成分分析)详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux的SOCKET编程详解HNOI2006——受欢迎的牛(强连通分量)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python 的音乐库

前言 其实处理这个用 Matlab 最方便,之前把 guitar-synthesizer 从 Matlab 移植到 Python,过程中更是体会到了这一点。不过 Matlab 安装包又大,启动又慢,还是收费软件。。平常也不怎么用得到,毕竟我也不搞算法、仿真。 所以还是,Python 真香hhh 库 音乐的时域处理 标准库有个 wave 模块,可以转成 w...

【转】几款网络仿真软件的比较

转自: 网络仿真技术是一种通过建立网络设备和网络链路的统计模型, 并模拟网络流量的传输, 从而获取网络设计或优化所需要的网络性能数据的仿真技术。由于仿真不是基于数学计算, 而是基于统计模型,因此,统计复用的随机性被精确地再现。网络仿真技术具有以下特点:一, 全新的模拟实验机理使其具有在高度复杂的网络环境下得到高可信度结果的特点。二, 网络仿真的预测功能是其...

【Matlab】线性调频信号LFM 仿真

【知识点】 生成序列 i = a:step:b 举例:i = 1:1:9 画图(子图) subplot(m,n,p)或者subplot(m n p) 总结起来就是,画一个m行n列的图。 p表示在第p个位置。 subplot是将多个图画到一个平面上的工具。其中,m表示是图排成m行,n表示图排成n列,也就是整个figure中有n个图是排成一行的,一共m行,如...

Matlab画图中图的方法

大家画图时可能会发现,有些地方太小了,看不清楚!但是这个小的部分,恰恰是我们需要强调的地方!怎么办?如何放大!其实matlab中的magnify函数可以解决我们这个问题。 第一步首先百度文库:magnify matlab源程序;找到对应的代码。   找到代码之后,我们将里面的源程序代码,复制到matlab新建的m文件。注意文件名为:magnif...

多分类问题中混淆矩阵(Confusion Matrix)的Matlab画法 | 丕子

在多分类问题中,有一种很实用的分类问题结果统计图。比如说多类别文类问题,那么每一个类别分到其他类别都有一些数据,但是分到自己类别的毕竟多,这样计算百分比之后就形成了一个矩阵,如果分类正确率高的话,那么对角线上的元素的值,也就是自己到自己的那一部分,value就大。我最近也在做多分类问题,要画这样的图,但是发现确实很少有代码,自己画的确实不好看,还牵扯到值的...

非线性方程(组):MATLAB内置函数 solve, vpasolve, fsolve, fzero, roots [MATLAB]

MATLAB函数 solve, vpasolve, fsolve, fzero, roots 功能和信息概览 求解函数 多项式型 非多项式型 一维 高维 符号 数值 算法 solve 支持,得到全部符号解 若可符号解则得到根 支持 支持 支持 当无符号解时  符号解方法:利用等式性质得到标准可解函数的方法 基本即模拟人工运算 vpasolv...