TensorTonic
Problems
PCA Projection
Classic MLLinear Algebra
Hard

Principal Component Analysis (PCA) finds the directions of maximum variance in the data and projects it onto a lower-dimensional subspace. This is done by computing the eigenvectors of the covariance matrix and projecting onto the top-k eigenvectors (principal components).

Given a data matrix X (n samples, d features) and the number of components k, project the data onto its top-k principal components.

Algorithm

  1. Center the data by subtracting the mean of each feature

  2. Compute the d x d covariance matrix using sample covariance (divide by n-1)

C=1n−1XcTXcC = \frac{1}{n-1} X_c^T X_cC=n−11​XcT​Xc​
  1. Find the top-k eigenvectors of C sorted by eigenvalue in descending order (e.g., using power iteration with deflation)

  2. Project the centered data onto these eigenvectors

Xproj=Xc⋅WX_{\text{proj}} = X_c \cdot WXproj​=Xc​⋅W

Where W is the d x k matrix whose columns are the top-k eigenvectors.

Return an n by k list of projected values.

Loading visualization...

Examples

Input: X = [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], k = 1

Output: [[-2.0], [-1.0], [0.0], [1.0], [2.0]]

Explanation: Centering leaves all variance along the first feature.

Input: X = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], k = 1

Output: [[-2.8284], [-1.4142], [0.0], [1.4142], [2.8284]]

Hint 1

Center each feature before forming the sample covariance matrix.

Hint 2

Use np.linalg.eigh, sort eigenvectors by descending eigenvalue, then project onto the first k columns.

Requirements

  • Center the data by subtracting the column means
  • Compute the covariance matrix using n-1 (sample covariance)
  • Find the top-k eigenvectors ordered by decreasing eigenvalue
  • Project the centered data onto these k eigenvectors
  • Return an n x k list of floats

Constraints

  • X has at least 2 rows and k <= d (number of features)
  • The top-k eigenvalues are distinct (no ties)
  • Return an n x k list of floats
  • Time limit: 300 ms
Try Similar Problems
Compute Pearson Correlation MatrixMediumCalculate Eigenvalues of a MatrixMediumImplement Matrix NormalizationMediumMatrix InverseMediumCompute Covariance MatrixEasyLinear Regression Closed FormMediumGaussian Naive BayesHardImplement BM25 Ranking in PythonHardRBF Kernel MatrixHardWhitening TransformHardPCA from ScratchMediumt-SNE Affinity MatrixMedium

Accepts: array

Accepts: number

You must run your code first.
PCA Projection

PCA Projection

Classic MLLinear Algebra
Hard

Principal Component Analysis (PCA) finds the directions of maximum variance in the data and projects it onto a lower-dimensional subspace. This is done by computing the eigenvectors of the covariance matrix and projecting onto the top-k eigenvectors (principal components).

Given a data matrix X (n samples, d features) and the number of components k, project the data onto its top-k principal components.

Algorithm

  1. Center the data by subtracting the mean of each feature

  2. Compute the d x d covariance matrix using sample covariance (divide by n-1)

C=1n−1XcTXcC = \frac{1}{n-1} X_c^T X_cC=n−11​XcT​Xc​
  1. Find the top-k eigenvectors of C sorted by eigenvalue in descending order (e.g., using power iteration with deflation)

  2. Project the centered data onto these eigenvectors

Xproj=Xc⋅WX_{\text{proj}} = X_c \cdot WXproj​=Xc​⋅W

Where W is the d x k matrix whose columns are the top-k eigenvectors.

Return an n by k list of projected values.

Loading visualization...

Examples

Input: X = [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], k = 1

Output: [[-2.0], [-1.0], [0.0], [1.0], [2.0]]

Explanation: Centering leaves all variance along the first feature.

Input: X = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], k = 1

Output: [[-2.8284], [-1.4142], [0.0], [1.4142], [2.8284]]

Hint 1

Center each feature before forming the sample covariance matrix.

Hint 2

Use np.linalg.eigh, sort eigenvectors by descending eigenvalue, then project onto the first k columns.

Requirements

  • Center the data by subtracting the column means
  • Compute the covariance matrix using n-1 (sample covariance)
  • Find the top-k eigenvectors ordered by decreasing eigenvalue
  • Project the centered data onto these k eigenvectors
  • Return an n x k list of floats

Constraints

  • X has at least 2 rows and k <= d (number of features)
  • The top-k eigenvalues are distinct (no ties)
  • Return an n x k list of floats
  • Time limit: 300 ms
Try Similar Problems
Compute Pearson Correlation MatrixMediumCalculate Eigenvalues of a MatrixMediumImplement Matrix NormalizationMediumMatrix InverseMediumCompute Covariance MatrixEasyLinear Regression Closed FormMediumGaussian Naive BayesHardImplement BM25 Ranking in PythonHardRBF Kernel MatrixHardWhitening TransformHardPCA from ScratchMediumt-SNE Affinity MatrixMedium

Accepts: array

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1