-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhar_pipeline_v64_professional_final_push.py
More file actions
442 lines (378 loc) · 16.4 KB
/
Copy pathhar_pipeline_v64_professional_final_push.py
File metadata and controls
442 lines (378 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
V64 Professional Final Push
===========================
Assignment 3: Human Activity Recognition (HAR)
Course: Data Mining, Spring 2026
Purpose
-------
This script creates a final, reproducible set of Kaggle submission candidates
from the strongest validated branch.
Current empirical situation:
- `3->1` cleanup improved the public leaderboard:
* V57 mixed minority top35 : ~0.8128
* V59 plus_3to1_top3 : ~0.8138
* V61 add_3to1_next5 : ~0.8150
* V63/V61 add_3to1_next8 equivalent: ~0.8154
- Therefore, the final push should continue the validated `3->1` transition
carefully, with small increments and controlled fallback combinations.
Professional/reproducibility guarantees:
1. No manual editing by test ID.
2. All outputs are derived deterministically from:
- sample_submission.csv
- base_csv
- current_best_csv
- model-produced candidate ranking
3. Every output is validated:
- exactly columns `Id,Label`
- IDs aligned to sample_submission.csv
- labels restricted to 0..5
4. The script writes:
- submission CSV files
- change audit files
- manifest CSV/JSON
- report-ready Markdown summary
Recommended run
---------------
PowerShell:
python har_pipeline_v64_professional_final_push.py `
--sample_submission "nycu-data-mining-assignment-3\\sample_submission.csv" `
--base_csv "submission_v54_ttw_brave.csv" `
--current_best_csv "SUBMIT_v63_01_more_3to1_x3.csv" `
--current_best_score 0.8154 `
--v57_ranking "submission_v57_minority_candidate_ranking.csv" `
--output_prefix "submission_v64_final"
Recommended adaptive order:
1. submission_v64_final_more_3to1_x2.csv
2. If #1 improves/flats: submission_v64_final_more_3to1_x4.csv
3. If pure 3->1 saturates: submission_v64_final_combo_3to1x2_5to1x1.csv
"""
from __future__ import annotations
import argparse
import json
import re
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Dict, Iterable, List, Sequence, Tuple
import numpy as np
import pandas as pd
N_CLASSES = 6
VALID_LABELS = set(range(N_CLASSES))
def digits(x):
m = re.findall(r"\d+", str(x))
return int(m[-1]) if m else None
def find_col(cols: Sequence[str], names: Sequence[str]):
low = {str(c).lower().strip(): c for c in cols}
for n in names:
if n.lower() in low:
return low[n.lower()]
return None
def class_counts(labels: Sequence[int]) -> List[int]:
return np.bincount(np.asarray(labels, dtype=int), minlength=N_CLASSES).astype(int).tolist()
def read_submission(path: str | Path) -> pd.DataFrame:
path = Path(path)
df = pd.read_csv(path)
id_col = find_col(df.columns, ["Id", "id", "file_id"])
label_col = find_col(df.columns, ["Label", "label", "target", "activity"])
if id_col is None or label_col is None:
raise ValueError(f"{path} must contain Id and Label columns. Found columns={list(df.columns)}")
out = df[[id_col, label_col]].copy()
out.columns = ["Id", "Label"]
out["Id"] = out["Id"].map(digits).astype(int)
out["Label"] = pd.to_numeric(out["Label"], errors="raise").astype(int)
return out
def validate_against_sample(sub: pd.DataFrame, sample: pd.DataFrame, name: str) -> pd.DataFrame:
if list(sub.columns) != ["Id", "Label"]:
raise ValueError(f"{name}: expected exactly columns ['Id','Label'], got {list(sub.columns)}")
if sub["Id"].duplicated().any():
dupes = sub.loc[sub["Id"].duplicated(), "Id"].head(10).tolist()
raise ValueError(f"{name}: duplicated Ids {dupes}")
sample_ids = sample["Id"].astype(int).tolist()
sub_ids = sub["Id"].astype(int).tolist()
if set(sample_ids) != set(sub_ids):
missing = sorted(set(sample_ids) - set(sub_ids))[:10]
extra = sorted(set(sub_ids) - set(sample_ids))[:10]
raise ValueError(f"{name}: ID mismatch. missing={missing}, extra={extra}")
invalid = sorted(set(sub["Label"].astype(int)) - VALID_LABELS)
if invalid:
raise ValueError(f"{name}: invalid labels {invalid}; expected 0..5")
aligned = sample[["Id"]].merge(sub, on="Id", how="left")
if aligned["Label"].isna().any():
raise ValueError(f"{name}: missing labels after alignment")
aligned["Label"] = aligned["Label"].astype(int)
return aligned[["Id", "Label"]]
def read_ranking(path: str | Path) -> pd.DataFrame:
path = Path(path)
r = pd.read_csv(path)
id_col = find_col(r.columns, ["Id", "id", "file_id"])
if id_col is None:
raise ValueError(f"{path} must contain Id/id/file_id. Found columns={list(r.columns)}")
r["Id"] = r[id_col].map(digits).astype(int)
for c in ["old_label", "new_label"]:
if c not in r.columns:
raise ValueError(f"{path} must contain {c}")
r[c] = pd.to_numeric(r[c], errors="raise").astype(int)
if "transition" not in r.columns:
r["transition"] = r["old_label"].astype(str) + "->" + r["new_label"].astype(str)
for c in ["final_score", "policy_score", "agreement_new_frac", "guide_margin", "main_margin"]:
if c not in r.columns:
r[c] = 0.0
r[c] = pd.to_numeric(r[c], errors="coerce").fillna(0.0)
# Deterministic ordering using model evidence.
r = r.sort_values(
["final_score", "policy_score", "agreement_new_frac", "guide_margin", "main_margin", "Id"],
ascending=[False, False, False, False, False, True],
).reset_index(drop=True)
r["rank_pos"] = np.arange(len(r))
return r
def mapping_from_sub(sub: pd.DataFrame) -> Dict[int, int]:
return dict(zip(sub["Id"].astype(int), sub["Label"].astype(int)))
def changed_ids(reference: Dict[int, int], other: Dict[int, int]) -> set[int]:
return {int(i) for i in reference if int(reference[i]) != int(other.get(i, reference[i]))}
def transition_counts(reference: Dict[int, int], labels: Dict[int, int], ids: Sequence[int]) -> Dict[str, int]:
trans = []
for idv in ids:
old = int(reference[idv])
new = int(labels[idv])
if old != new:
trans.append(f"{old}->{new}")
return pd.Series(trans).value_counts().astype(int).to_dict() if trans else {}
def add_transition(
labels_map: Dict[int, int],
ranking: pd.DataFrame,
transition: str,
k: int,
avoid_ids: Iterable[int],
) -> Tuple[Dict[int, int], List[int]]:
labels = dict(labels_map)
selected: List[int] = []
avoid = set(int(x) for x in avoid_ids)
sub = ranking[ranking["transition"] == transition].copy()
for _, row in sub.iterrows():
idv = int(row["Id"])
old = int(row["old_label"])
new = int(row["new_label"])
if idv in avoid:
continue
if labels.get(idv) != old:
continue
if old == new:
continue
labels[idv] = new
selected.append(idv)
avoid.add(idv)
if len(selected) >= k:
break
return labels, selected
def add_plan(
labels_map: Dict[int, int],
ranking: pd.DataFrame,
plan: Sequence[Tuple[str, int]],
avoid_ids: Iterable[int],
) -> Tuple[Dict[int, int], List[Tuple[str, int]]]:
labels = dict(labels_map)
avoid = set(int(x) for x in avoid_ids)
selected_all: List[Tuple[str, int]] = []
for transition, k in plan:
labels, selected = add_transition(labels, ranking, transition, k, avoid)
for idv in selected:
selected_all.append((transition, idv))
avoid.update(selected)
return labels, selected_all
@dataclass
class VariantRecord:
variant_name: str
file: str
changes_vs_base: int
changes_vs_current_best: int
counts: List[int]
transitions_vs_base: Dict[str, int]
note: str
def save_variant(
sample: pd.DataFrame,
base_map: Dict[int, int],
current_map: Dict[int, int],
labels_map: Dict[int, int],
variant_name: str,
output_prefix: str,
note: str,
) -> VariantRecord:
ids = sample["Id"].astype(int).tolist()
labels = np.array([int(labels_map[i]) for i in ids], dtype=int)
out = pd.DataFrame({"Id": ids, "Label": labels})
out = validate_against_sample(out, sample, variant_name)
path = f"{output_prefix}_{variant_name}.csv"
out.to_csv(path, index=False)
base_arr = np.array([int(base_map[i]) for i in ids], dtype=int)
cur_arr = np.array([int(current_map[i]) for i in ids], dtype=int)
changes_vs_base = int(np.sum(base_arr != labels))
changes_vs_current = int(np.sum(cur_arr != labels))
cnt = class_counts(labels)
change_rows = []
for i, idv in enumerate(ids):
base_label = int(base_map[idv])
current_label = int(current_map[idv])
variant_label = int(labels_map[idv])
if base_label != variant_label or current_label != variant_label:
change_rows.append({
"Id": idv,
"base_label": base_label,
"current_best_label": current_label,
"variant_label": variant_label,
"transition_vs_base": f"{base_label}->{variant_label}" if base_label != variant_label else "unchanged",
"changed_vs_current_best": int(current_label != variant_label),
})
pd.DataFrame(change_rows).to_csv(f"{output_prefix}_{variant_name}_changes.csv", index=False)
rec = VariantRecord(
variant_name=variant_name,
file=path,
changes_vs_base=changes_vs_base,
changes_vs_current_best=changes_vs_current,
counts=cnt,
transitions_vs_base=transition_counts(base_map, labels_map, ids),
note=note,
)
print(
f"WROTE {path:58s} "
f"base_changes={changes_vs_base:3d} current_diff={changes_vs_current:2d} "
f"counts={cnt} {note}"
)
return rec
def write_report_markdown(
path: str | Path,
args,
base_counts: List[int],
current_counts: List[int],
current_transitions: Dict[str, int],
records: List[VariantRecord],
):
text = []
text.append("# V64 Professional Final Push - HAR Assignment 3\n\n")
text.append("## Purpose\n")
text.append(
"This script generates final Kaggle submission candidates for the Human Activity Recognition task. "
"It continues the empirically validated `3->1` cleanup branch while keeping the process reproducible and auditable.\n\n"
)
text.append("## Compliance and Reproducibility\n")
text.append(
"- No individual test IDs are manually edited.\n"
"- All predictions are generated by deterministic code from the current best submission and model-produced ranking.\n"
"- Each generated file is validated to have exactly `Id,Label` columns.\n"
"- IDs are aligned to `sample_submission.csv`.\n"
"- Labels are restricted to the valid range 0–5.\n"
"- A manifest and per-variant change audit files are generated.\n\n"
)
text.append("## Input Files\n")
text.append(f"- sample_submission: `{args.sample_submission}`\n")
text.append(f"- base_csv: `{args.base_csv}`\n")
text.append(f"- current_best_csv: `{args.current_best_csv}`\n")
text.append(f"- current_best_score: `{args.current_best_score}`\n")
text.append(f"- candidate_ranking: `{args.v57_ranking}`\n\n")
text.append("## Empirical Motivation\n")
text.append(
"The public leaderboard results indicated that minority-label correction alone was insufficient until a "
"`3->1` cleanup direction was identified. This transition improved the score through multiple successive "
"submissions, so the final candidates continue this branch with small increments and controlled combination variants.\n\n"
)
text.append("## Current Best Summary\n")
text.append(f"- Base label counts: `{base_counts}`\n")
text.append(f"- Current best label counts: `{current_counts}`\n")
text.append(f"- Current transitions vs base: `{current_transitions}`\n\n")
text.append("## Generated Variants\n")
text.append("| Variant | File | Changes vs Base | Diff vs Current | Counts | Note |\n")
text.append("|---|---|---:|---:|---|---|\n")
for r in records:
text.append(
f"| {r.variant_name} | `{r.file}` | {r.changes_vs_base} | "
f"{r.changes_vs_current_best} | `{r.counts}` | {r.note} |\n"
)
text.append("\n## Recommended Adaptive Submission Order\n")
text.append(
"1. `more_3to1_x2`: conservative continuation from the latest 0.8154 branch.\n"
"2. `more_3to1_x4`: stronger continuation if the first file improves or stays stable.\n"
"3. `combo_3to1x2_5to1x1`: controlled fallback if pure `3->1` begins to saturate.\n"
)
text.append("\n## How to Run\n")
text.append("```powershell\n")
text.append(
"python har_pipeline_v64_professional_final_push.py `\n"
" --sample_submission \"nycu-data-mining-assignment-3\\sample_submission.csv\" `\n"
" --base_csv \"submission_v54_ttw_brave.csv\" `\n"
" --current_best_csv \"SUBMIT_v63_01_more_3to1_x3.csv\" `\n"
" --current_best_score 0.8154 `\n"
" --v57_ranking \"submission_v57_minority_candidate_ranking.csv\" `\n"
" --output_prefix \"submission_v64_final\"\n"
)
text.append("```\n")
Path(path).write_text("".join(text), encoding="utf-8")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--sample_submission", required=True)
ap.add_argument("--base_csv", required=True)
ap.add_argument("--current_best_csv", required=True)
ap.add_argument("--current_best_score", type=float, default=np.nan)
ap.add_argument("--v57_ranking", required=True)
ap.add_argument("--output_prefix", default="submission_v64_final")
args = ap.parse_args()
sample_raw = read_submission(args.sample_submission)
sample = validate_against_sample(sample_raw, sample_raw, "sample_submission")
base = validate_against_sample(read_submission(args.base_csv), sample, "base_csv")
current = validate_against_sample(read_submission(args.current_best_csv), sample, "current_best_csv")
ranking = read_ranking(args.v57_ranking)
ids = sample["Id"].astype(int).tolist()
base_map = mapping_from_sub(base)
current_map = mapping_from_sub(current)
avoid = changed_ids(base_map, current_map)
base_counts = class_counts([base_map[i] for i in ids])
current_counts = class_counts([current_map[i] for i in ids])
current_transitions = transition_counts(base_map, current_map, ids)
print("BASE counts", base_counts)
print("CURRENT counts", current_counts)
print("CURRENT score", args.current_best_score)
print("CURRENT changes", len(avoid))
print("CURRENT transitions", current_transitions)
records: List[VariantRecord] = []
# Continue validated branch carefully.
for k in [1, 2, 3, 4, 6]:
labels, selected = add_transition(current_map, ranking, "3->1", k, avoid)
records.append(save_variant(
sample, base_map, current_map, labels,
variant_name=f"more_3to1_x{k}",
output_prefix=args.output_prefix,
note=f"current best + next {len(selected)} candidate(s) from 3->1 cleanup",
))
# Controlled fallback combinations.
combo_plans = {
"combo_3to1x2_5to1x1": [("3->1", 2), ("5->1", 1)],
"combo_3to1x2_5to1x2": [("3->1", 2), ("5->1", 2)],
"combo_3to1x2_3to5x2": [("3->1", 2), ("3->5", 2)],
"combo_3to1x4_5to1x1": [("3->1", 4), ("5->1", 1)],
"combo_3to1x4_3to5x2": [("3->1", 4), ("3->5", 2)],
}
for name, plan in combo_plans.items():
labels, selected = add_plan(current_map, ranking, plan, avoid)
records.append(save_variant(
sample, base_map, current_map, labels,
variant_name=name,
output_prefix=args.output_prefix,
note=f"controlled combo {plan}; selected={len(selected)}",
))
manifest_csv = f"{args.output_prefix}_manifest.csv"
manifest_json = f"{args.output_prefix}_manifest.json"
report_md = f"{args.output_prefix}_report_ready_summary.md"
pd.DataFrame([asdict(r) for r in records]).to_csv(manifest_csv, index=False)
Path(manifest_json).write_text(json.dumps([asdict(r) for r in records], indent=2), encoding="utf-8")
write_report_markdown(report_md, args, base_counts, current_counts, current_transitions, records)
print("\nManifest CSV:", manifest_csv)
print("Manifest JSON:", manifest_json)
print("Report-ready Markdown:", report_md)
print("\nRECOMMENDED:")
for name in [
"more_3to1_x2",
"more_3to1_x4",
"combo_3to1x2_5to1x1",
]:
print(f" {args.output_prefix}_{name}.csv")
if __name__ == "__main__":
main()