Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

TSNE

TSNE is a nonlinear method for embedding high-dimensional samples into a lower-dimensional space for visualization. It models pairwise similarities as probabilities and optimizes their low-dimensional representation.

How it works

TSNE converts similarities among samples into joint probabilities, then optimizes a low-dimensional representation by minimizing their Kullback–Leibler divergence. Because its objective is non-convex, different initializations can produce different embeddings.

Syntax

class sklearn.manifold.TSNE(*, n_components=2, perplexity=30.0, early_exaggeration=12.0, learning_rate="auto", max_iter=1000, n_iter_without_progress=300, min_grad_norm=1e-7, metric="euclidean", metric_params=None, init="pca", verbose=0, random_state=None, method="barnes_hut", angle=0.5, n_jobs=None)

Parameters

NameTypeDescription
n_componentsint, default=2Dimension of the embedded space.
perplexityfloat, default=30.0Related to the number of nearest neighbors used in other manifold learning algorithms.
early_exaggerationfloat, default=12.0Controls the initial exaggeration of joint probabilities.
learning_ratefloat or "auto", default="auto"The learning rate for t-SNE.
max_iterint, default=1000Maximum number of iterations for the optimization.
metricstr or callable, default="euclidean"Metric used when calculating distances between instances.
init{"random", "pca"} or ndarray of shape (n_samples, n_components), default="pca"Initialization of the embedding.
random_stateint, RandomState instance or None, default=NoneSeed or random-state control for reproducible results.

Returns

fit_transform returns an ndarray of shape (n_samples, n_components) containing the embedded samples.

Key points

  • TSNE is the scikit-learn estimator implementing t-distributed stochastic neighbor embedding.
  • The method represents similarities as joint probabilities before constructing the low-dimensional embedding.
  • Training minimizes the divergence between the high-dimensional and embedded probability distributions.
  • n_components sets the number of dimensions in the resulting embedding and defaults to 2.
  • fit learns an embedding from the supplied samples; fit_transform both fits and returns the transformed data.
  • The documented usual learning-rate range is 10.0 to 1000.0.

Common mistakes

  • Different initializations can lead to different results, so use a fixed random_state when reproducibility matters.
  • Choose perplexity with the neighborhood scale of the data in mind; it is not an arbitrary dimensionality setting.
  • If setting learning_rate manually, keep it within the documented practical range unless you have a specific reason not to.

Example: Fit a two-dimensional embedding

import numpy as np
from sklearn.manifold import TSNE

X = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
embedding = TSNE(
    n_components=2,
    learning_rate="auto",
    init="random",
    perplexity=3,
    random_state=0,
).fit_transform(X)
print(embedding.shape)

Creates a two-dimensional t-SNE embedding and prints its shape.

When to use it

Cluster visualization

Visualizing clusters in high-dimensional feature data.

Neighborhood exploration

Inspecting local neighborhoods after feature extraction.

Low-dimensional plotting

Projecting embeddings or transformed features into two dimensions.

Interview questions

QWhat does TSNE optimize?

TSNE is a nonlinear embedding method for visualizing high-dimensional data. It transforms pairwise similarities into joint probabilities and minimizes the Kullback–Leibler divergence between the original and embedded distributions.

Follow-up: Why can two runs disagree? Its cost function is non-convex, so different initializations may converge to different embeddings.

QWhat does n_components control?

n_components controls the dimension of the output embedding and defaults to 2. The estimator's fit_transform method returns the transformed samples in that lower-dimensional space.

Follow-up: What is a common visualization choice? Use n_components=2 or 3, depending on whether the embedding will be plotted in two or three dimensions.

QHow do perplexity and learning_rate affect configuration?

perplexity is related to the number of nearest neighbors used to characterize local structure. learning_rate controls the optimization step scale, and the documentation places its usual range at 10.0 to 1000.0.

Follow-up: How can reproducibility be improved? Set random_state to a fixed value and keep the initialization and other parameters unchanged.

  • sklearn.manifold.MDS
  • sklearn.manifold.Isomap
  • sklearn.manifold.SpectralEmbedding
  • sklearn.decomposition.PCA

Source: TSNE — scikit-learn 1.9.1 documentation. Checked 2026-10-15.