Imagine you have a dataset with many features. What is the main goal of applying Principal Component Analysis (PCA) to this data?
Think about how PCA helps simplify data while keeping important information.
PCA transforms the original features into new features called principal components. These components are combinations of the original features and are ordered to capture the most variance in the data.
What is the output of the following Python code using PCA?
from sklearn.decomposition import PCA import numpy as np X = np.array([[2, 0], [0, 2], [3, 3]]) pca = PCA(n_components=1) X_pca = pca.fit_transform(X) print(X_pca.round(2))
Remember PCA projects data onto the direction of maximum variance.
The PCA reduces the 2D data to 1D by projecting onto the principal component. The output shows the transformed values rounded to two decimals.
You want to reduce your dataset's dimensions using PCA but keep at least 90% of the variance. Which approach correctly helps you decide the number of components?
Check how sklearn PCA handles variance thresholds.
Setting n_components to a float between 0 and 1 tells PCA to select the minimum number of components that explain at least that fraction of variance.
After fitting PCA on a dataset, you get explained variance ratios: [0.6, 0.3, 0.1]. What does this mean?
Look at how variance ratios sum up and their order.
The explained variance ratio shows how much variance each principal component captures. They sum to 1 and are sorted from highest to lowest.
You run this code but get an output shape of (5, 5) instead of (5, 2) as expected:
from sklearn.decomposition import PCA import numpy as np X = np.random.rand(5, 3) pca = PCA(n_components=2) X_pca = pca.fit_transform(X) print(X_pca.shape)
What is the most likely reason?
Check the PCA import source and its default behavior.
If PCA is imported from a library other than sklearn.decomposition, it might ignore n_components and return all components, causing unexpected output shape.
