pca主成份分析方法


1.應用pca的前提

  應用pca的前提是,連續信號具有相關性。相關性是什么,是冗余。就是要利用pca去除冗余。

2.pca的定義

  pca是一種去除隨機變量間相關性的線性變換。是一種常用的多元數據分析方法。pca將互相關的輸入數據轉換成統計上不相干的主成分(或者特征),所得到的主成份通常是按照方差大小進行降序排列的。

reference :基於CCA的fMRI時空模型數據處理方法的研究,肖柯,碩士論文。

———————————————————下面來參考一下代碼—————————————————————————————————---

參考:http://blog.csdn.net/watkinsong/article/details/8234766 

我在網上看了很多pca降維的例子,都大同小異,原理差不多,都是活的原來矩陣的協方差矩陣,然后計算協方差矩陣的特征值和特征向量,最后通過特征向量的根據特征值由大到小的排序進行KL變換神馬的獲得一個轉換矩陣。
 

1. matlab自帶的實現方式

 
 PCA在matlab中的實現舉例
 
  以下資料來自matlab的help,翻譯和注解部分由筆者添加:(重點部分添加了翻譯!)
 
   princomp-----函數名稱
 
  Principal component analysis (PCA) on data
 
  Syntax------函數調用語法
 
  [COEFF,SCORE] = princomp(X)
 
  [COEFF,SCORE,latent] = princomp(X)
 
  [COEFF,SCORE,latent,tsquare] = princomp(X)
 
  [...] = princomp(X,'econ')
 
   Description -----函數描述
 
   COEFF = princomp(X)  performs principal components analysis (PCA) on the n-by-p data matrix X, and returns the principal component coefficients, also known as loadings. Rows of X correspond to observations, columns to variables. COEFF is a p-by-p matrix, each column containing coefficients for one principal component. The columns are in order of decreasing component variance.
 
  在n行p列的數據集X上做主成分分析。返回主成分系數。X的每行表示一個樣本的觀測值,每一列表示特征變量。COEFF是一個p行p列的矩陣,每一列包含一個主成分的系數,列是按主成分變量遞減順序排列。(按照這個翻譯很難理解,其實COEFF是X矩陣所對應的協方差陣V的所有特征向量組成的矩陣,即變換矩陣或稱投影矩陣,COEFF每列對應一個特征值的特征向量,列的排列順序是按特征值的大小遞減排序,后面有具體例子解釋,見 說明1 )
 
  princomp centers X by subtracting off column means, but does not rescale the columns of X. To perform principal components analysis with standardized variables, that is, based on correlations, use princomp(zscore(X)). To perform principal components analysis directly on a covariance or correlation matrix, use pcacov.
 
  計算PCA的時候,MATLAB自動對列進行了去均值的操作,但是並不對數據進行規格化,如果要規格化的話,用princomp(zscore(X))。另外,如果直接有現成的協方差陣,用函數pcacov來計算。
 
   [COEFF,SCORE] = princomp(X)  returns SCORE, the principal component scores; that is, the representation of X in the principal component space. Rows of SCORE correspond to observations, columns to components.
 
  返回的SCORE是對主分的打分,也就是說原X矩陣在主成分空間的表示。SCORE每行對應樣本觀測值,每列對應一個主成份(變量),它的行和列的數目和X的行列數目相同。
 
   [COEFF,SCORE,latent] = princomp(X)  returns latent, a vector containing the eigenvalues of the covariance matrix of X.
 
  返回的latent是一個向量,它是X所對應的協方差矩陣的特征值向量。
 
   [COEFF,SCORE,latent,tsquare] = princomp(X)  returns tsquare, which contains Hotelling's T2 statistic for each data point.
 
  返回的tsquare,是表示對每個樣本點Hotelling的T方統計量(我也不很清楚是什么東東)。
 
  The scores are the data formed by transforming the original data into the space of the principal components. The values of the vector latent are the variance of the columns of SCORE. Hotelling's T2 is a measure of the multivariate distance of each observation from the center of the data set.
 
  所得的分(scores)表示由原數據X轉變到主成分空間所得到的數據。latent向量的值表示SCORE矩陣每列的方差(見 說明2 )。Hotelling的T方是用來衡量多變量間的距離,這個距離是指樣本觀測值到數據集中心的距離。
 
  When n <= p, SCORE(:,n:p) and latent(n:p) are necessarily zero, and the columns of COEFF(:,n:p) define directions that are orthogonal to X.
 
   [...] = princomp(X,'econ')  returns only the elements of latent that are not necessarily zero, and the corresponding columns of COEFF and SCORE, that is, when n <= p, only the first n-1. This can be significantly faster when p is much larger than n.
 
  當維數p超過樣本個數n的時候,用[...] = princomp(X,'econ')來計算,這樣會顯著提高計算速度
 
   Examples--舉例
 
  (上面說了那么多廢話,看了還不一定懂,還不如舉例容易理解,下面樣本數據集為ingredients,這個數據集是matlab自帶的)
 
  Compute principal components for the ingredients data in the Hald data set, and the variance accounted for by each component.
 
  load hald; %載入matlab內部數據
 
  [pc,score,latent,tsquare] = princomp(ingredients); %調用pca分析函數
 
  ingredients,score,pc,latent,tsquare %顯示得到的結果
 
  ingredients =
 
  7 26 6 60
 
  1 29 15 52
 
  11 56 8 20
 
  11 31 8 47
 
  7 52 6 33
 
  11 55 9 22
 
  3 71 17 6
 
  1 31 22 44
 
  2 54 18 22
 
  21 47 4 26
 
  1 40 23 34
 
  11 66 9 12
 
  10 68 8 12
 
  score =
 
  36.8218 -6.8709 -4.5909 0.3967
 
  29.6073 4.6109 -2.2476 -0.3958
 
  -12.9818 -4.2049 0.9022 -1.1261
 
  23.7147 -6.6341 1.8547 -0.3786
 
  -0.5532 -4.4617 -6.0874 0.1424
 
  -10.8125 -3.6466 0.9130 -0.1350
 
  -32.5882 8.9798 -1.6063 0.0818
 
  22.6064 10.7259 3.2365 0.3243
 
  -9.2626 8.9854 -0.0169 -0.5437
 
  -3.2840 -14.1573 7.0465 0.3405
 
  9.2200 12.3861 3.4283 0.4352
 
  -25.5849 -2.7817 -0.3867 0.4468
 
  -26.9032 -2.9310 -2.4455 0.4116
 
  pc =
 
  -0.0678 -0.6460 0.5673 0.5062
 
  -0.6785 -0.0200 -0.5440 0.4933
 
  0.0290 0.7553 0.4036 0.5156
 
  0.7309 -0.1085 -0.4684 0.4844
 
  latent =
 
  517.7969
 
  67.4964
 
  12.4054
 
  0.2372
 
  tsquare =
 
  5.6803
 
  3.0758
 
  6.0002
 
  2.6198
 
  3.3681
 
  0.5668
 
  3.4818
 
  3.9794
 
  2.6086
 
  7.4818
 
  4.1830
 
  2.2327
 
  2.7216
 
  %下面我們來做一個驗證
 
  %下面為計算ingredients協方差矩陣:
 
  cov_ingredients=cov(ingredients)
 
  cov_ingredients =
 
  34.6026 20.9231 -31.0513 -24.1667
 
  20.9231 242.1410 -13.8782 -253.4167
 
  -31.0513 -13.8782 41.0256 3.1667
 
  -24.1667 -253.4167 3.1667 280.1667
 
  %下面為計算ingredients所對應的協方差矩陣(也就是cov_ingredients矩陣)的特征值和特征
 
  %向量,下面的矩陣V為特征向量,D為特征值(對比上面的latent)組成的對角線矩陣
 
  [V,D] = eig(cov_ingredients)
 
  V =
 
  0.5062 0.5673 0.6460 -0.0678
 
  0.4933 -0.5440 0.0200 -0.6785
 
  0.5156 0.4036 -0.7553 0.0290
 
  0.4844 -0.4684 0.1085 0.7309
 
  D =
 
  0.2372 0 0 0
 
  0 12.4054 0 0
 
  0 0 67.4964 0
 
  0 0 0 517.7969
 
  % 說明1 :對比一下矩陣V和矩陣pc,現在很容易明白為什么COEFF是按列遞減順序排列的
 
  % 了!(V中第三列與pc中倒數第三列差個負號,學過線性代數的人都知道這沒問題)
 
  %下面再驗證一下說明2
 
  diag(cov(score))
 
  ans =
 
  517.7969
 
  67.4964
 
  12.4054
 
  0.2372
 
  % 說明2 :以上結果顯示latent確實表示SCORE矩陣每列的方差,517.7969表示第一列方差
 
  下面做圖表示結果:
 
  上面說了半天還沒有達到我們終極想要的,其實我們要的是由函數[pc,score,latent,tsquare] = princomp(ingredients)它所產生的pc和latent。由latent可以算出降維后的空間所能表示原空間的程度,只要這個累積的值大於95%就行了。
 
  The following command and plot show that two components account for 98% of the variance:
 
  cumsum(latent)./sum(latent)
 
  ans =
 
  0.86597
 
  0.97886
 
  0.9996
 
  1
 
  %由以上ans值可以看出前兩個主成分就能表示原空間的97.886%,所以取pc中的前兩列可
 
  %做主成分變換矩陣tranMatrix = pc(:,1:2)。則從原來的4維空間降到2維空間。對任意一個
 
  %原空間樣本,例如a=(7 ,26 ,6 ,60)變到低維空間的表達式為a1 = a*tranMatrix。(當然你也可
 
  %以取pc中的前三列,由原來的4維空間變到3維空間)
 
  biplot(pc(:,1:2),'Scores',score(:,1:2),'VarLabels',...
 
  {'X1' 'X2' 'X3' 'X4'})

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM