Principal Component Analysis (PCA)

Dimensionality Reduction & Anomaly Detection DS practice problem on Onlearn.

Difficulty: medium.

Topics: Understanding Principal Component Analysis (PCA) Implementation, Eigenvalue Decomposition, Covariance Matrix, Z-score Standardization, Orthogonal Projection, Explained Variance Ratio, Linear Algebra, Statistics, Unsupervised Learning, Numerical Analysis, Data Preprocessing, Matrix Decompositions, Multivariate Analysis, Dimensionality Reduction, Feature Scaling, Linear Transformations.

Write a Python function that performs Principal Component Analysis (PCA) from scratch. The function should take a 2D NumPy array as input, where each row represents a data sample and each column represents a feature. The function should standardize the dataset, compute the covariance matrix, find the eigenvalues and eigenvectors, and return the principal components (the eigenvectors corresponding to the largest eigenvalues). The function should also take an integer k as input, representing the number of principal components to return.