What a representation keeps, and what it throws away
Every representation maps rows to a few numbers by preserving one thing - variance, distances, neighbors, co-occurrence or links - at the expense of others. Learn to ask what a map keeps before you read it.
What runs in CodeSplash
- Principal component analysis Verified in Python and R
- t-SNE map Verified in Python and R
- Compare two embeddings (CKA, Procrustes, kNN overlap) Verified in Python and R
You will be able to
- Name what each family of representations preserves and what it gives up
- Tell parametric maps, which embed new rows, from maps of only the rows they were fitted on
- Separate a useful representation from an attractive plot, and name the checks that tell them apart
A map is a choice of what to keep
ReadA representation turns each row, document, node or signal into a short vector. Fewer numbers means something is lost, so every method decides what survives.
- Variance survives in PCA.
- Distances survive in multidimensional scaling.
- Neighbors survive in t-SNE and UMAP; far-apart distances don't.
- Co-occurrence survives in topic models.
- Links survive in graph embeddings.
Read any map by first asking which of these it was built to keep. Everything else on the picture may be an accident of the method.
The families in this track
Read| Family | Keeps | Lessons |
|---|---|---|
| Linear: PCA, ICA, factor analysis | variance, independence, shared covariance | 2–5 |
| Several views: CCA | correlation between paired tables | 6 |
| Sketches: random projection, CUR | pairwise distances, real columns | 7 |
| Parts and tensors: NMF, dictionaries | additive parts, multiway structure | 8–10 |
| Distance and manifold maps | distances, geodesics, neighbors | 11–14 |
| Topics and hidden states | word co-occurrence, state sequences | 15–17 |
| Graph embeddings | links and structural roles | 18–20 |
| Checks | agreement between two representations | 21 |
New rows or only these rows?
ReadParametric maps learn a function. PCA, ICA, CCA, NMF and kernel PCA can place a new row by applying the saved loadings or weights, so the map works on validation rows and on data that arrives next month.
Non-parametric maps place only the rows they were fitted on. t-SNE, Isomap and diffusion maps optimize coordinates directly; a new row needs an out-of-sample extension or a refit, and a refit moves every point.
That decides whether a map can feed a model, or only a picture.
Attractive is not useful
Readt-SNE map of 150 rows: trustworthiness 0.987, 80% of each row's 10 nearest neighbors kept.
- Trustworthiness (k = 10)
- 0.9874
- Neighbors kept
- 80.0%
Plots
View data
| species | t-SNE 1 | t-SNE 2 |
|---|---|---|
| setosa | −17.485 | 0.6277 |
| setosa | −14.167 | 0.9543 |
| setosa | −15.036 | 0.1135 |
| setosa | −14.400 | 0.0318 |
| setosa | −17.986 | 0.1561 |
| setosa | −19.595 | 0.6255 |
| setosa | −16.058 | −0.4235 |
| setosa | −16.617 | 0.5462 |
| setosa | −13.461 | 0.2454 |
| setosa | −14.742 | 0.8895 |
| setosa | −18.809 | 1.042 |
| setosa | −16.334 | 0.0119 |
| setosa | −14.052 | 0.6368 |
| setosa | −13.740 | −0.2452 |
| setosa | −20.066 | 1.079 |
| setosa | −20.596 | 0.6642 |
| setosa | −19.602 | 0.6767 |
| setosa | −17.469 | 0.6009 |
| setosa | −19.452 | 1.238 |
| setosa | −19.035 | 0.1696 |
| setosa | −17.292 | 1.572 |
| setosa | −18.582 | 0.3315 |
| setosa | −17.436 | −0.8033 |
| setosa | −16.229 | 1.292 |
| setosa | −16.319 | −0.0289 |
| setosa | −14.304 | 1.205 |
| setosa | −16.638 | 0.5695 |
| setosa | −17.637 | 0.9233 |
| setosa | −16.942 | 1.123 |
| setosa | −15.077 | 0.1450 |
| setosa | −14.660 | 0.5266 |
| setosa | −17.262 | 1.586 |
| setosa | −20.067 | 0.2069 |
| setosa | −20.339 | 0.5875 |
| setosa | −14.767 | 0.8968 |
| setosa | −15.366 | 0.9621 |
| setosa | −17.999 | 1.507 |
| setosa | −17.888 | −0.1899 |
| setosa | −13.822 | −0.0449 |
| setosa | −16.781 | 0.8663 |
| setosa | −17.293 | 0.2274 |
| setosa | −12.353 | 0.4452 |
| setosa | −14.607 | −0.4924 |
| setosa | −17.300 | −0.0602 |
| setosa | −19.130 | 0.0106 |
| setosa | −14.054 | 0.6586 |
| setosa | −19.035 | 0.1477 |
| setosa | −14.894 | −0.1504 |
| setosa | −18.706 | 0.8493 |
| setosa | −15.982 | 0.7493 |
| versicolor | 10.220 | −2.409 |
| versicolor | 9.667 | −1.459 |
| versicolor | 10.306 | −2.145 |
| versicolor | 2.346 | −0.8474 |
| versicolor | 7.986 | −1.192 |
| versicolor | 4.848 | 0.1438 |
| versicolor | 10.854 | 1.761 |
| versicolor | 1.676 | 0.0775 |
| versicolor | 8.255 | −1.806 |
| versicolor | 3.211 | 0.8110 |
First 60 of 150 rows.
The exact R script · R R version 4.5.1 (2025-06-13)
# t-SNE map
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method discover.tsne 1.0.0 · spec a810565fc549888c
# Dataset: ml_iris (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
suppressPackageStartupMessages(library(Rtsne))
# %% Data
IMPUTE <- "median"
SCALE <- "standard"
FEATURES <- c("sepal_length", "sepal_width", "petal_length", "petal_width")
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/ml_iris.parquet", col_select = FEATURES))
warnings <- list()
design <- cs_ml_design(
df, FEATURES,
kinds = list(
sepal_length = "numeric",
sepal_width = "numeric",
petal_length = "numeric",
petal_width = "numeric"
),
levels = list(
),
impute = IMPUTE,
scale = SCALE
)
fitted <- cs_ml_design_fit(design, df)
X <- unname(cs_ml_design_apply(fitted, df)) # numbers imputed / scaled; categories one-hot (missing -> all zero)
rows <- seq_len(nrow(df)) - 1L
LABEL <- "species"
if (!is.null(LABEL)) df[[LABEL]] <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/ml_iris.parquet", col_select = LABEL))[[LABEL]]
# %% t-SNE (same start and schedule as scikit-learn)
n <- length(rows)
perplexity <- min(30, (n - 1) / 3 - 1e-6)
learning_rate <- max(n / 12 / 4, 50)
init <- cs_ml_pca_init(X, -1e-4) # PCA start, first column SD 1e-4
set.seed(42)
fit <- Rtsne::Rtsne(
X,
dims = 2,
perplexity = perplexity,
theta = 0.5,
max_iter = 1000,
pca = FALSE,
check_duplicates = FALSE,
Y_init = init,
stop_lying_iter = 250L, # keep early exaggeration with a given start (as scikit-learn)
mom_switch_iter = 250L,
momentum = 0.5,
final_momentum = 0.8,
eta = learning_rate,
exaggeration_factor = 12,
num_threads = 1
)
Y <- fit$Y
METHOD_LABEL <- "t-SNE"
ENGINE_PACKAGES <- c("Rtsne", "nanoparquet")
extra_values <- list(perplexity = perplexity)
# %% Neighborhood preservation (same rows in both engines)
K_EVAL <- 10L
sample <- cs_ml_sample_rows(length(rows))
k_eval <- min(K_EVAL, length(sample) - 2L)
trust <- cs_ml_trustworthiness(X[sample, , drop = FALSE], Y[sample, , drop = FALSE], k_eval)
keep <- cs_ml_knn_preservation(X[sample, , drop = FALSE], Y[sample, , drop = FALSE], k_eval)
# %% Artifacts
out <- cs_ml_out_dir("target/academy-parity/run-8142/discover.tsne/iris/out-r")
nanoparquet::write_parquet(data.frame(row = as.integer(rows), dim_1 = Y[, 1], dim_2 = Y[, 2]), file.path(out, "embedding.parquet"))
# %% Result
values <- c(list(n = length(rows), n_features = length(FEATURES), trustworthiness = trust, knn_preservation = keep), extra_values)
pick <- cs_ml_sample_rows(length(rows), 5000)
groups <- if (is.null(LABEL)) rep("All rows", length(pick)) else {
g <- as.character(df[[LABEL]][rows[pick] + 1L]); g[is.na(g)] <- "(missing)"; g
}
cs_publish(list(
title = "t-SNE map",
engine = do.call(cs_engine_info, as.list(ENGINE_PACKAGES)),
n = length(rows),
summary = sprintf("%s map of %d rows: trustworthiness %.3f, %.0f%% of each row's %d nearest neighbors kept.",
METHOD_LABEL, length(rows), trust, 100 * keep, k_eval),
statistics = list(list(key = "trustworthiness", label = sprintf("Trustworthiness (k = %d)", k_eval), value = trust),
list(key = "knn_preservation", label = "Neighbors kept", value = keep, format = "percent")),
charts = list(cs_chart("map", "cluster-scatter", paste(METHOD_LABEL, "map"), list(
x = cs_col(paste(METHOD_LABEL, 1), Y[pick, 1], "numeric"),
y = cs_col(paste(METHOD_LABEL, 2), Y[pick, 2], "numeric"),
cluster = cs_col(if (is.null(LABEL)) "Group" else LABEL, groups, "categorical")
), note = if (length(pick) == length(rows)) NULL else sprintf("%d of %d rows shown.", length(pick), length(rows)))),
values = values,
warnings = warnings,
artifacts = list(list(key = "embedding", path = file.path(out, "embedding.parquet"), format = "parquet"))
))
t-SNE map of 150 rows: trustworthiness 0.986, 79% of each row's 10 nearest neighbors kept.
- Trustworthiness (k = 10)
- 0.9864
- Neighbors kept
- 79.4%
To keep this page light, the plots are shown once, under the R tab.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# t-SNE map
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method discover.tsne 1.0.0 · spec a810565fc549888c
# Dataset: ml_iris (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import os
import numpy as np
import pandas as pd
from sklearn.manifold import TSNE
import codesplash as cs
from codesplash import ml as csml
from codesplash.ml import embed as csembed
# %% Data
IMPUTE = "median"
SCALE = "standard"
FEATURES = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
df = pd.read_parquet("tests/parity/fixtures/ml_iris.parquet", columns=FEATURES)
warnings = []
design = csml.Design.for_frame(
df,
FEATURES,
kinds={
"sepal_length": "numeric",
"sepal_width": "numeric",
"petal_length": "numeric",
"petal_width": "numeric",
},
levels={
},
impute=IMPUTE,
scale=SCALE,
)
fitted = design.fit(df)
X = fitted.transform(df) # numbers imputed / scaled; categories one-hot (missing → all zero)
rows = np.arange(len(df))
LABEL = "species"
if LABEL:
df[LABEL] = pd.read_parquet("tests/parity/fixtures/ml_iris.parquet", columns=[LABEL])[LABEL]
# %% t-SNE (same start and schedule as Rtsne)
n = len(rows)
perplexity = min(30, (n - 1) / 3 - 1e-6) # Rtsne requires 3 · perplexity < n − 1
learning_rate = max(n / 12 / 4, 50.0) # scikit-learn's "auto", pinned for both engines
init = csembed.pca_init(X, -1e-4) # PCA start, first column SD 1e-4
tsne = TSNE(
n_components=2,
perplexity=perplexity,
early_exaggeration=12.0,
learning_rate=learning_rate,
max_iter=1000,
angle=0.5,
method="barnes_hut",
init=init,
random_state=42,
n_jobs=1,
)
Y = tsne.fit_transform(X)
METHOD_LABEL = "t-SNE"
ENGINE_PACKAGES = ("scikit-learn", "numpy", "pandas")
extra_values = {"perplexity": perplexity}
# %% Neighborhood preservation (same rows in both engines)
K_EVAL = 10
sample = csembed.sample_rows(len(rows))
k_eval = min(K_EVAL, len(sample) - 2)
trust = csembed.trustworthiness(X[sample], Y[sample], k_eval)
keep = csembed.knn_preservation(X[sample], Y[sample], k_eval)
# %% Artifacts
out = csml.out_dir("target/academy-parity/run-8142/discover.tsne/iris/out-python")
emb = pd.DataFrame({"row": rows.astype(np.int64), "dim_1": Y[:, 0], "dim_2": Y[:, 1]})
emb.to_parquet(os.path.join(out, "embedding.parquet"), index=False)
# %% Result
values = {"n": len(rows), "n_features": len(FEATURES), "trustworthiness": trust, "knn_preservation": keep}
values.update(extra_values)
pick = csembed.sample_rows(len(rows), 5000)
groups = [("All rows" if LABEL is None else ("(missing)" if pd.isna(v) else str(v))) for v in (df[LABEL].iloc[rows[pick]] if LABEL else [None] * len(pick))]
cs.publish({
"title": "t-SNE map",
"engine": cs.engine_info(*ENGINE_PACKAGES),
"n": len(rows),
"summary": f"{METHOD_LABEL} map of {len(rows)} rows: trustworthiness {trust:.3f}, {keep:.0%} of each row's {k_eval} nearest neighbors kept.",
"statistics": [
{"key": "trustworthiness", "label": f"Trustworthiness (k = {k_eval})", "value": trust},
{"key": "knn_preservation", "label": "Neighbors kept", "value": keep, "format": "percent"},
],
"charts": [cs.chart("map", "cluster-scatter", f"{METHOD_LABEL} map", {
"x": cs.col(f"{METHOD_LABEL} 1", Y[pick, 0], "numeric"),
"y": cs.col(f"{METHOD_LABEL} 2", Y[pick, 1], "numeric"),
"cluster": cs.col(LABEL or "Group", groups, "categorical"),
}, note=None if len(pick) == len(rows) else f"{len(pick):,} of {len(rows):,} rows shown.")],
"values": values,
"warnings": warnings,
"artifacts": [{"key": "embedding", "path": os.path.join(out, "embedding.parquet"), "format": "parquet"}],
})
The t-SNE map of iris keeps neighborhoods well: trustworthiness 0.987. That number says rows close on the map were close in the data. It says nothing about the gaps between the islands, their sizes, or whether the islands are real groups.
A useful representation passes checks that don't depend on how the picture looks: neighborhood scores across sizes, reconstruction on held-out rows, stability across seeds and subsamples, and performance on a downstream task.
Readiness across the track
ReadMost methods in this track run in CodeSplash, under Discover, in Python and R with matching results. Some run as an option of a broader method; a few are explained here only (sparse coding, tensor trains, maximum variance unfolding), and some need image stacks or multi-table data CodeSplash doesn't load. Each lesson says which, and the readiness badge on the page shows the current label for every entry it covers.
Check: what a map keeps
CheckpointCheck yourself
You need a map whose distances between far-apart groups are meaningful. Which family is built for that?
Show the answer
Answer: b
t-SNE and UMAP keep neighbors and let far distances bend. MDS keeps distances and PCA keeps variance, so their global layout carries meaning.
Check yourself
You want to fit a map on training rows and place next month's rows on the same map. Which is the better fit?
Show the answer
Answer: a
PCA learns a function; t-SNE learns coordinates for the fitted rows only, and refitting moves every point.
Check yourself · choose all that apply
Which checks tell a useful representation from an attractive plot? Pick all that apply.
Show the answer
Answer: a, b, c
Clean islands are what every method is good at producing. The other three can fail even when the picture looks good.
Recap
Recap- Each representation preserves one thing: variance, distances, neighbors, co-occurrence or links.
- Parametric maps embed new rows; coordinate-only maps describe the fitted rows.
- Judge a map by checks independent of its looks: neighborhoods, reconstruction, stability, downstream use.
Concepts in this lesson