Workshop: A t-SNE Map of the Nine Wells
Learning objectives
- Draw a seeded subsample of the nine wells, standardize its six logs and map it with t-SNE, in Python and on the book's table
- Measure what a map keeps: each sample's nearest neighbours, and the distances between all pairs, for t-SNE and for PCA
- Say what near, far, the size of a group and the gap between groups mean on a t-SNE map, and what they do not
- Watch the map change with the perplexity and the seed, and say which of its features survive
A Map That Keeps Neighbours
t-SNE draws samples of many logs on a plane so that each sample's nearest neighbours in the logs stay its neighbours on the map, and it lets everything else go. On 600 samples of the Ogbon field's nine wells, its map at perplexity 30 keeps 0.589 of each sample's 10 nearest neighbours in the six standardized logs, where Section 11.1's first two principal components keep 0.311; but its distances between pairs follow the logs' with a correlation of 0.729, where PCA's follow them at 0.988. Near on a t-SNE map means alike in the logs. Far, the size of a group and the gap between two groups mean little, and they move with the perplexity and the seed. This workshop runs t-SNE in Python; the figure reads the book's precomputed maps.
The Samples
The data are the book's own learning set: Ogbon-1, the Petrophysics book's well, and eight hypothetical appraisal wells of the book's field, each 1201 samples every 0.5 ft, 10809 in all, logged by Ogbon-1's own build and the book's own tool model, all synthetic. The logs are Section 11.1's six, standardized over all 10809 samples. t-SNE compares every sample with every other, so its work grows as the square of their number: the map is drawn for 600 samples, drawn at random with seed 11, and precomputed in the book's embedding table, since a phone would take a minute or more over it. The 600 hold 220 samples of shale, 58 of the upper oil sand, 67 of the shaly oil sand, 69 of the tight limestone and 186 of the main sand. The zones colour the map; t-SNE never sees them.
Step 1: Load and Scale
The set is one CSV, the book's own, with a comment header naming every column's unit. Add log10 RT and standardize the six logs over every sample.
import numpy as np
import pandas as pd
URL = "https://ogbonlab.com/mlgeo-core/data/ogbon-learning-set.csv"
df = pd.read_csv(URL, comment="#")
df["LRT"] = np.log10(df["RT_ohmm"])
logs = ["GR_API", "RHOB_gcc", "NPHI_vv", "DT_usft", "LRT", "PEF_be"]
X_all = df[logs].to_numpy()
Z_all = (X_all - X_all.mean(axis=0)) / X_all.std(axis=0)
Step 2: A Subsample
Draw 600 rows at random and keep them in row order.
rng = np.random.default_rng(11)
idx = np.sort(rng.choice(len(df), size=600, replace=False))
Z = Z_all[idx]
zone = df["zone"].to_numpy()[idx]
NumPy's generator draws other rows than the book's kernel, which shuffles with its own seeded generator, so your 600 samples are not the figure's, and your numbers below will differ from the kernel's in their second or third decimal.
Step 3: The Map
scikit-learn's t-SNE, with the settings the book's kernel uses: perplexity 30, the affinities exaggerated 12 times early on, a learning rate of 200, a random start.
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
Y = TSNE(n_components=2, perplexity=30, early_exaggeration=12.0,
learning_rate=200.0, init="random", method="exact",
random_state=1).fit_transform(Z)
P = PCA(n_components=2).fit(Z_all).transform(Z)
The book's kernel gives, for its 600 samples, a Kullback-Leibler divergence of 0.568 after 1000 iterations at perplexity 30. scikit-learn starts and steps its own way, so its map is another drawing of the same neighbourhoods: the islands land elsewhere and turn.
Step 4: The Neighbours a Map Keeps
For each sample, take its 10 nearest neighbours in the six logs and count how many are among its 10 nearest on the map.
from sklearn.neighbors import NearestNeighbors
def nearest(A, k=10):
nn = NearestNeighbors(n_neighbors=k + 1).fit(A)
return nn.kneighbors(A, return_distance=False)[:, 1:]
def kept(A, B, k=10):
a, b = nearest(A, k), nearest(B, k)
return np.mean([len(set(a[i]) & set(b[i])) / k for i in range(len(A))])
kept_tsne, kept_pca = kept(Z, Y), kept(Z, P)
The book's kernel gives 0.589 for the t-SNE map and 0.311 for PCA's two axes: t-SNE keeps almost twice as many of each sample's neighbours.
Step 5: The Distances a Map Keeps
from scipy.spatial.distance import pdist
d = pdist(Z)
corr_tsne = np.corrcoef(d, pdist(Y))[0, 1]
corr_pca = np.corrcoef(d, pdist(P))[0, 1]
The book's kernel gives a correlation of 0.729 between the t-SNE map's pair distances and the logs', over all 179700 pairs, and 0.988 for PCA's. PCA's two axes keep the large distances and lose the small ones: they project the samples flat, so two samples apart along a dropped axis land together. t-SNE keeps the small ones and lets the large ones go, as plate (c) shows.
What the Perplexity Does
The perplexity sets how many neighbours each sample's affinities count, roughly that number: each sample's Gaussian width is found by bisection until the entropy of its affinities is the logarithm of the perplexity. At perplexity 5 the map keeps 0.529 of the neighbours and its distances follow the logs' at 0.483: it breaks the zones into many small islands. At 80 it keeps 0.500 and follows at 0.715, with fewer, tighter islands. The divergence falls from 0.784 at 5 to 0.568 at 30 and 0.255 at 80, but a smaller divergence at a larger perplexity is not a better map: it fits smoother affinities.
Sizes and Gaps
Measure a zone's spread as the mean distance between two of its samples over the mean distance between any two. In the six logs the tight limestone is spread 0.323, more than the shale's 0.226. On the map at perplexity 30 the limestone is drawn at 0.097 and the shale at 0.225; at 80, at 0.040 and 0.150. t-SNE gives every neighbourhood about the same number of neighbours, whatever its spread, so an island's size says nothing about how varied its rock is, and the gap between two islands nothing about how different they are.
Another Seed
Drawn again from seed 2 at perplexity 30, the islands move and turn, and the map keeps 0.575 of the neighbours where the first kept 0.589, its distances following the logs' at 0.763. Where an island sits, and which way up, belongs to the start, not to the rock. What survives from seed to seed is who is near whom.
UMAP
UMAP is another method that draws a map from each sample's neighbours. This book does not compute it, and what this workshop measures is how to read any such map: count the neighbours it keeps, and check its distances against the logs before you read them.
What Comes Next
Section 11.3, the chapter quiz, asks what PCA and t-SNE keep on these wells. Chapter 12 turns to the pipeline that makes a learning set like this one: from the truth to the logs, their tool model and their checks.