Spaces:
Sleeping
Sleeping
feat: add CalibratedClassifierCV and roc_curve imports for enhanced model evaluation
Browse files
app/training/train_classifier.py
CHANGED
|
@@ -40,12 +40,14 @@ from sklearn.base import clone
|
|
| 40 |
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
|
| 41 |
from sklearn.exceptions import ConvergenceWarning
|
| 42 |
from sklearn.linear_model import LogisticRegression
|
|
|
|
| 43 |
from sklearn.metrics import (
|
| 44 |
accuracy_score,
|
| 45 |
f1_score,
|
| 46 |
precision_score,
|
| 47 |
recall_score,
|
| 48 |
roc_auc_score,
|
|
|
|
| 49 |
)
|
| 50 |
from sklearn.model_selection import StratifiedKFold, cross_val_predict, train_test_split
|
| 51 |
from sklearn.neural_network import MLPClassifier
|
|
@@ -602,6 +604,13 @@ def _build_candidate_families() -> dict[str, list[Any]]:
|
|
| 602 |
return families
|
| 603 |
|
| 604 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
def _build_eval_pipeline(model: Any) -> Pipeline:
|
| 606 |
return Pipeline(
|
| 607 |
[
|
|
|
|
| 40 |
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
|
| 41 |
from sklearn.exceptions import ConvergenceWarning
|
| 42 |
from sklearn.linear_model import LogisticRegression
|
| 43 |
+
from sklearn.calibration import CalibratedClassifierCV
|
| 44 |
from sklearn.metrics import (
|
| 45 |
accuracy_score,
|
| 46 |
f1_score,
|
| 47 |
precision_score,
|
| 48 |
recall_score,
|
| 49 |
roc_auc_score,
|
| 50 |
+
roc_curve,
|
| 51 |
)
|
| 52 |
from sklearn.model_selection import StratifiedKFold, cross_val_predict, train_test_split
|
| 53 |
from sklearn.neural_network import MLPClassifier
|
|
|
|
| 604 |
return families
|
| 605 |
|
| 606 |
|
| 607 |
+
def _optimal_threshold(y_true: np.ndarray, y_prob: np.ndarray) -> float:
|
| 608 |
+
"""Youden's J statistic: threshold that maximises sensitivity + specificity - 1."""
|
| 609 |
+
fpr, tpr, thresholds = roc_curve(y_true, y_prob)
|
| 610 |
+
j_scores = tpr - fpr
|
| 611 |
+
return float(thresholds[np.argmax(j_scores)])
|
| 612 |
+
|
| 613 |
+
|
| 614 |
def _build_eval_pipeline(model: Any) -> Pipeline:
|
| 615 |
return Pipeline(
|
| 616 |
[
|