Therdpoom commited on
Commit
320b93f
·
verified ·
1 Parent(s): a63d759

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -478,6 +478,10 @@ def hr_classify():
478
  for k, v in fi],
479
  }
480
 
 
 
 
 
481
  # ── Best model predictions ──
482
  best_model_name = model_type if model_type != 'all' else 'xgboost'
483
  if best_model_name not in results:
@@ -494,7 +498,31 @@ def hr_classify():
494
 
495
  best_clf.fit(X_train, y_train)
496
  all_probs = best_clf.predict_proba(X)[:, 1]
497
- all_preds = best_clf.predict(X)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
 
499
  # ── Employee predictions ──
500
  predictions = []
@@ -562,6 +590,8 @@ def hr_classify():
562
  "top_at_risk": sorted(predictions,
563
  key=lambda x: x['resign_probability'],
564
  reverse=True)[:10],
 
 
565
  "disclaimer": "ผลการพยากรณ์ใช้เพื่อสนับสนุนการตัดสินใจ HR เท่านั้น"
566
  })
567
 
 
478
  for k, v in fi],
479
  }
480
 
481
+ # ── Threshold ──
482
+ threshold = float(body.get("threshold", 0.5))
483
+ threshold = max(0.1, min(0.9, threshold))
484
+
485
  # ── Best model predictions ──
486
  best_model_name = model_type if model_type != 'all' else 'xgboost'
487
  if best_model_name not in results:
 
498
 
499
  best_clf.fit(X_train, y_train)
500
  all_probs = best_clf.predict_proba(X)[:, 1]
501
+ # ใช้ threshold ที่กำหนด แทน default 0.5
502
+ all_preds = (all_probs >= threshold).astype(int)
503
+
504
+ # ── Threshold analysis (สำหรับ chart) ──
505
+ thresholds_range = [round(t/10, 1) for t in range(1, 10)]
506
+ threshold_analysis = []
507
+ if has_label:
508
+ test_probs = best_clf.predict_proba(X_test)[:, 1]
509
+ for t in thresholds_range:
510
+ t_preds = (test_probs >= t).astype(int)
511
+ tp = int(((t_preds == 1) & (y_test == 1)).sum())
512
+ fp = int(((t_preds == 1) & (y_test == 0)).sum())
513
+ fn = int(((t_preds == 0) & (y_test == 1)).sum())
514
+ tn = int(((t_preds == 0) & (y_test == 0)).sum())
515
+ prec = tp/(tp+fp) if (tp+fp) > 0 else 0
516
+ rec = tp/(tp+fn) if (tp+fn) > 0 else 0
517
+ f1 = 2*prec*rec/(prec+rec) if (prec+rec) > 0 else 0
518
+ threshold_analysis.append({
519
+ "threshold": t,
520
+ "precision_pct": round(prec*100, 1),
521
+ "recall_pct": round(rec*100, 1),
522
+ "f1_pct": round(f1*100, 1),
523
+ "flagged": int(t_preds.sum()),
524
+ "tp": tp, "fp": fp, "fn": fn, "tn": tn
525
+ })
526
 
527
  # ── Employee predictions ──
528
  predictions = []
 
590
  "top_at_risk": sorted(predictions,
591
  key=lambda x: x['resign_probability'],
592
  reverse=True)[:10],
593
+ "threshold_used": threshold,
594
+ "threshold_analysis": threshold_analysis if has_label else [],
595
  "disclaimer": "ผลการพยากรณ์ใช้เพื่อสนับสนุนการตัดสินใจ HR เท่านั้น"
596
  })
597