-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature_engineering.py
More file actions
176 lines (142 loc) · 7.39 KB
/
Copy pathfeature_engineering.py
File metadata and controls
176 lines (142 loc) · 7.39 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
"""
feature_engineering.py — Build ML-ready features from ca_ed_clean.csv.
Run: python feature_engineering.py
"""
import pandas as pd
import numpy as np
INPUT_PATH = "data/ca_ed_final.csv"
OUTPUT_PATH = "data/ca_ed_features_v2.csv"
FACILITY_COL = "facility_id"
YEAR_COL = "year"
BURDEN_COL = "burden_score"
VISIT_COL = "ed_visit"
BASE_YEAR = 2012
COVID_YEAR = 2020
def main():
df = pd.read_csv(INPUT_PATH)
print(f"Loaded {len(df)} rows from {INPUT_PATH}\n")
# ── STEP 1 — SORT ─────────────────────────────────────────────────────────
print("=" * 60)
print("STEP 1 — SORT")
print("=" * 60)
df = df.sort_values([FACILITY_COL, YEAR_COL], ascending=True).reset_index(drop=True)
print(f"Sorted by [{FACILITY_COL}, {YEAR_COL}] ascending. Shape: {df.shape}")
# ── STEP 2 — LAG FEATURES ─────────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 2 — LAG FEATURES")
print("=" * 60)
grp = df.groupby(FACILITY_COL)[BURDEN_COL]
df["burden_lag_1"] = grp.shift(1)
df["burden_lag_2"] = grp.shift(2)
df["burden_lag_3"] = grp.shift(3)
lag_nulls = df[["burden_lag_1", "burden_lag_2", "burden_lag_3"]].isnull().sum()
print("Nulls created by lagging:")
print(lag_nulls.to_string())
# New lag: visits_per_station_normalized (if present)
VPS_NORM_COL = "visits_per_station_normalized"
try:
if VPS_NORM_COL in df.columns:
df["visits_per_station_lag1"] = (
df.groupby(FACILITY_COL)[VPS_NORM_COL].shift(1)
)
print(f"\nvisits_per_station_lag1 created from {VPS_NORM_COL}.")
print(f" Nulls: {df['visits_per_station_lag1'].isnull().sum()}")
else:
print(f"\n{VPS_NORM_COL} not found — visits_per_station_lag1 skipped.")
except Exception as exc:
print(f"WARNING: Could not create visits_per_station_lag1: {exc}")
# ── STEP 3 — ROLLING FEATURES ─────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 3 — ROLLING FEATURES")
print("=" * 60)
def rolling_mean(series, window):
return series.rolling(window, min_periods=1).mean()
def rolling_std(series, window):
return series.rolling(window, min_periods=1).std().fillna(0)
df["rolling_mean_3"] = grp.transform(lambda s: rolling_mean(s, 3))
df["rolling_mean_7"] = grp.transform(lambda s: rolling_mean(s, 7))
df["rolling_std_7"] = grp.transform(lambda s: rolling_std(s, 7))
sample_facility = df[FACILITY_COL].iloc[0]
sample = df[df[FACILITY_COL] == sample_facility][
[YEAR_COL, BURDEN_COL, "rolling_mean_3", "rolling_mean_7", "rolling_std_7"]
]
print(f"Rolling features for facility: {sample_facility}")
print(sample.to_string(index=False))
# ── STEP 4 — MOMENTUM ─────────────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 4 — MOMENTUM")
print("=" * 60)
df["pct_change_recent"] = (
(df[BURDEN_COL] - df["burden_lag_1"]) / df["burden_lag_1"]
).fillna(0).clip(-1, 1)
df["ed_visits_per_year"] = df[VISIT_COL]
print("pct_change_recent — min: {:.4f} max: {:.4f} mean: {:.4f}".format(
df["pct_change_recent"].min(),
df["pct_change_recent"].max(),
df["pct_change_recent"].mean(),
))
print(f"ed_visits_per_year column added (copy of {VISIT_COL}).")
# ── STEP 5 — TIME FEATURES ────────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 5 — TIME FEATURES")
print("=" * 60)
df["month"] = 1
df["is_post_covid"] = (df[YEAR_COL] >= COVID_YEAR).astype(int)
df["year_index"] = df[YEAR_COL] - BASE_YEAR
print(f"month : always 1 (annual data placeholder)")
print(f"is_post_covid : 1 if year >= {COVID_YEAR}")
print(f"year_index : year - {BASE_YEAR} (range {df['year_index'].min()}–{df['year_index'].max()})")
# ── STEP 6 — TARGET VARIABLE ──────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 6 — TARGET VARIABLE")
print("=" * 60)
# Per-facility 75th-percentile threshold
facility_p75 = (
df.groupby(FACILITY_COL)[BURDEN_COL]
.transform(lambda s: s.quantile(0.75))
)
next_year_burden = df.groupby(FACILITY_COL)[BURDEN_COL].shift(-1)
df["high_burden_next"] = (next_year_burden > facility_p75).astype("Int64")
null_target = df["high_burden_next"].isnull().sum()
df = df.dropna(subset=["high_burden_next"]).copy()
df["high_burden_next"] = df["high_burden_next"].astype(int)
print(f"Dropped {null_target} rows where high_burden_next was null (last year per facility).")
balance = df["high_burden_next"].value_counts().sort_index()
print("Class balance:")
print(f" 0 (not high burden next year): {balance.get(0, 0)}")
print(f" 1 (high burden next year) : {balance.get(1, 0)}")
print(f" Positive rate: {balance.get(1, 0) / len(df):.1%}")
# ── STEP 7 — DROP NULLS ───────────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 7 — DROP NULLS")
print("=" * 60)
lag_cols = ["burden_lag_1", "burden_lag_2", "burden_lag_3"]
rows_before = len(df)
df = df.dropna(subset=lag_cols).copy()
rows_dropped = rows_before - len(df)
print(f"Dropped {rows_dropped} rows with nulls in lag columns.")
print(f"Final row count: {len(df)}")
# ── STEP 8 — SAVE ─────────────────────────────────────────────────────────
print("\n" + "=" * 60)
print("STEP 8 — SAVE")
print("=" * 60)
df.to_csv(OUTPUT_PATH, index=False)
print(f"Saved → {OUTPUT_PATH}")
final_balance = df["high_burden_next"].value_counts().sort_index()
feature_cols = [c for c in df.columns if c not in [
"year", "oshpd_id", "facility_name", "county_name",
"er_service_level_desc", "ed_admit", "ed_visit",
"burden_score", "facility_id", "high_burden_next",
"true_burden_score", "burden_score_normalized",
"visits_per_station", "visits_per_station_normalized",
]]
print("\n── Final Summary ──")
print(f"Total rows : {len(df)}")
print(f"Total feature count : {len(feature_cols)}")
print(f"Features : {feature_cols}")
print(f"Class balance : 0 = {final_balance.get(0, 0)}, 1 = {final_balance.get(1, 0)}")
print(f"Positive rate : {final_balance.get(1, 0) / len(df):.1%}")
print(f"Year range : {df[YEAR_COL].min()} – {df[YEAR_COL].max()}")
print(f"Unique facilities : {df[FACILITY_COL].nunique()}")
if __name__ == "__main__":
main()