COM6004 Data Mining · Chapter 7
Dimensionality Reduction: PCA, SVD & Factor Analysis(非考試範圍)
COM6004 · Chapter 7 · Source: COM6004_Chapter_7_Dimensionality_Reduction.pptx (21 slides) · Tan, Steinbach, Karpatne, Kumar, Introduction to Data Mining 2nd ed.
Contents · 12 sections
COM6004 考試範圍只包括 Chapter 1–6,所以呢頁冇 Exam 標記,亦冇 Exam-only 按鈕。如果要準備考試,請先溫 Chapter 1 至 Chapter 6。呢章需要用到嘅 eigenvalue、eigenvector 同 orthogonal 等概念,可以喺 Matrix Algebra Review 溫返。
🎯 本講內容
- PCA:principal components 嘅定義同 problem formulation,點樣由 covariance matrix 嘅 eigen-decomposition 得出 PCs,PCs 有咩重要性質,同埋應該保留幾多個 PCs(例如 90% variability、scree plot elbow),幾時改用 correlation matrix 。
- SVD:、right / left singular vectors、點樣用 降維,同 PCA 嘅關係,同埋 sparse document-term matrix 嘅應用。
- Factor Analysis:latent factors、common factors、specific factors、factor loading ,同埋 decathlon 例子(speed 同 strength)。
🧭 三種方法嘅分別
| PCA | SVD | Factor Analysis | |
|---|---|---|---|
| 新 attributes 係咩 | Original attributes 嘅 linear combinations | Original attributes 嘅 linear combinations | 反過嚟:original variables 係 latent factors 嘅 linear combinations |
| 分解嘅對象 | Covariance matrix (或 correlation matrix ) | Data matrix 本身 | Standardized variables |
| 使唔使先減 mean | 要(centering) | 唔一定,適合 sparse data | 要 Z-score standardize |
| 新 data matrix | : |
1. 點解要做 Dimensionality Reduction(Slide 2–3)
Chapter 2 講 data preprocessing 時已經提過 curse of dimensionality:attributes 越多,data 喺 space 入面就越 sparse,distance 同 density 嘅意義會變弱,計算量亦都會增加。呢章介紹三種方法,用少量新 attributes 代替原本好多個 attributes:
- Principal Components Analysis (PCA)
- Singular Value Decomposition (SVD)
- Factor Analysis (FA)
Chapter 2 將 dimensionality reduction 同 feature subset selection 分開講。Feature selection 係揀返部分原有 attributes;PCA 同 SVD 就係造一組新 attributes,每個新 attribute 都係全部原有 attributes 嘅 linear combination。
2. PCA 嘅目標同 Problem Formulation(Slide 3–4)
Slide 嘅定義:
The goal of PCA is to find a new set of attributes [called principal components (PCs), which are linear combinations of the original attributes] that better captures the variability of the data.
PCA 有三個好處(Appealing characteristics):
- 佢傾向搵出 data 入面最強嘅 patterns(strongest patterns)。
- 好多時,一小部分(small fraction)新 attributes 已經可以捕捉到 data 大部分 variability。
- 假設 noise 比 patterns 弱,捨棄最後嗰幾個 PCs 就可以去走大部分 noise(eliminate much of the noise)。
Problem formulation(Slide 4):
- 任何兩個唔同嘅 PCs 之間 covariance 都係 0。
- 第一個 PC 要盡量捕捉最多 variability。
- 第二個 PC 要同第一個 PC orthogonal,喺呢個 constraint 之下再捕捉最多剩餘嘅 variability。
- 第 個 PC 要同之前 個 PCs orthogonal,喺呢個 constraint 之下再捕捉最多剩餘嘅 variability。
可以想像一團橢圓形嘅點雲。PC1 就係橢圓最長嗰條軸,即係 data 散得最開嘅方向。PC2 同 PC1 成 90°,喺垂直方向入面搵最長嘅一條軸。PCA 其實只係將座標軸旋轉,令新座標軸對準 data 最分散嘅方向。旋轉之後,最後幾條軸方向上 data 好少變化,所以可以捨棄佢哋。
3. Mathematical Details:Eigen-decomposition of (Slide 5–6)
Data matrix 係一個 matrix, 係 observations 數目, 係 attributes 數目。Slide 嘅原句係:
PCA is ALL about eigen-decomposition of the sample covariance matrix of the data.
如果 已經 preprocess 過,令每個 attribute 嘅 mean 都係 0(centering),咁:
跟住對 做 eigen-decomposition(Slide 6):
- 嘅 eigenvalues 係 ,由大至細排列:。
- 對應嘅 eigenvectors 係 。 叫做第 個 eigenpair。
- 第一個 PC 係 ,第二個 PC 係 ,如此類推。
每個 eigenvector 係一個 嘅 weight vector。佢話你知原有 個 attributes 每個佔幾多比重,所以 就係「原有 attributes 嘅 linear combination」,符合 Slide 3 嘅定義。
Slide 5 有一張圖,但冇 text 抽到出嚟,所以呢度冇解釋圖入面嘅內容。上面嘅公式係由 slide 嘅 equation object 抽出嚟。
- 只喺 centered data 先成立。未減 mean 嘅話, 就唔係 covariance matrix。
- 嘅 size 係 (attributes × attributes),唔係 。
- Eigenvalues 一定要由大至細排好先對應 PC1、PC2……。好多 library(例如
numpy.linalg.eigh)係由細至大輸出。
4. PC 嘅重要性質(Slide 7)
- 任何兩個唔同嘅 PCs,covariance 都係 0(uncorrelated)。
- 第 個 PC 嘅 variance 等於 。
- 原有 attributes 嘅 variance 總和等於 ,亦等於全部 PCs 嘅 variance 總和。所以 data 嘅 total variability 冇損失。
- 最大 eigenvalue 對應嘅 eigenvector 指向 data variance 最大嘅方向(variance )。第二大 eigenvalue 對應嘅 eigenvector 同第一個 orthogonal,指向剩餘 variance 最大嘅方向(variance ),如此類推。
第 3 點其實就係 supplement 入面嘅 ,因為 嘅 diagonal 就係每個 attribute 嘅 variance。第 1 點嚟自 symmetric matrix 嘅 eigenvectors 互相 orthogonal(),所以 。
Worked example(自製,用嚟理解)
以下係一個已經 centered 嘅細 dataset,,:
兩個 column 嘅 sum 都係 0,所以確實係 centered。Eigenvalues 由 求:,所以 ,。
對應嘅 unit eigenvectors 係 同 。
- PC1 ,佢嘅 variance 係 ✔
- Total variance ✔,total variability 冇損失。
- PC1 解釋嘅比例 。如果準則係「保留 90% variability」,用 1 個 PC 就夠,即係由 2D 降到 1D。
5. 2D 例子(Slide 8)
Slide 用 1000 個 two-dimensional data points,展示 PCA transformation 之前同之後嘅樣。
Slide 8 嘅 before / after 散點圖係圖片,冇抽到 text,所以呢度唔估圖入面畫咗啲咩。一般嚟講,PCA 之後嘅座標軸就係 PC1 同 PC2,可以參考第 2 節嘅「直覺」同第 4 節嘅 worked example。
6. 揀幾多個 PCs?Covariance 定 Correlation?(Slide 9)
原本有 個 attributes,PCA 就可以產生 個 PCs。做 data mining 時,通常只用頭幾個 PCs。至於要用幾多個,Slide 嘅答案係:It depends on your application. 一般有兩種方法:
- Cumulative proportion of variance:保留足夠嘅 PCs,令佢哋捕捉到大部分 variability(例如 90%):
- Scree plot:x 軸係 PC 編號,y 軸係 (或者 proportion of variance),然後揀曲線出現 elbow(由急跌轉為平)嘅位置。Slide 註明詳情見 Tutorial 8。
例如 ,total 係 8.0。Cumulative proportion 依次係 50%、81.25%、91.25%、96.25%、100%,所以用 90% 準則就揀 。Scree plot 喺 之後變平,elbow 大約喺第 3 個 PC,兩種方法嘅答案一致。
Correlation matrix :如果 attributes 嘅 scale 同 variance 相差好遠,有時會用 代替 做 PCA。原因係 PCA 追求最大 variance,如果唔處理,scale 最大嗰個 attribute(例如以「元」計嘅收入,對比以「年」計嘅年齡)會霸佔 PC1。用 做 PCA,等同先將每個 attribute 做 Z-score standardize,再用 做 PCA。
COM6001 Week 8 Feature Scaling 教嘅 StandardScaler,就係做 PCA 之前要準備嘅 standardization。所以 PCA 前面通常會先做 scaling。
# 參考用 scikit-learn 示範(唔係 lecture code)
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
Z = StandardScaler().fit_transform(X) # 先 standardize,即係用 R 做 PCA
pca = PCA(n_components=0.90) # 保留 90% variability
X_new = pca.fit_transform(Z) # n × k
print(pca.explained_variance_) # λ_1, λ_2, …
print(pca.explained_variance_ratio_.cumsum())
print(pca.components_) # 每一行係一個 eigenvector v_i
7. Singular Value Decomposition(Slide 10–12)
Slide 10 指出,只要每個 variable 嘅 mean 已經減咗,PCA 就同對 data matrix 做 SVD 係等價嘅。不過,從 SVD 角度理解 dimensionality reduction 都好有用,因為有時唔適合減 mean,特別係 data 比較 sparse 嘅時候。
SVD 直接分解 嘅 data matrix (Slide 11):
- :singular values,由大至細排列,放喺 diagonal matrix 。
- 嘅 columns 叫 right singular vectors。
- 嘅 columns 叫 left singular vectors。
要得到一個只有 個 attributes 嘅新 data matrix(Slide 12),就用頭 個 right singular vectors:
每一項 都係一個 rank-1 matrix。 係呢啲「層」疊加出嚟嘅結果,而 越大,嗰一層越重要。只保留頭 層,就係保留最主要嘅結構。
如果 已經 centered:。所以:
- Right singular vectors 就係 嘅 eigenvectors,即係 PCA 嘅方向。
- 。
- 正正就係頭 個 PCs。
用返第 4 節嘅例子:,。
Slide 11 有一張圖(可能係 、、 嘅 dimension 示意圖),冇抽到 text,所以呢度唔估圖入面嘅內容。
8. SVD 嘅應用:Document Data(Slide 13)
- Document data 一般都係 sparse 嘅。
- Data matrix 係 document-term matrix:每一行係一份 document,每一列係一個 term(word)。
- 對 document-term matrix 做 SVD,可以睇到某類 documents 嘅「common terms」。
點解唔先減 mean?因為減 mean 之後,原本大量嘅 0 會變成非 0,matrix 就唔再 sparse,儲存同計算成本都會大增。所以 sparse data 會直接做 SVD,呢個亦係 Slide 10 講嘅原因。呢個方法喺 text mining 叫做 Latent Semantic Analysis (LSA),scikit-learn 嘅實作係 TruncatedSVD。
Chapter 2 嘅 document data 同 cosine similarity 都用 document-term matrix;Chapter 6 嘅 market basket data 都係 sparse 嘅 asymmetric binary data。
Slide 13 有一張 document-term matrix 或者 SVD 結果嘅圖,冇抽到 text,所以呢度唔估圖入面有咩 terms 或者 categories。
9. Factor Analysis:概念同 Motivation(Slide 14–17)
同 PCA 嘅根本分別(Slide 14):
- PCA / SVD:新 attributes 係原有 variables 嘅 linear combinations,即係 新 = f(舊)。
- FA:將原有 variables 表示成少量 hidden / latent attributes 嘅 linear combinations,即係 舊 = f(隱藏因素)。
Motivation 1(Slide 15):有啲 data objects 嘅特徵好難直接量度(hard to measure directly),但似乎同一啲可以量度嘅特徵有關,例如:
- Intelligence 同各類 IQ tests 嘅表現。
- 運動員喺唔同項目嘅表現,同佢嘅 speed 同 strength。
如果可以搵到少量 attributes 去歸納原有 attributes,就可以同時做到 dimensionality reduction,又可以加深對 data 嘅理解。
Motivation 2(Slide 16):亦可以從 covariance 或 correlation matrix 嘅角度理解。假設有一組 attributes 同其他 attributes 嘅 correlation 唔高,但組內嘅 attributes 之間 correlation 好強,可能係因為佢哋量度緊同一個 underlying quantity(latent variable)。咁樣就應該搵一個 underlying attribute 去總結每一組。
Decathlon 例子(Slide 17):Decathlon(十項全能)有 10 個項目。著重 speed 嘅項目(100m、200m、400m……)之間,運動員嘅表現差唔多;著重 strength 嘅項目亦一樣。所以可以假設,運動員喺任何一個項目嘅表現,都係由項目本身嘅性質,加上 speed 同 strength 兩個 underlying factors 決定。Factor Analysis 就係要搵出呢種關係。
假設 10 個項目嘅 correlation matrix 大致分成兩大塊:跑步類項目之間 correlation 高,投擲類項目之間 correlation 高,但兩類之間 correlation 低。FA 就係將 10 個 observable variables 濃縮成 2 個 latent factors,而且每個 factor 都有清楚嘅意思(speed、strength)。呢種 interpretability 係 FA 嘅主要賣點。
Slide 14 有一張圖,冇抽到 text,所以呢度唔估圖入面嘅內容。
10. Factor Analysis 嘅 Mathematical Formulation(Slide 18–20)
設 係 個 standardized(Z-score normalized)observable variables:
設 係 個 latent / hidden factors。佢哋就係新 attributes,每個 object 都有自己嘅值。Slide 18 嘅 -factor analysis model 係以圖片顯示。根據 Slide 19 嘅符號(、)同教科書,model 嘅形式係:
Slide 18 嘅 model 公式係圖片,冇抽到 text。上面係按 Slide 19 嘅符號同 Tan et al. 教科書寫出嚟嘅標準形式,請對照 slide 原圖確認符號。注意 喺 slide 寫成 (冇下標),呢度寫成 ,意思係每個 variable 有自己嘅 mean。
名詞解釋(Slide 19):
| 名詞 | 意思 |
|---|---|
| Common factors | 每個原有 attribute 嘅值都由全部 latent factors 共同決定,所以叫 common factors。 |
| Factor loading | 第 個 factor 對第 個 attribute 嘅重要性。 |
| Specific / unique factor | Error term,代表第 個 attribute 入面 common factors 解釋唔到嘅部分。 |
Slide 20:FA 會 estimate factor loadings,然後就可以 estimate 每個 observation 喺 latent factors 上嘅值。如果原本 data matrix 係 ,新 data matrix 就係 。
PCA 入面嘅 係 covariance matrix 嘅 eigenvalue(PC 嘅 variance)。FA 入面嘅 係 factor loading(有兩個下標)。兩者符號一樣,但意思完全唔同。
Decathlon 例子:,。睇 loadings 就知道每個 factor 代表咩意思(呢啲數字只係示意,唔係 slide 數據)。
FA model 好似一組 linear regression: 係 response, 係 predictors, 係 coefficients, 係 error。最大分別係 FA 嘅 predictors 睇唔到,要同 loadings 一齊 estimate。
自我測驗 Self-test
Q1. 用一句說話講 PCA 嘅目標,同埋列出佢三個 appealing characteristics。
搵一組新 attributes(principal components),佢哋係原有 attributes 嘅 linear combinations,而且更能捕捉 data 嘅 variability。三個 characteristics:(1) 傾向搵出 strongest patterns;(2) 一小部分新 attributes 已經可以捕捉大部分 variability;(3) 因為 noise 比 patterns 弱,可以去走大部分 noise。
Q2. 第二個 PC 要滿足咩條件?
同第一個 PC orthogonal(covariance 係 0),喺呢個 constraint 之下捕捉最多剩餘 variability。
Q3. Centered data 嘅 係 。 嘅 size 係幾多?公式係咩?可以產生幾多個 PCs?
,size 係 。可以產生 個 PCs。
Q4. 嘅 eigenvalues 係 。Total variance 係幾多?PC2 嘅 variance 係幾多?要保留 90% variability 需要幾多個 PCs?
Total variance 。PC2 嘅 variance 。Cumulative proportion 依次係 50%、80%、95%,所以需要 。
Q5. 幾時應該用 correlation matrix 代替 做 PCA?點解?
當 attributes 嘅 scale 同 variance 相差好遠時。PCA 追求最大 variance,scale 大嘅 attribute 會霸佔頭幾個 PCs。用 等同先將每個 attribute standardize,令每個 attribute 都有平等地位。
Q6. 入面,邊啲係 right singular vectors?點樣得到 維嘅新 data?
嘅 columns 係 right singular vectors, 嘅 columns 係 left singular vectors。新 data 係 ,size 係 。
Q7. PCA 同 SVD 幾時等價?點解 document-term matrix 偏向直接用 SVD?
當每個 variable 嘅 mean 已經減咗,兩者就等價( 相同,)。Document-term matrix 好 sparse,減 mean 之後大量 0 會變成非 0,破壞 sparsity,所以直接對 做 SVD 會比較好。
Q8. PCA 同 Factor Analysis 喺「linear combination」方向上有咩分別?
PCA / SVD:新 attributes 係原有 variables 嘅 linear combinations。FA:原有 variables 表示成少量 latent factors 嘅 linear combinations(),方向啱啱相反。
Q9. 喺 FA model 入面,、 同 分別叫咩,代表咩?
:factor loading,第 個 factor 對第 個 attribute 嘅重要性。:common factor,即係 latent attribute。:specific / unique factor,代表第 個 attribute 入面 common factors 解釋唔到嘅部分。
Q10. 用 decathlon 解釋 FA 嘅 motivation。
10 個項目嘅表現睇得到,但 speed 同 strength 睇唔到。著重 speed 嘅項目之間表現相近,著重 strength 嘅項目亦一樣。所以可以假設每個項目嘅表現由項目性質加兩個 latent factors(speed、strength)決定。FA 可以將 10 個 variables 減到 2 個 factors,同時令人更了解 data。
一頁總結 Cheat sheet
- PCA:PCs 係原有 attributes 嘅 linear combinations。PC1 捕捉最多 variance;PC 同之前 個 PCs orthogonal,再捕捉最多剩餘 variance。
- Centered ():()。Eigenpairs ,;。
- 性質:PCs 兩兩 uncorrelated;;,total variability 冇損失。
- 揀 :cumulative ,或者 scree plot 嘅 elbow。Scale 差好遠就用 correlation matrix 。
- SVD:,。 係 right singular vectors, 係 left singular vectors。。
- Centered 嘅話,PCA 同 SVD 等價,。Sparse data(document-term matrix)唔減 mean,直接做 SVD。
- FA:原有 standardized variables 係少量 latent factors 嘅 linear combinations:。 係 common factors, 係 factor loading, 係 specific factor。()會變成 ()。
- FA 例子:IQ tests 同 intelligence;decathlon 同 speed、strength。