Dimensionality Reduction: PCA and t-SNE Explained

Dimensionality reduction with PCA and t-SNE, measured on real 64-dimensional digit images: how much variance PCA keeps per component, what compressing features costs a model's accuracy, why t-SNE preserves neighbourhoods far better in a 2D plot, and the rules for reading t-SNE plots without fooling yourself.

GloryTecks Team

Training & Placement Faculty

6 May 2026 6 min read

Last updated: 25 September 2026

Share:

A dataset with dozens or hundreds of columns is hard to visualise and can be slow or noisy to model. Dimensionality reduction maps it into fewer dimensions while keeping as much of the important structure as possible. The two methods most people meet first, PCA and t-SNE, solve different problems: PCA compresses data for modelling and understanding, and t-SNE draws maps of it for looking. Using one for the other's job is a common mistake. Here both are measured on the same data: 1,797 handwritten digit images, each a 64-dimensional vector of pixel values.

PCA: find the directions that matter

Principal component analysis finds new axes, the principal components, along which the data varies most. The first component captures the most variance, the second the most of what is left, at right angles to the first, and so on (scikit-learn: PCA). Keeping only the first few components compresses the data with the least loss of variance.

python
import numpy as np
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.manifold import TSNE, trustworthiness
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_digits(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)
cumulative = np.cumsum(PCA().fit(X_scaled).explained_variance_ratio_)
print({k: round(cumulative[k - 1], 3) for k in (2, 10, 20, 30, 40)})
print("components for 95% of variance:", int(np.argmax(cumulative >= 0.95)) + 1)
text
{2: 0.216, 10: 0.589, 20: 0.793, 30: 0.893, 40: 0.951}
components for 95% of variance: 40

Two components keep only about a fifth of the variance: a 2D PCA plot of this data shows a lot of overlap. Forty of the 64 components keep 95%. Standardising first matters, because PCA chases variance, and unscaled features with large ranges would dominate the components.

What compression costs a model

Fewer features can mean faster training and less noise, but also less information. Putting PCA inside a pipeline and cross-validating shows the trade-off directly:

python
for k in (5, 10, 20, 40, None):
    steps = [StandardScaler()] + ([PCA(n_components=k)] if k else []) + [LogisticRegression(max_iter=3000)]
    accuracy = cross_val_score(make_pipeline(*steps), X, y, cv=5).mean()
    print(f"{k if k else 'all 64'} components: accuracy {accuracy:.3f}")
text
5 components: accuracy 0.771
10 components: accuracy 0.840
20 components: accuracy 0.899
40 components: accuracy 0.914
all 64 components: accuracy 0.920

Forty components get within a percentage point of using all 64. Twenty cost two more points. Five lose a lot. Choose the number of components from a curve like this, for your model and metric, rather than from a variance threshold alone. Keeping PCA inside the pipeline also means it is fitted only on training folds, which avoids leaking information from the test data.

t-SNE: a map of neighbourhoods

t-SNE does something different. It places points in two or three dimensions so that points that are close in the original space stay close in the map, caring much less about large distances (scikit-learn: t-SNE). The trustworthiness score measures how well each method keeps each point's nearest neighbours as neighbours, from 0 to 1 (scikit-learn: trustworthiness):

python
pca_2d = PCA(n_components=2).fit_transform(X_scaled)
tsne_2d = TSNE(n_components=2, perplexity=30, init="pca", random_state=0).fit_transform(X_scaled)
print("PCA:", round(trustworthiness(X_scaled, pca_2d, n_neighbors=5), 3),
      "| t-SNE:", round(trustworthiness(X_scaled, tsne_2d, n_neighbors=5), 3))

from sklearn.metrics import silhouette_score
print("digit separation (silhouette on true labels) PCA:", round(silhouette_score(pca_2d, y), 3),
      "| t-SNE:", round(silhouette_score(tsne_2d, y), 3))
text
PCA: 0.818 | t-SNE: 0.993
digit separation (silhouette on true labels) PCA: 0.055 | t-SNE: 0.485

In two dimensions, t-SNE keeps neighbourhoods almost perfectly. The second line measures how well the ten digits separate in each 2D map, using the silhouette score of the true labels: close to zero for PCA, where the digit groups overlap heavily, and far higher for t-SNE, where they form distinct groups. For looking at structure, t-SNE wins decisively.

Reading t-SNE plots without fooling yourself

The scikit-learn documentation lists the caveats, and they matter (scikit-learn: t-SNE):

  • Distances between clusters mean little. Two islands far apart are not necessarily more different than two close ones.
  • Cluster sizes mean little. t-SNE expands dense regions and contracts sparse ones.
  • Results depend on perplexity and randomness. Try several perplexity values and seeds; trust only patterns that persist.
  • There is no transform for new data. t-SNE maps the points it was fitted on; it is not a preprocessing step for a model.

So use t-SNE to explore: to see whether classes separate, to spot mislabelled points sitting in the wrong island, or to check whether embeddings group sensibly. Do not measure distances on it, and do not feed its output to a model.

PCA in practice: reading the components

PCA is not only a compression tool; its components can explain your data. Each component is a weighted combination of the original features, and the weights, called loadings, show which features move together. In a customer dataset, a first component with large positive loadings on visits, spend and items per order might be read as overall engagement, and a second contrasting discount use with full-price purchases as price sensitivity. Interpretations like these are hypotheses to check, not facts, but they often reveal redundant features and suggest simpler, more meaningful ones to build.

A workflow for high-dimensional data

  1. Standardise numeric features.
  2. Run PCA and look at the cumulative explained variance to see how much redundancy there is.
  3. Visualise with t-SNE or UMAP to explore groups and outliers, with a few perplexity settings.
  4. If you compress for a model, put PCA inside the pipeline and choose the number of components by cross-validated performance.
  5. Report what was lost: the variance dropped and the metric change, as in the table above.

Choosing between them

GoalUse
Compress features before a modelPCA, inside the pipeline
Remove noise and correlated featuresPCA
Understand which original features drive variationPCA (inspect component loadings)
Visualise clusters or classes in 2Dt-SNE (or UMAP)
Apply the same reduction to new dataPCA

UMAP is a popular alternative to t-SNE for visualisation that is often faster and preserves more of the global layout; it lives in a separate library.

Does PCA work with categorical data?

Not directly, because it relies on variance and linear combinations of numeric features. Encode categories first, or use methods designed for mixed data.

Should I always reduce dimensions before modelling?

No. Many models, especially tree ensembles and regularised linear models, cope well with many features. Reduce when it measurably helps speed, noise or overfitting.

Is dimensionality reduction the same as feature selection?

No. Feature selection keeps a subset of the original columns, which stay interpretable; PCA creates new columns that mix all of them. Both reduce dimensions, and feature engineering covers when selecting or building features is the better route.

Is t-SNE a clustering algorithm?

No. It produces a map that often shows clusters visually, but it does not assign cluster labels. Run a clustering algorithm on the original or PCA-reduced data if you need labels.

Reduced data is often clustered next; clustering algorithms compared shows how. Dimensionality reduction with PCA is part of the preprocessing and feature engineering module of the Data Science course.

Sources

GloryTecks Team

Training & Placement Faculty

The GloryTecks faculty team mentors learners across Data Science, AI and Data Engineering, with a focus on real-time projects and placement readiness in Hyderabad.

Want to master Data Science?

Join GloryTecks’ expert-led Data Science training in Hyderabad with real-time projects and 100% placement support.

Explore course