t-SNE for looking, not for measuring
Reduce with PCA first — t-SNE on raw high-dimensional data is slow and noisier.
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
X_pca = PCA(n_components=50, random_state=42).fit_transform(X_scaled)
emb = TSNE(
n_components=2,
perplexity=30, # roughly "how many neighbours matter"; 5-50
learning_rate="auto",
init="pca", # more stable than random init
random_state=42,
).fit_transform(X_pca)
plt.scatter(emb[:, 0], emb[:, 1], c=y, s=4, alpha=0.6, cmap="coolwarm")
# Three things this plot CANNOT tell you:
# - cluster sizes (t-SNE expands sparse regions)
# - distances between clusters (not meaningful)
# - whether clusters are real (changing perplexity changes them)
# Use it to generate hypotheses, then test them on the real features.