Based on the MLS-C01 exam guideReport an error
n_components=0.95) or the elbow of the scree plot.transform new data, and distances between clusters are meaningless.algorithm_mode=randomized.Week 4 moves into the second pillar of exploratory data analysis (EDA): handling high-dimensional data. As features multiply, a model seems to have more information, but as dimensions grow, the volume of space that the same number of samples must fill grows exponentially. As a result, data points drift apart from one another and distance-based measures lose their discriminating power.
| Symptom | Description |
|---|---|
| Data sparsity | As dimensions grow, the number of samples needed for the same density grows exponentially |
| Distance homogenization | The distances of all point pairs become similar, so telling nearest from farthest becomes meaningless |
| Overfitting risk | When the number of features approaches the number of samples, the model memorizes noise |
| Computational cost | Training and inference costs grow in proportion to (or with the square of) the dimension |
[Low dimension (2D)] [High dimension (d ≫ 1)]
● ● ● ●
● ← close ● ● ●
● ← far ● ● ●
All pairwise distances are similar
Nearest / farthest distinguishable d_max / d_min → 1
→ KNN·K-means work normally → the notion of a neighbor collapses
| Algorithm | Vulnerability | Reason |
|---|---|---|
| KNN | Very high | Decides neighbors by Euclidean distance |
| K-means | Very high | Assigns clusters by distance to the centroid |
| SVM (RBF kernel) | High | Kernel values depend on distances between samples |
| Linear/logistic regression | Medium | When the number of features approaches the number of samples, coefficients become unstable and the model overfits |
| Decision Tree / Random Forest | Medium to low | Looks at one feature at a time, but many irrelevant features degrade split quality |
| Naive Bayes | Low | Multiplies per-feature conditional probabilities, so it does not use a notion of distance |
Algorithms that depend on Euclidean distance, such as KNN and K-means, are especially vulnerable in high dimensions.
The mathematical core of the curse of dimensionality is that, in a high-dimensional unit hypercube, the ratio of the maximum to the minimum distance between two random points converges to 1. In other words, the "nearest neighbor" and the "farthest neighbor" become effectively the same. For this reason, dimensionality reduction does not merely cut computation; it also restores the space in which distance-based learning works.
PCA is a linear dimensionality-reduction technique that finds orthogonal axes (principal components) that preserve as much variance as possible and projects the data onto those axes.
Key precondition: always standardize before PCA. Because it is variance-based, variables with large scales dominate the principal components.
| Item | Requirement | Reason |
|---|---|---|
| Scale | Standardization required | It is variance-based, so large-scale variables monopolize the principal components |
| Missing values | Impute beforehand | Missing values cannot enter the covariance computation |
| Categorical | Encoding required (one-hot, etc.) | It accepts only numeric input |
| Target | Not used | PCA is an unsupervised transform |
| Fit scope | Fit on the training set only | Fitting with test statistics causes leakage |
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X_scaled = StandardScaler().fit_transform(X_train)
pca = PCA(n_components=0.95) # minimum number of components explaining 95% of the variance
X_reduced = pca.fit_transform(X_scaled)
print("Selected number of components:", pca.n_components_)
print("Explained variance ratio:", pca.explained_variance_ratio_.cumsum())If you pass a 0–1 ratio to n_components instead of an integer, it automatically selects "the minimum number of principal components whose cumulative explained variance exceeds that ratio."
| Method | What it does | Criterion |
|---|---|---|
| Target explained variance | Minimum k whose cumulative explained variance exceeds the target | Specify the ratio directly, as in n_components=0.95 |
| Scree plot elbow | The point where the eigenvalue (explained variance) drops sharply | The elbow where the curve bends |
| Downstream model performance | Compare cross-validation scores while varying k | Costly, but the most practical |
Choose an appropriate k from the scree plot or the elbow of the cumulative explained variance curve.
PCA is equivalent to an eigendecomposition of the data's covariance matrix. The eigenvectors are the principal component directions, and the eigenvalues are the variance in those directions. So the "explained variance ratio" is simply each eigenvalue divided by the sum of all eigenvalues. Singular value decomposition (SVD) yields the same result, and scikit-learn and SageMaker use randomized SVD on large data to improve efficiency.
⚠️ Pitfall: PCA is an unsupervised transform that never looks at the target. "The direction of large variance" is not always "the direction useful for prediction." If a decisive signal hides in a low-variance axis, PCA may throw it away.
⚠️ Pitfall: Principal components are linear combinations of the original features, so physical meaning is lost. If a question says "you must explain which variables were used for prediction," the answer is feature selection, not PCA.
Large datasets cannot be loaded into memory entirely, so use SageMaker's built-in PCA algorithm.
| Mode | When to use |
|---|---|
regular | Medium-sized number of features and observations |
randomized | When the number of features is very large (approximate SVD provides scalability) |
recordIO-protobuf or CSV, and training supports distributed processing| Hyperparameter | Role |
|---|---|
feature_dim | Number of input features |
num_components | Number of principal components to keep |
algorithm_mode | regular / randomized |
subtract_mean | Whether to subtract the mean |
from sagemaker import image_uris
container = image_uris.retrieve("pca", region="ap-northeast-2")
# Example Estimator hyperparameters
hyperparameters = {
"feature_dim": 784,
"num_components": 50,
"algorithm_mode": "randomized",
"subtract_mean": "true",
}| Situation | Service / feature | Reason |
|---|---|---|
| Extract principal components from a large matrix that exceeds memory | SageMaker built-in PCA | Distributed training, supports recordIO-protobuf/CSV input |
| Very many features and scalability is the priority | Built-in PCA + algorithm_mode=randomized | Approximate SVD saves memory and time |
| Exploratory reduction of small data in a notebook | SageMaker notebook + scikit-learn PCA | Free experimentation such as scree plots |
| Downstream classification or regression on the reduced features | Built-in algorithms such as SageMaker XGBoost | Use the PCA output directly as training input |
t-SNE (t-distributed Stochastic Neighbor Embedding) is a nonlinear technique that maps data so that neighbor relationships (local structure) in high dimensions are preserved in 2–3 dimensions.
| Item | PCA | t-SNE |
|---|---|---|
| Linear/nonlinear | Linear | Nonlinear |
| Purpose | Variance preservation, dimensionality reduction | Cluster visualization |
| Distance interpretation | Preserves global structure | Trust only local structure |
| Transforming new data | transform possible | Not possible (retrain each time) |
| Determinism | Deterministic | Random (depends on seed) |
t-SNE is excellent for seeing clusters with the eye, but it is not used as preprocessing for model input. Distances between clusters and cluster sizes have no physical meaning, and new samples cannot be projected into the same space.
from sklearn.manifold import TSNE
tsne = TSNE(n_components=2, perplexity=30, random_state=42)
X_embedded = tsne.fit_transform(X_scaled) # 2D coordinates for visualizationperplexity balances how many neighbors to consider and is typically tuned in the range 5–50.
t-SNE converts high-dimensional distances into conditional probability distributions and minimizes the KL divergence so that a heavy-tailed t-distribution in low dimensions reproduces the same relationships. The heavy tails of the t-distribution relieve the "crowding problem," letting clusters spread out instead of clumping. Because of this process, however, the size of the empty space between clusters is meaningless — on the exam, an option that says "judge similarity by the distance between t-SNE clusters" is a trap.
t-SNE has only fit_transform; it has no transform that stores the learned mapping and applies it to new samples. An option that says "put new data arriving in production into the same embedding space" is wrong.
| Technique | Linear/nonlinear | New-data transform | Main use | When not to use |
|---|---|---|---|---|
| PCA | Linear | Possible (transform) | Model preprocessing, noise removal, mitigating multicollinearity | When interpretation of the original variables is needed, or when relationships are strongly nonlinear |
| t-SNE | Nonlinear | Not possible (retrain) | 2–3D cluster visualization | Model input, projecting new data, interpreting distances between clusters |
| Autoencoder | Nonlinear | Possible (reuse the encoder) | Nonlinear compression, anomaly detection | When you cannot afford the data and tuning cost, or when axis interpretation is needed |
| Feature selection (Day 2) | — | Possible (original columns kept) | Reduction where interpretability is required | When interactions between features are the key to performance (filter methods only) |
You need to reduce dimensions
├─ Must the result be explained variable by variable? (regulated industry, audit response)
│ ├─ Yes → Feature selection (keep the original columns as they are)
│ └─ No ↓
├─ Is the goal "check by eye whether clusters exist"?
│ ├─ Yes → t-SNE (2–3D, visualization only. Never as model input)
│ └─ No ↓
├─ Are relationships mostly linear?
│ ├─ Yes → PCA (standardize → choose k by cumulative explained variance)
│ └─ No → Nonlinear compression such as an autoencoder
└─ Does the data exceed single-instance memory?
└─ Yes → SageMaker built-in PCA (randomized if there are many features)
Dimensionality reduction is only one solution to the high-dimensional problem. The exam asks not "reduce the dimensions" but "what fits this situation."
| Technique | Effect | When to use | Caution |
|---|---|---|---|
| Standardization (Z-score) | Puts all features on the same scale | Before PCA, KNN, SVM, neural networks | Fit on the training set only |
| PCA | Compresses correlated axes into orthogonal axes | Many features that are correlated with each other | Not interpretable; standardize first |
| Feature selection | Removes irrelevant or redundant columns | Interpretation needed, manageable number of features | May miss interactions |
| Remove constant / quasi-constant columns | Removes axes with no information | The first step in any situation | Beware of columns whose values appear only in a minority class |
| Regularization terms (L1/L2) | Shrinks coefficients or sets them to 0 | Number of features ≳ number of samples | Scaling must come first |
| Embeddings | Turns high-cardinality categories into dense vectors | Deep learning, ultra-high cardinality | Needs training data and a network |
[High-dimensional raw data]
│
① Split (train / test) ← from here on, every fit uses the training set only
│
② Missing-value imputation · encoding
│
③ Standardization (StandardScaler)
│
④ ┌─ Interpretation needed ─→ Feature selection (keep original columns)
└─ Not needed ─→ PCA (n_components=0.95)
│
⑤ Model training (XGBoost / linear / neural network …)
│
⑥ On the test set, apply only transform with what was learned in ②–④
| Clue in the question | Answer to choose | Reason |
|---|---|---|
| "There are 784 features and scalability matters" | SageMaker built-in PCA, algorithm_mode=randomized | Approximate SVD gives scalability in high dimensions |
| "What do you do before applying PCA?" | Standardization (scaling) | It is variance-based, so scale dominates the result |
| "I want to see by eye whether clusters exist" | t-SNE 2D embedding | Reveals local neighbor structure visually |
| "Use t-SNE coordinates as classifier input" | Wrong option | New data cannot be transformed, and distances are meaningless |
| "You must explain which variables were used for prediction" | Feature selection | Keeps the original features, so it is interpretable |
| "KNN accuracy drops as dimensions increase" | Curse of dimensionality → dimensionality reduction | Distance homogenization makes neighbor decisions meaningless |
| "You must decide how many principal components to keep" | Cumulative explained variance / scree plot elbow | A standard method based on the variance structure |
The next post covers feature selection, which reduces dimensions while keeping the original features, and multicollinearity.
Click a choice to reveal the answer and explanation.
Question 1
You want to reduce the dimensionality of 784-dimensional image data with SageMaker and use it as input to a downstream classification model. The number of features is large, so scalability is important. What is the MOST appropriate choice?
Question 2
What preprocessing must be done before applying PCA?
Question 3
Which statement about t-SNE is NOT correct?
Question 4
What is the most appropriate way to choose an appropriate number of principal components k in PCA?
Question 5
Which type of algorithm does the curse of dimensionality worsen MOST directly?