matlab 自帶函數 pca 的用法


用於學習記錄:

matlab函數的pca函數的輸入參數除了數據集X還有10個  

數據集X(每行為一個樣本,行數為樣本數)
- coeff = pca(X)
- coeff = pca(X,Name,Value)
- [coeff,score,latent] = pca(___)
- [coeff,score,latent,tsquared] = pca(___)
- [coeff,score,latent,tsquared,explained,mu] = pca(___)
i.e 

Input Argument 0
X :--數據集 假設n個樣本, 每個樣本p維,則 X是n-by-p的matrix

Input Argument 1
'Algorithm' — Principal component algorithm
'svd' (default) | 'eig' | 'als'
解釋:PCA 涉及到求協方差矩陣的特征向量, 在matlab 有3中算法
默認 :SVD,
eig (Eigenvalue decomposition )算法, 此算法當n(number of examples) > p (features) 時,速度快於SVD,但是計算的結果沒有SVD精確
als( Alternating least squares )算法,此算法為了處理數據集X中有少許缺失數據時的情況(i.e 0), 但是對於X為稀疏數據集(缺失數據過多)時,不好用

Input Argument 2
'Centered' — Indicator for centering columns
true (default) | false
解釋:選擇是否對數據進行中心化, 也是數據的特征是否進行零均值化(i.e.按列減去均值, 為了得到covariance            
        matrix), 如果選擇了true,  則可用score*coeff'恢復中心化后的X, 若選擇了false,則可用score*coeff'
        恢復原始的X

默認:true(中心化)

Input Argument 3
'Economy' — Indicator for economy size output
true (default) | false
解釋: 有時候輸出的coeff(映射矩陣p-by-p)過大, 而且是沒有必要的(因為我們要降維),所以可以只輸出coeff(以及score,latent)的前d列,
d是數據集的自由度,數據集沒NAN的時候d=n-1; 具體的解釋見matlab.總之如果將看見完整的PCA結果,可以設置為false.
默認:true ,(默認ture以后對於初次使用matlab這個函數的人非常迷惑).

Input Argument 4
'NumComponents' — Number of components requested
number of variables (default) | scalar integer
解釋:輸出指定的components 也就是更為靈活的Economy,但是經過試驗發現指定成分數 僅在小於d(自由度)時有效,大於d時無效;
默認: number of variables ( i.e p,特征數目)

Input Argument 5
'Rows' — Action to take for NaN values
'complete' (default) | 'pairwise' | 'all'
解釋: 此選項是為了智能處理數據集X中含有NAN的情況,
          complete: 計算之前.移除X中含有NAN的行(i.e 樣本),計算完成后,含有NAN的行被重新插入到
                     score and tsquared相應的位置中(coeff呢?)
  pairwise :      首先這個選項必須配合 'Argorithm'中 'eig'進行使用.如果沒有指定'eig'(默認svd),
                   當指定pairwise時,則會自動切換為eig; 指定為svd,則會發送warning message,
                   然后自動切換為eig;若指定為als, 則會發送warning message然后忽略 'Rows'此選項.
                        成功使用此選項時,若計算協方差(i,j)處值時遇到NAN,則使用X中第i或j列中不含NAN
                   的行此處來計算的協方差值.
    all : 當確定X中無缺失數據,使用'all',則pca會使用X中所有的數據,當遇到NAN時則會自動終止.
默認:complete

Input Argument 6
'Weights' — Observation weights
ones (default) | row vector
解釋: 基於observations(i.e 樣本)的權重pca,有需求的可以自己查查

Input Argument 7
'VariableWeights' — Variable weights
row vector | 'variance'
解釋:基於variables(i.e.features)的權重pca,有需求的自己查
默認: 無默認值, 也就是默認不使用此選項

Input Argument 8
'Coeff0' — Initial value for coefficients
matrix of random values (default) | p-by-k matrix
解釋: Initial value for the coefficient matrix coeff, 不是太看的懂,但是要配合'Algorithm'中的
         'als'使用
默認:   p-by-random

Input Argument 9
'Score0' — Initial value for scores
matrix of random values (default) | k-by-m matrix
解釋: Initial value for scores matri score.不是太看的懂,但是要配合'Algorithm'中的 'als'使用
默認 : n-by-random

Input Argument 10
'Options' — Options for iterations
structure(此用法是個結構體)
解釋:  用於迭代的選項,僅配合'Algorithm'中的'als'使用. 因為'als'是使用迭代的方法進行計算的
          對這個不感興趣, 有興趣的可以去help一下
          附上help中的使用方法 opt = statset('pca'); opt.MaxIter = 2000; coeff =pca(X,'Options',opt);

Output Argument 1
coeff : 主成分系數 應該就是協方差矩陣的特征向量矩陣(也就是映射矩陣).
           完整輸出的情況下是一個p-by-p 的matrix.每一列都是一個特征向量.按對應的特征值
           的大小,從大到小進行排列.
Output Argument 2
score: 進行旋轉(也就是利用映射矩陣coeff進行)后的結果i.e. score = X * coeff.  n-by-p matrix
          這里要注意: 如果你使用pca時使用的是'Centered'設置'false', 拿X *coeff 和score對比的時候, 

   記得把X中心化后再乘以coeff,之后再和score對比....; 同樣如果pca使用的是默認值'true',

          恢復的X = score * coeff' (注意轉置)是中心化后的數據

Output Argument 3
latent: 主成分方差 也就是各特征向量對應的特征值,從大到小進行排列

Output Argument 4
tsquared :層次不夠 無法解釋......

Output Argument 5
explained : 每一個主成分所貢獻的比例,可以更直觀的選擇所需要降維的維數了,不用再用特征值去求了

Output Argument 6
mu: X 按列的均值,當前僅當 'Centered'置於'true'(默認值)時才會返回此變量
 


免責聲明!

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



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