Visualize high-dimensional embeddings in two dimensions
Exploring embedding datasets
AI & ML · Const
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.
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.
TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200).fit_transform(matrix)| Name | Type | Description |
|---|---|---|
n_components | integer | Number of output dimensions; the documented example sets this to 2. |
perplexity | number | Neighborhood-size setting used by the documented t-SNE example. |
random_state | integer | Seed used to make the documented example reproducible. |
init | string | Initialization strategy used by the documented example. |
learning_rate | number | Optimization learning rate used by the documented example. |
A two-dimensional representation of the embedding matrix for visualization.
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.
Exploring embedding datasets
Inspecting semantic structure
Maintaining retrieval quality
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.
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.
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.
embeddingscosine similarityTSNENumPypandas