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

AI & ML · Const

t-SNE

OpenAI’s embeddings guide uses t-SNE to transform high-dimensional embedding data into two dimensions for visualization. The example applies scikit-learn’s TSNE to an embedding matrix and recommends cosine similarity for comparing embeddings.

How it works

t-SNE is used to transform high-dimensional embedding vectors into two dimensions so their relationships can be visualized. The documented example configures the number of output dimensions, perplexity, initialization, learning rate, and random seed before transforming the embedding matrix.

Syntax

TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200).fit_transform(matrix)

Parameters

NameTypeDescription
n_componentsintegerNumber of output dimensions; the documented example sets this to 2.
perplexitynumberNeighborhood-size setting used by the documented t-SNE example.
random_stateintegerSeed used to make the documented example reproducible.
initstringInitialization strategy used by the documented example.
learning_ratenumberOptimization learning rate used by the documented example.

Returns

A two-dimensional representation of the embedding matrix for visualization.

Key points

  • The dimensionality of the source vectors depends on the embedding model, so visualization code should not assume a fixed input width.
  • The workflow explicitly creates a t-SNE model and then transforms the embedding matrix.
  • The official example uses two output dimensions, perplexity 15, a fixed seed, random initialization, and learning rate 200.
  • Use cosine similarity for embedding relatedness rather than interpreting t-SNE coordinates as the canonical similarity space.

Common mistakes

  • t-SNE is a visualization transformation, not a replacement for the original embedding space or its similarity metric.

Example: Project embeddings with t-SNE

import numpy as np
import pandas as pd
from sklearn.manifold import TSNE

embeddings = np.array([
    [0.1, 0.2, 0.3],
    [0.2, 0.1, 0.4],
    [0.8, 0.7, 0.6],
    [0.7, 0.8, 0.5],
])

projection = TSNE(
    n_components=2,
    perplexity=2,
    random_state=42,
    init="random",
    learning_rate=200,
).fit_transform(embeddings)

print(pd.DataFrame(projection, columns=["x", "y"]))

This projects four embedding vectors into two dimensions for visualization.

When to use it

Visualize high-dimensional embeddings in two dimensions

Exploring embedding datasets

Look for clusters or relationships in projected vectors

Inspecting semantic structure

Use cosine similarity on original embeddings while reserving t-SNE for plots

Maintaining retrieval quality

Interview questions

QWhat is t-SNE used for in the embeddings workflow?

t-SNE takes high-dimensional embedding vectors and maps them to two dimensions for visualization. It is useful for inspecting structure in embedding data, but the projection is not the embedding itself.

Follow-up: Why should similarity evaluation remain in the original space? The documentation recommends cosine similarity for comparing embeddings, while t-SNE is introduced for visualization.

QWhich t-SNE parameters appear in the official example?

The documented configuration sets n_components to 2, uses perplexity 15, initializes randomly, sets learning_rate to 200, and fixes random_state at 42. These settings produce a reproducible two-dimensional projection for the example.

Follow-up: What should change when the dataset or visualization goal changes? Reconsider the projection parameters and validate the resulting plot rather than treating the example values as universal defaults.

QWhat does t-SNE transform?

The input is a matrix created from the embedding column, converted to a NumPy array, and passed to TSNE.fit_transform. The result is a two-dimensional representation of the original embedding rows.

Follow-up: How can the resulting coordinates be used? Attach them to the corresponding records and plot them, while retaining the source embeddings for actual similarity operations.

  • embeddings
  • cosine similarity
  • TSNE
  • NumPy
  • pandas

Source: Vector embeddings | OpenAI API. Checked 2026-10-15.