MCPcopy Index your code
hub / github.com/CommonstackAI/UncommonRoute / stratified_3way_split

Function stratified_3way_split

scripts/split_data.py:17–47  ·  view source on GitHub ↗
(
    rows: list[dict[str, Any]],
    seed: int = 42,
    train_frac: float = 0.64,
    cal_frac: float = 0.16,
)

Source from the content-addressed store, hash-verified

15
16
17def stratified_3way_split(
18 rows: list[dict[str, Any]],
19 seed: int = 42,
20 train_frac: float = 0.64,
21 cal_frac: float = 0.16,
22) -> tuple[list[dict[str, Any]], list[dict[str, Any]], list[dict[str, Any]]]:
23 rng = random.Random(seed)
24 strata: dict[str, list[dict[str, Any]]] = defaultdict(list)
25 for row in rows:
26 key = f"{row.get('benchmark', 'unknown')}_{row.get('target_tier', 'unknown')}"
27 strata[key].append(row)
28
29 train, cal, holdout = [], [], []
30 for key in sorted(strata.keys()):
31 group = list(strata[key])
32 rng.shuffle(group)
33 n = len(group)
34 n_train = max(1, round(n * train_frac))
35 n_cal = max(0, round(n * cal_frac))
36 if n > 1:
37 n_holdout = n - n_train - n_cal
38 if n_holdout < 1:
39 n_cal = max(0, n_cal - 1)
40 train.extend(group[:n_train])
41 cal.extend(group[n_train:n_train + n_cal])
42 holdout.extend(group[n_train + n_cal:])
43
44 rng.shuffle(train)
45 rng.shuffle(cal)
46 rng.shuffle(holdout)
47 return train, cal, holdout
48
49
50def main():

Callers 5

test_split_sizesFunction · 0.90
test_split_no_overlapFunction · 0.90
test_split_deterministicFunction · 0.90
test_split_stratifiedFunction · 0.90
mainFunction · 0.85

Calls 2

getMethod · 0.45
appendMethod · 0.45

Tested by 4

test_split_sizesFunction · 0.72
test_split_no_overlapFunction · 0.72
test_split_deterministicFunction · 0.72
test_split_stratifiedFunction · 0.72