f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
179 lines
5.9 KiB
Markdown
179 lines
5.9 KiB
Markdown
---
|
|
id: wiki-2026-0508-linear-discriminant-analysis
|
|
title: Linear Discriminant Analysis
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [LDA, Fisher Discriminant, Fisher LDA]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [machine-learning, classification, dimensionality-reduction, supervised, sklearn]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: python
|
|
framework: scikit-learn
|
|
---
|
|
|
|
# Linear Discriminant Analysis
|
|
|
|
## 매 한 줄
|
|
> **"매 LDA = 클래스 간 분산은 최대, 클래스 내 분산은 최소가 되는 축을 찾는 supervised dim reduction"**. Fisher (1936)가 제안한 방법으로 PCA가 variance-maximizing이라면 LDA는 separability-maximizing. 동시에 Gaussian + 공분산 동일 가정 하에서 optimal Bayes classifier가 된다.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 핵심 수식
|
|
- $S_W = \sum_c \sum_{x \in c}(x - \mu_c)(x - \mu_c)^T$ — within-class scatter.
|
|
- $S_B = \sum_c n_c (\mu_c - \mu)(\mu_c - \mu)^T$ — between-class scatter.
|
|
- 목적: $\arg\max_W \frac{|W^T S_B W|}{|W^T S_W W|}$.
|
|
- 해: $S_W^{-1} S_B$의 top eigenvector — 최대 (C-1)개 (C: class 수).
|
|
|
|
### 매 LDA vs PCA
|
|
| 항목 | PCA | LDA |
|
|
|---|---|---|
|
|
| 지도/비지도 | 비지도 | 지도 |
|
|
| 목적 | variance 최대 | class separability 최대 |
|
|
| 출력 차원 한계 | min(n, d) | C-1 |
|
|
| 가정 | 없음 (centered) | Gaussian, 공분산 동일 |
|
|
|
|
### 매 분류기로서의 LDA
|
|
- 각 class가 같은 공분산 Σ를 가진 Gaussian이라 가정.
|
|
- Decision boundary가 linear.
|
|
- QDA (Quadratic): 공분산이 class별로 다름 → quadratic boundary.
|
|
|
|
### 매 한계
|
|
- 클래스 분포가 비-Gaussian이면 약함.
|
|
- Class 불균형 시 majority에 끌림.
|
|
- 비선형 boundary 못함 → Kernel LDA / NDA.
|
|
- C-1 차원 제약 → 2-class면 1차원만.
|
|
|
|
### 매 응용
|
|
1. Face recognition (Fisherfaces).
|
|
2. 의료 진단 (small classes, Gaussian-ish).
|
|
3. Document classification (TF-IDF 후).
|
|
4. EEG/생체신호 분류.
|
|
5. PCA 후 분류 직전 supervision으로 dim 줄이기.
|
|
|
|
## 💻 패턴
|
|
|
|
### sklearn — 기본 분류
|
|
```python
|
|
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
|
from sklearn.model_selection import cross_val_score
|
|
|
|
lda = LinearDiscriminantAnalysis()
|
|
print(cross_val_score(lda, X, y, cv=5).mean())
|
|
lda.fit(X_tr, y_tr)
|
|
print("priors:", lda.priors_, "means shape:", lda.means_.shape)
|
|
```
|
|
|
|
### LDA를 dim reduction으로 사용
|
|
```python
|
|
lda = LinearDiscriminantAnalysis(n_components=2)
|
|
X_2d = lda.fit_transform(X, y) # (n, 2) — class별로 시각화
|
|
import matplotlib.pyplot as plt
|
|
for c in np.unique(y):
|
|
plt.scatter(X_2d[y==c, 0], X_2d[y==c, 1], label=str(c), alpha=.6)
|
|
plt.legend(); plt.show()
|
|
```
|
|
|
|
### LDA + Logistic 비교
|
|
```python
|
|
from sklearn.linear_model import LogisticRegression
|
|
from sklearn.pipeline import Pipeline
|
|
from sklearn.preprocessing import StandardScaler
|
|
|
|
models = {
|
|
"lda": LinearDiscriminantAnalysis(),
|
|
"logreg": Pipeline([("sc", StandardScaler()), ("lr", LogisticRegression(max_iter=2000))]),
|
|
}
|
|
for name, m in models.items():
|
|
print(name, cross_val_score(m, X, y, cv=5).mean())
|
|
# Gaussian/공분산 동일에 가까우면 LDA가 logreg보다 안정적
|
|
```
|
|
|
|
### Shrinkage LDA — 고차원/소표본
|
|
```python
|
|
# d >> n 일 때 S_W가 singular → shrinkage 사용
|
|
lda = LinearDiscriminantAnalysis(solver="lsqr", shrinkage="auto") # Ledoit-Wolf
|
|
lda.fit(X, y)
|
|
```
|
|
|
|
### QDA — 공분산 다를 때
|
|
```python
|
|
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
|
|
qda = QuadraticDiscriminantAnalysis(reg_param=0.01).fit(X_tr, y_tr)
|
|
print(qda.score(X_te, y_te))
|
|
```
|
|
|
|
### From scratch — eigenvalue 풀이
|
|
```python
|
|
import numpy as np
|
|
def lda_fit(X, y, n_components=None):
|
|
classes = np.unique(y); d = X.shape[1]
|
|
mean_total = X.mean(0)
|
|
Sw = np.zeros((d, d)); Sb = np.zeros((d, d))
|
|
for c in classes:
|
|
Xc = X[y == c]
|
|
mc = Xc.mean(0)
|
|
Sw += (Xc - mc).T @ (Xc - mc)
|
|
Sb += len(Xc) * np.outer(mc - mean_total, mc - mean_total)
|
|
eigvals, eigvecs = np.linalg.eig(np.linalg.pinv(Sw) @ Sb)
|
|
idx = np.argsort(-eigvals.real)
|
|
W = eigvecs[:, idx[:n_components or len(classes)-1]].real
|
|
return W
|
|
|
|
W = lda_fit(X, y, n_components=2)
|
|
X_proj = X @ W
|
|
```
|
|
|
|
### Pipeline — PCA → LDA
|
|
```python
|
|
from sklearn.decomposition import PCA
|
|
pipe = Pipeline([
|
|
("pca", PCA(n_components=50)), # 1차로 dim 줄임 (S_W singular 회피)
|
|
("lda", LinearDiscriminantAnalysis(n_components=9)), # 10-class 가정
|
|
("clf", LogisticRegression()),
|
|
]).fit(X_tr, y_tr)
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Class별 Gaussian + 공분산 동일 | LDA (분류 + dim reduction) |
|
|
| Class별 공분산 다름 | QDA |
|
|
| d >> n 또는 small sample | Shrinkage LDA |
|
|
| 비-Gaussian / 비선형 | Kernel LDA, tree, NN |
|
|
| 시각화 (class-aware) | LDA(n_components=2 또는 3) |
|
|
|
|
**기본값**: `StandardScaler` 후 sklearn `LinearDiscriminantAnalysis(solver="lsqr", shrinkage="auto")`.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[Dimensionality-Reduction]]
|
|
- 응용: [[Image-Classification-Mastery]], [[Bioinformatics]]
|
|
- Adjacent: [[PCA]], [[Logistic-Regression-Foundations]], [[Naive-Bayes]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: PCA vs LDA 결정 가이드, scatter matrix 직관 설명, shrinkage 파라미터 해석.
|
|
**언제 X**: covariance 동일성 통계 검정 — Box's M test 등 statistician 영역.
|
|
|
|
## ❌ 안티패턴
|
|
- **Scaling 안 함**: 큰 scale feature가 scatter matrix 지배.
|
|
- **C-1 차원 초과 요구**: 수학적으로 불가능.
|
|
- **Class 매우 불균형**: priors 자동값으로 거대 majority 쏠림.
|
|
- **고차원 소표본 raw LDA**: S_W singular → shrinkage 또는 PCA 선행.
|
|
- **비-Gaussian 무시**: outlier 한 개로 전체 boundary 흔들림.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (Fisher 1936, ESL Ch.4, sklearn 1.5+).
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — Shrinkage/QDA, from-scratch eigen, PCA→LDA pipeline |
|