Exercise 3.6
Water Cut Trend Classifier
A rising water cut is one of the most-watched signals on any field. Even small monthly changes compound into early-life uneconomic operation. Write a classifier that turns a list of monthly water-cut values into one of three trend labels.
Write classify_wc_trend(monthly_wc) that takes a list of monthly water cuts (fractions 0–1) and returns:
"Stable": the mean month-over-month change is< 0.02in
absolute value
"Rising": the mean monthly change is between 0.02 and 0.05
(inclusive of 0.02, exclusive of 0.05)
"Rapid": the mean monthly change is> 0.05
The "monthly change" between months i and i+1 is monthly_wc[i+1] - monthly_wc[i].
Stuck? Reveal hints one at a time — they progress from nudge to near-solution.
visibilityReveal reference solutionexpand_more
Try solving it yourself first — the hints walk you through it. The solution below is one valid approach; yours may differ and still be correct.
def classify_wc_trend(monthly_wc):
diffs = [monthly_wc[i + 1] - monthly_wc[i] for i in range(len(monthly_wc) - 1)]
mean_change = sum(diffs) / len(diffs)
if abs(mean_change) < 0.02:
return "Stable"
if mean_change > 0.05:
return "Rapid"
return "Rising"
for series in [[0.10, 0.11, 0.12, 0.13],
[0.10, 0.13, 0.16, 0.20],
[0.10, 0.20, 0.32, 0.50]]:
print(series, "→", classify_wc_trend(series))
lockCopying code is a Full Access feature.