Workshop: A Decision Tree Classifier for Lithology
Learning objectives
- Grow decision trees on the Ogbon field's nine wells and score each depth leave one well out, in Python and on the book's table
- Read the gap between the training score and the held-out score as what a deeper tree memorises
- Choose a tree's depth by the held-out wells, not by the wells it learned from
- Show that a tree reads only the order of a log: raw and log10 resistivity give the same trees
Depth Is Capacity
A tree's depth is how many questions it may ask, and so how much it can learn: on the wells it learned from, every extra level scores better. On a well it has not seen, the score rises, peaks and then falls, because the deeper levels learn the training wells' own noise. Grown on eight of the Ogbon field's nine wells from the porosity logs and log10 RT and scored on the ninth, nine times over, trees score 0.993 on their training wells and 0.975 on the well held out at depth 6, the best of the twelve depths; at depth 12 they score 0.997 on their training wells and only 0.972 held out. The depth to use is the one the held-out wells choose. This workshop computes those curves in Python; the figure reads them from the book's precomputed table.
The Nine Wells
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, logged by Ogbon-1's own build and the book's own tool model, all synthetic. The class is pay, the Petrophysics book's net-pay rule on the generator's truth. Leave one well out makes nine folds: in each, a tree is grown on eight wells, 9608 samples, and called on the ninth, 1201. The held-out score pools the nine wells' calls, 10809 samples; the training score is each tree on its own eight wells. A random split of samples would put each well's neighbouring half-foot samples on both sides of the split and flatter every model; a new well is a new well.
Step 1: Load the Learning Set
The set is one CSV, the book's own, with a comment header naming every column's unit. Resistivity spans decades, so add its log10.
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"])
A = ["GR_API", "RHOB_gcc", "NPHI_vv", "DT_usft"]
B = A + ["LRT"]
y = df["pay"].to_numpy()
wells = df["well"].to_numpy()
Step 2: Every Depth, Leave One Well Out
For each depth, grow one tree per held-out well and count its calls on that well and on its own eight.
from sklearn.tree import DecisionTreeClassifier
def depth_scores(cols, depths=range(1, 13)):
X = df[cols].to_numpy()
out = []
for d in depths:
ok_te = ok_tr = n_tr = 0
for w in np.unique(wells):
te = wells == w
tree = DecisionTreeClassifier(
criterion="gini", max_depth=d,
random_state=0)
tree.fit(X[~te], y[~te])
p_te = tree.predict(X[te])
p_tr = tree.predict(X[~te])
ok_te += (p_te == y[te]).sum()
ok_tr += (p_tr == y[~te]).sum()
n_tr += (~te).sum()
out.append((d, ok_tr / n_tr,
ok_te / len(y)))
return out
scores_B = depth_scores(B)
The book's kernel gives, with log10 RT, 0.967 on the training wells and 0.954 held out at depth 1, 0.993 and 0.975 at depth 6, and 0.997 and 0.972 at depth 12: the training score rises at every depth, and the held-out score is lower at every depth after 6. The gap between them grows from 0.018 at depth 6 to 0.025 at depth 12. scikit-learn's tree is CART too, but it breaks ties between equal splits its own way and the CSV rounds the logs, so its third decimal may differ from the kernel's.
Step 3: Without Resistivity
scores_A = depth_scores(A)
best = max(scores_A, key=lambda s: s[2])
On the porosity logs alone the book's kernel gives its best held-out score at depth 1, 0.880, and no deeper tree does better: at depth 12, 0.911 on the training wells and 0.852 held out. The porosity logs see clean, porous rock, not the oil in it, so what the deeper splits learn about pay is only the training wells' noise.
Step 4: Raw or log10 Resistivity
scores_raw = depth_scores(A + ["RT_ohmm"])
The book's kernel gives exactly the same scores with raw RT as with log10 RT, at every depth: a split asks whether a log is above a threshold, and the logarithm keeps the order of the values, so the trees are the same trees. A model that measures distances is not so indifferent: Chapter 5's nearest neighbours, at k = 15 on the same table, score 0.932 held out with raw RT and 0.976 with log10 RT.
Step 5: The Well With No Pay
tree_d = 6
for w in np.unique(wells):
te = wells == w
X = df[B].to_numpy()
tree = DecisionTreeClassifier(
max_depth=tree_d, random_state=0)
tree.fit(X[~te], y[~te])
acc = (tree.predict(X[te]) == y[te]).mean()
print(w, round(acc, 3))
Ogbon-8 lies down the dip, wholly in the water leg: it has no pay at all, so every sample a tree calls wrong there is false pay. At depth 6 with log10 RT the book's kernel calls it right at 0.872: 154 of its 1201 samples, 77.0 ft, called pay that is not. Pooled over the nine held-out wells the calls are 2820.0 ft of pay called pay, 13.5 ft of pay missed and 121.5 ft of false pay, most of it Ogbon-8's. Without resistivity Ogbon-8 is called right at only 0.358: of its 771 samples called wrong, 614 are sandstone and 156 limestone, clean rock that reads like the oil sand on the porosity logs.
What One Tree Leaves Behind
A single tree is not the best model of pay on these wells: logistic regression on the same logs scores 0.986 held out in the same table (Chapter 4). A tree's splits are greedy and its leaves small, so a little noise in the training wells moves them. Chapter 8 grows many of Chapter 7's trees on resampled wells and lets them vote.