Workshop: A Random Forest for Pay on the Nine Wells

Chapter 8, Tree-Based Models II, Random Forests

Learning objectives

  • Grow a random forest on eight of the nine wells at a time and score it on the ninth, in Python and on the book's table
  • Compare the forest with one tree and with logistic regression on wells they have not seen
  • Compute a log's permutation importance on the held-out well and read it beside the impurity importance
  • Say which logs the forest needs to call pay on a new well, and why the two importances part

Which Logs, on Which Wells

Grown on eight of the Ogbon field's nine wells and called on the ninth, nine times over, a random forest of 100 trees on the porosity logs and log10 RT calls 0.976 of the held-out samples right; one tree at its best depth, 6, calls 0.975, and logistic regression 0.986. A vote cuts the trees' variance, not the bias of what every tree sees. Which logs the forest leans on has two answers. The impurity its splits remove credits log10 RT with 0.566, gamma ray with 0.227 and neutron with 0.098. Shuffling a log within the held-out well, which asks what a new well loses without it, costs 1194.0 ft of the nine wells' calls for log10 RT, 1023.5 ft for gamma ray and 2.5 ft for neutron. This workshop computes them in Python; the figure reads them from the book's precomputed forest table.

Forest Importance Nine WellsInteractive figure, enable JavaScript to interact.

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. The forest is Section 8.1's: 100 unpruned trees, each on a bootstrap, 2 logs drawn at random for each split. Leave one well out grows it on eight wells, 9608 samples, and calls the ninth, 1201, by the trees' vote, nine times.

Step 1: Load the Learning Set

The set is one CSV, the book's own, with a comment header naming every column's unit. Add log10 RT, and read the sample interval from the depths.

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()
dz = np.diff(df["depth_ft"].to_numpy()[:2])[0]

Step 2: The Forest, Out of Bag

scikit-learn's forest keeps its out-of-bag score and its impurity importances.

from sklearn.ensemble import RandomForestClassifier

def forest():
    return RandomForestClassifier(
        n_estimators=100, max_features=2,
        criterion="gini", oob_score=True,
        random_state=0)

rf = forest().fit(df[B].to_numpy(), y)
oob_B = rf.oob_score_
imp_B = rf.feature_importances_

The book's kernel gives an out-of-bag score of 0.990 with log10 RT (Section 8.1). scikit-learn draws its own bootstraps and breaks ties its own way, so its third decimal may differ from the kernel's.

Step 3: Leave One Well Out

def held_out(cols):
    X = df[cols].to_numpy()
    ok, per_well = 0, {}
    for w in np.unique(wells):
        te = wells == w
        rf = forest().fit(X[~te], y[~te])
        hit = rf.predict(X[te]) == y[te]
        ok += hit.sum()
        per_well[w] = hit.mean()
    return ok / len(y), per_well

acc_B, per_well_B = held_out(B)

The book's kernel gives 0.976 held out with log10 RT, against one tree's 0.975 at depth 6 and logistic regression's 0.986 on the same table (Chapters 7 and 4). Pooled over the nine held-out wells the calls are 2821.0 ft of pay called pay, 12.5 ft of pay missed and 117.5 ft of false pay. Ogbon-8, with no pay, is called right at 0.872 by the forest and by the tree: 154 of its samples, 77.0 ft, called pay that is not. Its wet sand fools every tree in the same way, and a vote cannot cancel a mistake they share.

Step 4: Permutation Importance on the Held-Out Well

Shuffle one log among the held-out well's samples, keep the others, and count what the forest grown without that well loses. A shuffled log keeps its values and loses its meaning.

def shuffle_cost(cols, seed=88):
    X = df[cols].to_numpy()
    rng = np.random.default_rng(seed)
    lost = np.zeros(len(cols))
    for w in np.unique(wells):
        te = wells == w
        rf = forest().fit(X[~te], y[~te])
        Xt, yt = X[te], y[te]
        base = (rf.predict(Xt) == yt).sum()
        for j in range(len(cols)):
            Xp = Xt.copy()
            Xp[:, j] = rng.permutation(Xp[:, j])
            lost[j] += base - (rf.predict(Xp) == yt).sum()
    return dict(zip(cols, lost * dz))

cost_B = shuffle_cost(B)

The book's kernel gives, with log10 RT, a cost of 1194.0 ft for log10 RT, 1023.5 ft for gamma ray, 11.0 ft for density, 3.5 ft for sonic and 2.5 ft for neutron: shuffled, log10 RT leaves 0.755 of the held-out samples right and gamma ray 0.787.

Step 5: Two Importances

The book's kernel gives the impurity importances of the forest grown on all nine wells as 0.227 for gamma ray, 0.020 for density, 0.098 for neutron, 0.088 for sonic and 0.566 for log10 RT. The two measures agree that log10 RT and gamma ray carry the call: resistivity tells oil from water, gamma ray clean rock from shale. They part on neutron and sonic, which the trees split on to fit what the training wells leave over, and which a new well does not need, because gamma ray and log10 RT already carry what they would add. The impurity importance is the training wells' answer; the shuffle on the held-out well is a new well's.

Without Resistivity

acc_A, per_well_A = held_out(A)
cost_A = shuffle_cost(A)

On the porosity logs alone the book's kernel gives the forest 0.864 held out, below one tree at depth 1, 0.880: unpruned trees on logs that cannot see oil fit the training wells' noise, and a vote does not undo all of it. Gamma ray is then the log the held-out wells lose most without, 1586.5 ft, then neutron, 382.0 ft.

What a Forest Buys

On these wells the forest is a steadier tree, not a better reader of the logs: it gives one tree's answer with less of the tree's variance. The logs decide what can be called, and the next chapter turns to them: how they are scaled and transformed, and why depth is a feature to distrust.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.