-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_eval.py
More file actions
159 lines (137 loc) · 4.66 KB
/
train_eval.py
File metadata and controls
159 lines (137 loc) · 4.66 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
"""Train/test split, models, metrics, ROC (no leakage: simple random split per person-row)."""
from __future__ import annotations
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix,
f1_score,
precision_score,
recall_score,
roc_auc_score,
roc_curve,
)
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
import config
def _feature_columns(panel: pd.DataFrame, id_col: str, target: str) -> List[str]:
return [c for c in panel.columns if c not in (id_col, target, "cesd_sum")]
def prepare_xy(
panel: pd.DataFrame,
target: str = "y_depressed",
id_col: str = config.ID_COL,
) -> Tuple[pd.DataFrame, pd.Series, List[str]]:
feat_cols = _feature_columns(panel, id_col, target)
if not feat_cols:
raise ValueError("No feature columns in panel.")
X = panel[feat_cols]
y = panel[target]
mask = y.notna()
X = X.loc[mask]
y = y.loc[mask]
return X, y, feat_cols
def make_logistic_pipeline() -> Pipeline:
return Pipeline(
[
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
(
"clf",
LogisticRegression(
max_iter=2000,
class_weight="balanced",
random_state=config.RANDOM_SEED,
),
),
]
)
def make_forest_pipeline() -> Pipeline:
return Pipeline(
[
("imputer", SimpleImputer(strategy="median")),
(
"clf",
RandomForestClassifier(
n_estimators=200,
max_depth=8,
class_weight="balanced",
random_state=config.RANDOM_SEED,
n_jobs=-1,
),
),
]
)
def evaluate_models(
X: pd.DataFrame,
y: pd.Series,
test_size: float = 0.2,
out_dir: Optional[Path] = None,
) -> Dict[str, Any]:
"""Fit logistic regression and random forest; return metrics dict."""
out_dir = Path(out_dir or config.OUTPUT_DIR)
out_dir.mkdir(parents=True, exist_ok=True)
if y.nunique() < 2:
raise ValueError("Target has a single class; cannot train classifier.")
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=test_size,
random_state=config.RANDOM_SEED,
stratify=y,
)
models = {
"logistic_regression": make_logistic_pipeline(),
"random_forest": make_forest_pipeline(),
}
results: Dict[str, Any] = {}
for name, pipe in models.items():
pipe.fit(X_train, y_train)
proba = pipe.predict_proba(X_test)[:, 1]
pred = pipe.predict(X_test)
results[name] = {
"accuracy": float(accuracy_score(y_test, pred)),
"precision": float(precision_score(y_test, pred, zero_division=0)),
"recall": float(recall_score(y_test, pred, zero_division=0)),
"f1": float(f1_score(y_test, pred, zero_division=0)),
"roc_auc": float(roc_auc_score(y_test, proba)),
"confusion_matrix": confusion_matrix(y_test, pred).tolist(),
"classification_report": classification_report(
y_test, pred, zero_division=0
),
}
# ROC plot
fpr, tpr, _ = roc_curve(y_test, proba)
plt.figure(figsize=(6, 5))
plt.plot(fpr, tpr, label=f"{name} AUC={results[name]['roc_auc']:.3f}")
plt.plot([0, 1], [0, 1], "k--", alpha=0.4)
plt.xlabel("False positive rate")
plt.ylabel("True positive rate")
plt.title(f"ROC — {name}")
plt.legend(loc="lower right")
plt.tight_layout()
roc_path = out_dir / f"roc_{name}.png"
plt.savefig(roc_path, dpi=120)
plt.close()
results[name]["roc_path"] = str(roc_path)
return results
def metrics_to_csv(results: Dict[str, Any], path: Path) -> None:
rows = []
for name, r in results.items():
rows.append(
{
"model": name,
"accuracy": r["accuracy"],
"precision": r["precision"],
"recall": r["recall"],
"f1": r["f1"],
"roc_auc": r["roc_auc"],
}
)
pd.DataFrame(rows).to_csv(path, index=False)