06 — Personal Analytics / Fantasy

Smart Pitch + Fantasy XI

A Fantasy Cricket system: rule-based venue tagging from historical scoring patterns, a faithful Dream11-schema fantasy points calculator, and an Integer Linear Program that selects the optimal 11-player team under credit constraints. The notebook also includes two ML experiments — both kept in the writeup as a teaching moment on target leakage.

DomainFantasy Cricket
ApproachRules + ILP
SolverPuLP · CBC
StatusHonest cut
01 / Read This First

The honest framing.

⚠ Important context before the rest of the page

This is the hardest project on my portfolio to write up honestly. The notebook contains two ML models with target leakage — they predict labels that are deterministic functions of their own input features. Their reported metrics (Accuracy 1.0, R² 0.998) are the textbook tells.

Rather than hide them or fix them silently, I'm leaving them in and explaining exactly what went wrong, because the habit they cost me to learn — if your metric is too good, audit the feature-target dependence — is more valuable than any single result.

The genuine engineering in this project is the rule-based venue tagger, the Dream11-schema fantasy points calculator, and the PuLP LP for Fantasy XI selection. Lead with those. The ML experiments are a story about a mistake.

02 / What's In The Box

Three working pieces. Two broken ones.

Works — Rule-Based

Venue Tagger

Three if/then rules on first/second-innings run rate and wicket rate classify IPL grounds as Batting-friendly, Bowling-friendly, or Neutral. Not ML — a deterministic heuristic. Useful as a feature for downstream tasks.

Works — Schema

Dream11 Calculator

Faithful implementation of the actual Dream11 fantasy scoring rules: runs, fours, sixes, milestone bonuses, duck penalty, wickets, wicket-haul bonuses. Real engineering, real schema.

Works — LP

Fantasy XI Optimizer

PuLP Integer Linear Program: 11 players, credit cap of 100, role minimums. Decision variables are binary. CBC solves in under a second. Has a string-matching bug in the role constraints — flagged honestly below.

03 / The Real Engineering

The fantasy points schema, faithfully implemented.

The Dream11 fantasy scoring system has dozens of components. This calculator implements the core ones: runs scoring, boundary bonuses, milestone bonuses (30+, 50+, 100+), wicket points, and wicket-haul bonuses (3 wickets, 4+ wickets). The duck penalty fires on a zero score with balls faced. Per-player-per-match.

▸ Batting Points ◂
batting_pts = runs · 1 // 1 pt per run
           + fours · 1 // boundary bonus
           + sixes · 2 // six bonus
           + 10 if 30 ≤ runs < 50 // half-fifty bonus
           + 20 if 50 ≤ runs < 100 // fifty bonus
           + 30 if runs ≥ 100 // century bonus
           − 2 if runs == 0 // duck penalty
▸ Bowling Points ◂
bowling_pts = wickets · 25 // 25 pts per wicket
            + 10 if wickets == 3 // 3-fer
            + 20 if wickets ≥ 4 // 4-fer+
04 / How It Works

From ball-by-ball to optimal XI.

01

Ingest and join three datasets

Deliveries, matches, and player style metadata joined on match_id and bowler. Result: every delivery tagged with venue, season, toss, winner, and the bowler's style (pace/spin/all-rounder).

pandasthree-way merge
02

Engineer venue-level features

Per ground: average runs per over, wicket rate, first/second-innings run rate split, pace vs spin wicket counts, phase-wise run distribution (PP/Middle/Death). These are genuinely useful features — clean, interpretable, reusable.

groupbybowling-style classifierphase tagging
03

Tag venues with rule-based classifier

Three conditions: Batting-friendly if both innings RR > 9 and wicket rate < league average. Bowling-friendly if both innings RR < 8 and wicket rate > league average. Neutral otherwise. Deterministic — same input always gives same label. Not ML.

if/then rulesdomain heuristic
04

Compute per-match fantasy points

Group ball-by-ball by (match_id, player) to get batting and bowling lines per player. Apply the Dream11 schema above. Faithful implementation of the actual fantasy scoring rules.

Dream11 schemagroupby aggregation
05

Formulate the Fantasy XI LP

One binary decision variable per player. Linear objective: maximize total fantasy points. Linear constraints: exactly 11 players, total credits ≤ 100, role minimums for bowlers / all-rounders / WK / batsmen. Caveat: the role constraints have a string-matching bug — flagged in the limitations.

PuLPbinary variablescredit cap
06

Solve with CBC

Branch-and-bound on the LP relaxation. For a problem this size, sub-second convergence. Returns an exact-optimal Fantasy XI under the active constraints.

CBC solverbranch-and-bound
05 / Results

The genuine numbers.

Datasets Joined
3
Deliveries × matches × player style metadata, joined on multiple keys.
Pace Wickets
7.7k
Total pace bowler wickets across IPL history. Bowling-style classified from raw strings.
Spin Wickets
3.9k
Total spin bowler wickets — roughly half the pace count. Useful for venue-type signal.
Fantasy XI Players
11
LP-selected team under a 100-credit cap. Output below.
Top Pick (LP)
DAJ Bracewell
98 avg fantasy points — small-sample artifact, same problem as the Impact Index LP.
LP Solve Time
<1s
CBC converges quickly. The LP isn't the slow part — the bug is in its constraints.
06 / Technical Decisions

Why these choices.

Why rule-based
venue tagging?
A coach can read three if/then rules. The label is deterministic — same venue features always give the same tag — so it's reproducible and auditable. The honest weakness is that the thresholds (RR > 9, RR < 8) aren't calibrated; they're domain heuristics. V2 calibrates them against held-out match outcomes.
Why faithful schema
over a learned model?
Dream11 fantasy points are defined by a formula. There's no prediction problem — it's pure schema. The right engineering is implementing the formula correctly, not training a model to recover it. The fact that I did later train a model to recover it (and got R² 0.998 from feature leakage) is exactly why this section matters: knowing when not to use ML is itself a skill.
Why PuLP
over heuristics?
Same reason as the Impact Index project — ILP guarantees the optimum under constraints, greedy heuristics don't. The Fantasy XI problem is binary decision variables with a linear objective and linear constraints. Textbook ILP. CBC solves it in under a second.
Why keep the
broken ML models in?
Because the lesson is more valuable than the result. Target leakage doesn't announce itself — it produces models with great metrics. Being able to spot it in your own work is a skill, and the only way to demonstrate I have it is to show it happening and call it out. A portfolio that hides its failures is less informative than one that owns them.
07 / The ML Mistake

When your model is too good, audit your features.

⚠ Two cases of target leakage

The textbook tells — Accuracy 1.0, R² 0.998 — are not flexes.

The notebook contains two ML models. Both report metrics that look elite. Both are reverse-engineering their own labeling functions. Here's exactly what went wrong, why the metrics are misleading, and what the honest version of each task would look like.

CASE 01 — PITCH TYPE CLASSIFIER

Predicting venue_type from the variables that define venue_type

Reported: Accuracy 1.0 · F1 1.0 · CV 0.9318 · n_test = 24

The label venue_type is computed from a three-condition rule on first_innings_rr, second_innings_rr, and wicket_rate. Those exact three columns are then passed as features to a RandomForest and an XGBoost classifier.

venue_type = f(first_innings_rr, second_innings_rr, wicket_rate)
features ⊇ {first_innings_rr, second_innings_rr, wicket_rate}
→ model learns f, not pitch behavior

The honest version: predict pitch type from features that don't define it — weather conditions, soil type, days since the last match, time of year, time of day. That's a real prediction problem with no leakage.

CASE 02 — FANTASY POINTS REGRESSOR

Predicting total_fantasy_points from the variables that compute total_fantasy_points

Reported: R² 0.9977 · MAE 0.328

The target is literally 25 × wickets + 1 × runs + 1 × fours + 2 × sixes + bonus(runs). The regressor's features include wickets, runs_scored, fours, sixes. The model learns the linear combination of its own inputs.

fantasy_pts = 25·wickets + 1·runs + 1·fours + 2·sixes + bonus(runs)
features ⊇ {runs, fours, sixes, wickets}
→ model recovers the schema arithmetic

The MAE of 0.328 on integer-stepped fantasy points is the giveaway. You don't get a sub-1 MAE on integer targets unless you're recovering the formula directly.

The honest version: predict expected next-match fantasy points from pre-match features only — recent form, opponent quality, venue type, pitch report. That's a forecasting task with real uncertainty.

— The takeaway —
If you can write the label as a function of the features in under five lines of pseudocode, you don't have a prediction problem. You have a relabeling problem. Check before training, not after.
08 / Other Limitations

What else needs fixing.

Beyond the leakage issue, the gaps between this and a production-grade fantasy cricket tool.

01
LP role constraints silently fail. The role strings in the player metadata have trailing spaces ('Bowler ' not 'Bowler') and inconsistent naming ('Batsman' vs the code's 'Batter'). The check 'Bowler' in role_counts evaluates False, so the constraints never get added to the LP. Output shows a player with role None — the tell.
02
Credits are synthetic. Generated via np.random.uniform over buckets derived from average fantasy points — circular dependence between cost and value. Real Dream11 credits are exogenous; these aren't.
03
Small-sample players dominate the LP output. DAJ Bracewell at 98 avg fantasy points, Vivrant Sharma at 88 — likely a handful of matches with one strong performance inflating the mean. Same root cause as the Impact Index LP. Fix: minimum-games filter.
04
Dream11 schema is incomplete. No maiden over bonus (+12), no dot ball bonus (+1), no economy rate bonus/penalty, no fielding points (catches, run-outs, stumpings).
05
Venue tagging thresholds aren't calibrated. RR > 9 for Batting-friendly and RR < 8 for Bowling-friendly are domain heuristics. V2 calibrates them against held-out match outcomes.
06
Pitch dataset has duplicate rows per venue. Each venue appears twice (once per inning) with identical labels. Train/test split likely places the same venue on both sides — another layer of leakage on top of the feature-target dependence.
07
Closing notebook summary claims a CNN. It doesn't exist — only the RandomForest classifier. The summary is aspirational, not descriptive. Rewriting that summary is part of the V2 cleanup.
Version 2 — Build Order

What ships next.

  • 01Reframe both ML tasks as forecasting problems with pre-match features only — no more recovering the formula.
  • 02Fix the LP role string matching: .str.strip() + normalize 'Batter' / 'Batsman' / 'WK-Batsman'.
  • 03Source actual Dream11 credit values; drop the synthetic-credit generator entirely.
  • 04Add minimum-games filter (≥ 30 matches) to the Fantasy XI eligible pool.
  • 05Complete the Dream11 schema: maiden overs, dot balls, economy bonus, fielding points.
  • 06Calibrate the venue tagger thresholds against held-out match scoring distributions.
  • 07Rewrite the notebook summary to describe what was actually built — no aspirational CNN claims.
Read the code / Audit the bug

A portfolio that owns its failures beats one that hides them.

Full notebook on GitHub — the working pieces, the broken ML models, and exactly what target leakage looks like.