πŸ€– AI/ML

PCA

Principal Component Analysis

Principal component analysis. A dimensionality reduction technique. Data visualization, noise removal.

πŸ“– Detailed Explanation

Principal Component Analysis (PCA) is a representative unsupervised learning technique that reduces high-dimensional data to lower dimensions. It preserves as much of the data's variance as possible while transforming highly correlated variables into a smaller number of principal components.

PCA was first introduced by Karl Pearson in 1901 and was formalized in its current form by Harold Hotelling in 1933. Originating in statistics, it is a proven technique that has been used for more than 100 years in diverse fields such as image processing, genomics, and financial analysis.

The core principle is the eigenvalue decomposition of the covariance matrix. The direction in which the data's variance is largest is selected as the first principal component, and the direction with the next largest variance that is orthogonal to the first is selected as the second principal component. Each eigenvalue represents the proportion of variance explained by the corresponding principal component.

In practice, it is used for visualization (2D/3D plots), noise removal, feature extraction, and reducing computational cost. In embedding visualization, first reducing the dimensions with PCA before t-SNE or UMAP greatly improves computation speed.

πŸ’» Code Example

# PCA 차원 μΆ•μ†Œ 및 μ‹œκ°ν™” 예제
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

# 데이터 λ‘œλ“œ (Iris: 4차원)
iris = load_iris()
X = iris.data
y = iris.target

# 1. 데이터 ν‘œμ€€ν™” (PCA μ „ ν•„μˆ˜!)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 2. PCA 적용 (4차원 -> 2차원)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# 3. μ„€λͺ…λœ λΆ„μ‚° λΉ„μœ¨ 확인
print(f"각 μ£Όμ„±λΆ„μ˜ μ„€λͺ… λΆ„μ‚°: {pca.explained_variance_ratio_}")
print(f"λˆ„μ  μ„€λͺ… λΆ„μ‚°: {sum(pca.explained_variance_ratio_):.2%}")
# 좜λ ₯: μ•½ 95% (2개 μ£Όμ„±λΆ„μœΌλ‘œ 95%의 정보 보쑴)

# 4. 차원 μΆ•μ†Œλœ 데이터 μ‹œκ°ν™”
plt.figure(figsize=(8, 6))
scatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis')
plt.xlabel('PC1 (첫 번째 μ£Όμ„±λΆ„)')
plt.ylabel('PC2 (두 번째 μ£Όμ„±λΆ„)')
plt.title('PCA둜 μ‹œκ°ν™”ν•œ Iris 데이터셋')
plt.colorbar(scatter, label='Species')
plt.show()

# 5. 졜적 차원 수 κ²°μ • (λΆ„μ‚° 95% 이상 보쑴)
pca_full = PCA()
pca_full.fit(X_scaled)
cumsum = np.cumsum(pca_full.explained_variance_ratio_)
n_components = np.argmax(cumsum >= 0.95) + 1
print(f"95% λΆ„μ‚° 보쑴에 ν•„μš”ν•œ 차원: {n_components}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"To visualize the 1000-dimensional embedding data, we first reduced it to 50 dimensions with PCA and then applied t-SNE. The computation time dropped from 10 minutes to 30 seconds."
πŸ’¬ In an Interview
"PCA uses the eigenvectors of the covariance matrix as principal components. The first eigenvector indicates the direction of greatest variance, and the eigenvalue represents the amount of variance explained by that direction."
πŸ’¬ In a Technical Discussion
"Data standardization is essential before PCA. If variables have different scales, variables with larger scales dominate the first principal component, making meaningful analysis difficult."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Applying PCA without standardization

If variables have different scales, the variable with larger values dominates the first principal component. Be sure to standardize with StandardScaler.

❌
Applying PCA to nonlinear relationships

PCA is a linear transformation, so it cannot capture nonlinear relationships. For nonlinear data, consider Kernel PCA or t-SNE.

βœ…
The right way

Check explained_variance_ratio_ to choose an appropriate number of dimensions. Usually, you use the number of dimensions at which the cumulative variance reaches 95% or more.

πŸ”— Related Terms

πŸ“š Learn More