Challenge - 5 Problems
PCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this PCA variance explained code?
Given the following MATLAB code that performs PCA on a dataset, what is the output of the variable
explainedVariance?MATLAB
data = [2.5 2.4; 0.5 0.7; 2.2 2.9; 1.9 2.2; 3.1 3.0; 2.3 2.7; 2 1.6; 1 1.1; 1.5 1.6; 1.1 0.9]; [coeff, score, latent, tsquared, explained] = pca(data); explainedVariance = round(explained, 1);
Attempts:
2 left
💡 Hint
Think about how PCA finds directions with most variance and how explained variance is calculated.
✗ Incorrect
The first principal component explains about 95.6% of the variance, and the second explains about 4.4%. This is typical for this dataset where most variance is along one direction.
🧠 Conceptual
intermediateWhich statement best describes the role of eigenvectors in PCA?
In Principal Component Analysis, what do the eigenvectors of the covariance matrix represent?
Attempts:
2 left
💡 Hint
Think about what PCA tries to find in the data.
✗ Incorrect
Eigenvectors of the covariance matrix point in the directions where the data varies the most. PCA uses these directions to reduce dimensionality.
🔧 Debug
advancedWhy does this PCA code produce an error?
Consider this MATLAB code snippet for PCA:
data = rand(10,3); [coeff, score, latent] = pca(data); reducedData = score(:, 4);Why does this code produce an error?
Attempts:
2 left
💡 Hint
Check the size of the 'score' matrix relative to the input data dimensions.
✗ Incorrect
The input data has 3 features, so PCA returns 3 principal components. Trying to access the 4th component causes an index out-of-bounds error.
📝 Syntax
advancedWhich option correctly computes the covariance matrix for PCA?
In MATLAB, which code snippet correctly computes the covariance matrix of the data matrix
X for PCA?Attempts:
2 left
💡 Hint
Remember that MATLAB's cov function uses normalization by (N-1) by default; the second argument controls normalization.
✗ Incorrect
cov(X,0) computes the covariance matrix normalized by (N-1), which is the unbiased estimator used in PCA. cov(X,1) normalizes by N, which is biased. cov(X) is equivalent to cov(X,0). X'*X is not the covariance matrix unless data is centered and scaled.
🚀 Application
expertHow many principal components are needed to explain at least 90% variance?
Given the explained variance ratios from PCA on a dataset as follows:
explained = [40.5, 25.0, 15.0, 10.0, 5.0, 4.5];How many principal components are needed to explain at least 90% of the total variance?
Attempts:
2 left
💡 Hint
Add the explained variances cumulatively until the sum reaches or exceeds 90%.
✗ Incorrect
Adding the first 3 components gives 40.5 + 25 + 15 = 80.5%, which is less than 90%. Adding the 4th component (10%) reaches 90.5%, which meets the requirement.
