Workshop: The Pipeline's Last Steps in pandas
Learning objectives
- Check a delivery in pandas: its empty cells, its rows per well and its depth step
- Plant Section 12.1's sonic gap in the learning set's file
- Treat the gap three ways, dropping the rows, dropping the column and filling it, and count what each costs
- Count the memory the file's numbers take as doubles
A Gap Has No Free Treatment
The last stages of Section 12.1's pipeline, checking what arrived and treating what is missing, are a few lines of pandas, and every treatment of a gap costs something. This workshop plants Section 12.1's sonic gap in the learning set's file, Ogbon-6's sonic missing over the first 30 ft of its upper oil sand, and treats it three ways: dropping the rows takes 60 rows, every one of them pay; dropping the column takes the sonic from all nine wells; filling keeps everything and invents 60 values, which miss the file's by 5.58 us/ft as a root mean square, more than twice the tool model's own noise.
The Data
The file is the book's own synthetic learning set, loaded as Section 1.2 loads it. Its sonic is the tool model's record; the gap is planted here, so the file's own values are there to score each treatment against. Each step below gives its code and then what the book's kernel gives for the same computation on the same file.
Step 1: Check What Arrived
Empty Cells, Rows, the Depth Step
Before any model, a delivery is checked: how many cells are empty, whether every well has its rows, and whether the depths step evenly.
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="#")
print(df.isna().sum().sum())
by_well = df.groupby("well", sort=False)
print(by_well.size().unique())
step = by_well["depth_ft"].diff().round(2)
print(step.dropna().unique())
The book's kernel gives 0 empty cells, 1201 rows in every well and one depth step, 0.5 ft: the file is clean by construction.
Step 2: Plant the Gap
Section 12.1's Sonic Gap
Set the sonic to missing over Ogbon-6's first 30 ft of upper oil sand, 60 rows of 0.5 ft, in a copy of the file.
og6 = df["well"] == "Ogbon-6"
sand = og6 & (df["zone"] == "upper oil sand")
n = int(30 / 0.5)
idx = df.index[sand][:n]
raw = df.copy()
raw.loc[idx, "DT_usft"] = np.nan
print(len(idx), raw.loc[idx, "pay"].sum())
print(raw.loc[idx, "depth_ft"].agg(["min", "max"]))
The book's kernel gives 60 rows, 60 of them pay, from 5786.28 to 5815.78 ft.
Step 3: Drop the Rows
dropna
dropna() drops every row with a value missing.
a = raw.dropna()
print(a.shape, round(a["pay"].mean(), 4))
The book's kernel gives (10749, 14) and pay 0.5216 of the rows left, against 0.5243 in the whole file: the rows a gap in an oil sand takes are pay.
Step 4: Drop the Column
drop(columns=...)
Dropping the column keeps every row and loses the log everywhere.
b = raw.drop(columns="DT_usft")
print(b.shape)
The book's kernel gives (10809, 13): 10809 sonic readings lost to mend 60.
Step 5: Fill the Gap
interpolate
interpolate() fills the gap on a straight line, row by row, between the readings either side; the file's own values score the fill.
c = raw.copy()
c["DT_usft"] = c["DT_usft"].interpolate()
err = c.loc[idx, "DT_usft"] - df.loc[idx, "DT_usft"]
print(round(np.sqrt((err ** 2).mean()), 2))
print(round(err.abs().max(), 2))
The book's kernel gives 5.58 us/ft as a root mean square and 12.42 us/ft at most, against the tool model's noise of 2.5 us/ft. The line starts at the last reading above the gap, 96.97 us/ft in the shale over the sand, so it carries the shale's sonic into the sand.
Step 6: What the Numbers Take
Memory, Counted
A numeric column holds a double, 8 bytes, a row, as Section 12.1 counts.
num = df.select_dtypes("number")
print(num.shape[1], num.memory_usage(index=False).sum())
The book's kernel gives 11 numeric columns and 951192 bytes: 11 times 10809 times 8. The file holding them and the three text columns is 1016917 bytes.
What Each Treatment Costs
Dropping the rows is the default of many pipelines and is not neutral: it takes the rows the gap sits in, here pay. Dropping the column is safe for the rows and costly for every well. Filling keeps everything and is the one treatment whose cost does not show in the table's shape: 60 values no tool recorded, missing most where the gap meets a bed boundary. Whichever is chosen, it is a stage of the pipeline, and its cost is counted.