-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_sample_train.py
More file actions
56 lines (44 loc) · 1.74 KB
/
dev_sample_train.py
File metadata and controls
56 lines (44 loc) · 1.74 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
#!/usr/bin/env python3
"""
Generate a development CSV with columns:
f0, f1, ..., f{d-1}, y, s
- s is a binary sensitive attribute (0/1)
- y is binary and depends on f0 (signal) + a small bias from s
- features are standard normal; f0 carries true signal
Usage:
python dev_sample_csv.py --n 1000 --d 6 --seed 0 --out dev_sample.csv
"""
from __future__ import annotations
import argparse
from pathlib import Path
from typing import Tuple
import numpy as np
import pandas as pd
def make_data(n: int, d: int, seed: int) -> Tuple[pd.DataFrame, np.ndarray, np.ndarray]:
rng = np.random.RandomState(seed)
X = rng.randn(n, d) # features f0..f{d-1}
s = (rng.rand(n) > 0.5).astype(int) # balanced sensitive attribute
# logistic link: y* depends on f0 + small unfair dependence on s
logits = 0.9 * X[:, 0] + 0.3 * s + 0.1 * rng.randn(n)
probs = 1 / (1 + np.exp(-logits))
y = (rng.rand(n) < probs).astype(int)
cols = {f"f{i}": X[:, i] for i in range(d)}
df = pd.DataFrame(cols)
df["y"] = y
df["s"] = s
return df, y, s
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--n", type=int, default=1000, help="number of rows")
ap.add_argument("--d", type=int, default=6, help="number of feature columns f0..f{d-1}")
ap.add_argument("--seed", type=int, default=0)
ap.add_argument("--out", type=str, default="dev_sample_train.csv", help="output CSV path")
args = ap.parse_args()
df, _, _ = make_data(args.n, args.d, args.seed)
out_path = Path(args.out)
out_path.parent.mkdir(parents=True, exist_ok=True)
df.to_csv(out_path, index=False)
print(f"Wrote {len(df):,} rows to {out_path.resolve()}")
print(f"Columns: {', '.join(df.columns)}")
if __name__ == "__main__":
main()