Cluster visualization
Visualizing clusters in high-dimensional feature data.
Scikit-Learn · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
n_components | int, default=2 | Dimension of the embedded space. |
perplexity | float, default=30.0 | Related to the number of nearest neighbors used in other manifold learning algorithms. |
early_exaggeration | float, default=12.0 | Controls the initial exaggeration of joint probabilities. |
learning_rate | float or "auto", default="auto" | The learning rate for t-SNE. |
max_iter | int, default=1000 | Maximum number of iterations for the optimization. |
metric | str 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_state | int, RandomState instance or None, default=None | Seed or random-state control for reproducible results. |
fit_transform returns an ndarray of shape (n_samples, n_components) containing the embedded samples.
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.
Visualizing clusters in high-dimensional feature data.
Inspecting local neighborhoods after feature extraction.
Projecting embeddings or transformed features into two dimensions.
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.
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.
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.MDSsklearn.manifold.Isomapsklearn.manifold.SpectralEmbeddingsklearn.decomposition.PCA