PCA
Principal Component Analysis
Principal component analysis. A dimensionality reduction technique. Data visualization, noise removal.
Principal Component Analysis
Principal component analysis. A dimensionality reduction technique. Data visualization, noise removal.
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.
# 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}")
"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."
"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."
"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
If variables have different scales, the variable with larger values dominates the first principal component. Be sure to standardize with StandardScaler.
PCA is a linear transformation, so it cannot capture nonlinear relationships. For nonlinear data, consider Kernel PCA or t-SNE.
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.