PCA is a method to reduce data size by keeping the most important parts. The key metric is explained variance ratio. It tells us how much of the original data's information is kept in the new smaller set. We want this number to be high, so we don't lose important details.
Principal Component Analysis (PCA) in ML Python - Model Metrics & Evaluation
PCA does not use a confusion matrix because it is not a classification method. Instead, we look at the explained variance ratio for each principal component. For example:
Component 1: 60% variance explained Component 2: 25% variance explained Component 3: 10% variance explained Component 4: 5% variance explained Total variance explained by first 2 components: 85%
This means the first two components keep 85% of the original data's information.
Choosing more components keeps more information but makes data bigger and harder to use. Choosing fewer components makes data smaller but loses some details.
For example, if you keep only 2 components out of 10 original features, you might keep 85% of information but lose 15%. This is a tradeoff between simplicity and accuracy.
Good: Explained variance ratio above 80% with few components. This means you keep most information with less data.
Bad: Explained variance ratio below 50% with many components. This means you lose too much information or need many components, defeating the purpose of PCA.
- Ignoring explained variance ratio and choosing components arbitrarily.
- Using PCA on data without scaling features first, which can distort results.
- Assuming PCA improves model accuracy directly; it only reduces data size.
- Overfitting by keeping too many components, losing the benefit of simplification.
Your PCA model keeps 3 components out of 10 and explains 40% of the variance. Is this good? Why or why not?
Answer: This is not good because 40% variance explained is low. It means most information is lost. You should try to keep more components or check data preprocessing.
Practice
Solution
Step 1: Understand PCA's goal
PCA aims to reduce many features into fewer main features without losing important information.Step 2: Compare options with PCA's goal
To reduce the number of features while keeping important information correctly states PCA reduces features while keeping key info. Other options describe different tasks.Final Answer:
To reduce the number of features while keeping important information -> Option AQuick Check:
PCA purpose = reduce features + keep info [OK]
- Thinking PCA increases features
- Confusing PCA with data splitting
- Mixing PCA with data normalization
Solution
Step 1: Recall correct import syntax for PCA
In scikit-learn, PCA is in the decomposition module, imported as: from sklearn.decomposition import PCA.Step 2: Check each option's syntax
from sklearn.decomposition import PCA matches the correct Python import syntax. Others are invalid or incorrect.Final Answer:
from sklearn.decomposition import PCA -> Option DQuick Check:
Correct import = from sklearn.decomposition import PCA [OK]
- Using wrong import order
- Trying to import PCA directly from sklearn
- Incorrect Python import syntax
X_pca?
from sklearn.decomposition import PCA import numpy as np X = np.random.rand(100, 5) pca = PCA(n_components=2) X_pca = pca.fit_transform(X)
Solution
Step 1: Understand input and PCA parameters
Input X has shape (100 samples, 5 features). PCA is set to reduce to 2 components.Step 2: Determine output shape after PCA transform
fit_transform returns transformed data with shape (samples, n_components), so (100, 2).Final Answer:
(100, 2) -> Option CQuick Check:
Output shape = (samples, components) = (100, 2) [OK]
- Confusing samples and features in output shape
- Mixing up n_components with original features
- Thinking PCA changes sample count
from sklearn.decomposition import PCA import numpy as np X = np.random.rand(50, 4) pca = PCA(n_components=5) X_pca = pca.fit_transform(X)
Solution
Step 1: Check PCA parameter n_components vs data features
Data has 4 features, but n_components is set to 5, which is invalid.Step 2: Validate other code parts
Random data is fine, fit_transform is correctly called, and import is correct.Final Answer:
n_components cannot be greater than number of features -> Option AQuick Check:
n_components ≤ features = True [OK]
- Setting n_components larger than features
- Misunderstanding fit_transform order
- Thinking random data is invalid
Solution
Step 1: Understand PCA's explained variance ratio parameter
Setting n_components as a float between 0 and 1 tells PCA to select components to keep that fraction of variance.Step 2: Evaluate options for keeping 95% variance
Set n_components=0.95 to keep 95% variance automatically uses n_components=0.95, which automatically selects the right number of components. Others do not guarantee 95% variance.Final Answer:
Set n_components=0.95 to keep 95% variance automatically -> Option BQuick Check:
n_components=0.95 keeps 95% variance [OK]
- Choosing fixed number without variance check
- Setting n_components too low or too high blindly
- Confusing number of features with variance percentage
Ready to prove it?
Pass the test and this pattern moves unknown
