Technical Writing
Published July 2026 · 12 min read
Building TriageAI’s ESI 3/4/5 Model: Why LightGBM Won
An end-to-end account of how I scoped the clinical task, created a leakage-safe pipeline, engineered 273 runtime features, compared model families, tuned safety-sensitive decisions, evaluated calibration, and packaged the final LightGBM registry for FastAPI.
The real modelling question
TriageAI is a clinical decision-support workflow for emergency intake and ESI care routing. It does not diagnose patients or replace clinician judgment.
The modelling goal was deliberately narrower: classify ESI 3, ESI 4, and ESI 5 cases while keeping higher-acuity safety handling explicit in the surrounding workflow.
That framing changed the question from:
Which algorithm gives the highest accuracy?
to:
Which candidate gives the best balance of class-level performance, unsafe-downgrade control, reproducibility, explainability, and deployment simplicity?
The source dataset contained 366,890 emergency-department records and 972 source columns. After restricting the deployment scope to ESI 3/4/5, the modelling subset contained 257,306 records. The final runtime feature contract contained 273 features.
Architecture Flow
- 01Stage
Source audit
366,890 records and 972 source columns
- 02Stage
Deployment scope
257,306 ESI 3/4/5 records
- 03Stage
Leakage-safe split
Train, validation, and test separated before fitting preprocessing
- 04Stage
Feature contract
273 final runtime features
- 05Stage
Candidate bake-off
Tree ensembles, neural baselines, and sequence challengers
- 06Stage
Risk-aware selection
Performance, unsafe downgrades, calibration, and deployment fit
- 07Stage
Runtime registry
Model, preprocessing, thresholds, labels, metrics, and manifest
1. Understanding the data before training
The first phase was exploratory analysis, not model fitting.
I examined:
- class distribution across ESI 3, 4, and 5
- missing-value patterns
- vital-sign distributions
- arrival mode and operational context
- age and complaint patterns
- duplicate or leakage-prone fields
- clinically important confusion directions
The classes were imbalanced, with ESI 3 much more common than ESI 5. That meant a model could achieve acceptable accuracy while still underperforming on the minority class.
I therefore treated accuracy as one useful measure—not the final selection rule.
2. Defining the evaluation contract
The final comparison used:
- Accuracy for overall correctness
- Macro F1 for balanced performance across all three classes
- Weighted F1 for class-frequency-aware performance
- ESI 5 F1 for the minority class
- Confusion patterns for directional errors
- Unsafe ESI 3 → ESI 5 downgrade rate as a safety-sensitive metric
- Deployment complexity and reproducibility
The unsafe downgrade rate mattered because a true ESI 3 case classified as ESI 5 represents a more consequential error than many other misclassifications.
A higher-accuracy candidate was not automatically better if it increased this downgrade risk.
3. Building a leakage-safe pipeline
I rebuilt the workflow around a strict separation:
Raw records
→ train / validation / test split
→ fit preprocessing on training data only
→ transform validation and test
→ train candidate models
→ tune decision policy on validation data
→ evaluate the final candidate once on test data
This ordering matters.
If imputation, encoding, scaling, vocabulary construction, or threshold selection uses information from validation or test data, the final metrics can become overly optimistic.
I also removed or avoided context-sensitive fields that were not appropriate for the intended workflow, including variables that could introduce fairness, trust, or governance concerns without a clear clinical justification.
The purpose was not to claim that bias had been eliminated. It was to reduce avoidable risk and keep the model contract more defensible.
4. Engineering clinically meaningful features
The source columns were transformed into a smaller runtime contract that the backend could reproduce.
Feature engineering included signals such as:
- missing-vital indicators
- abnormal-vital flags
- shock index
- pulse pressure
- mean arterial pressure
- complaint groupings such as cardiac, respiratory, abdominal, trauma, neurological, and infection-related categories
- encoded arrival and operational context
Missingness was not treated only as a technical nuisance. In clinical records, the absence of a measurement can itself reflect workflow behaviour.
The final design therefore combined safe imputation with missing-value indicators so the model could distinguish “measured and normal” from “not recorded.”
5. Choosing preprocessing deliberately
Different candidate families required different preprocessing.
One-hot encoding
Categorical values were converted into binary features rather than arbitrary integer codes. This avoided creating false numeric order between categories such as walk-in, ambulance, or other arrival modes.
Median imputation
Numeric medians were fitted only on training data and then applied consistently to validation, test, and runtime inputs.
Scaling
Scaling was used for neural-network experiments because gradient-based models are sensitive to feature magnitude. LightGBM did not require the same scaling step.
Tokenization
Tokenization supported RNN and GRU experiments using chief-complaint text. It was not required by the final LightGBM runtime.
Class weighting
Class weighting helped the candidate models pay more attention to underrepresented ESI 5 examples.
These choices were saved as part of the deployment contract rather than being left only inside a notebook.
6. Comparing model families
The bake-off included:
| Candidate | Why it was tested |
|---|---|
| LightGBM | Strong fit for high-dimensional tabular data |
| XGBoost | Competitive boosted-tree baseline |
| Random Forest | Interpretable traditional ensemble baseline |
| Dense Neural Network | Structured-data neural baseline |
| Simple RNN | Sequence modelling for complaint text |
| GRU hybrid | Combined structured and text-sequence experiment |
| Ensemble experiments | Tested whether combining candidates improved the safety/performance balance |
The neural and sequence models were valuable experiments, but they did not provide enough improvement to justify their additional preprocessing, tuning, latency, and explanation complexity.
Chief-complaint text was relatively short, while the strongest information remained structured and tabular.
7. Why LightGBM won
LightGBM produced the strongest practical combination of:
- overall test performance
- macro and weighted F1
- ESI 5 behaviour
- low unsafe-downgrade risk
- efficient inference
- compatibility with sparse, one-hot encoded features
- straightforward artifact loading
- explanation-ready feature importance
XGBoost was a serious candidate, but the final decision was not based on brand preference. LightGBM fit this dataset and deployment workflow better.
Final LightGBM V2 Registry
78.32% accuracy · 0.68% unsafe downgrade rate
Macro F1: 70.37%. Weighted F1: 78.88%. ESI 5 F1: 54.70%. Runtime scope: ESI 3/4/5 with 273 features.
8. Safety-sensitive decision tuning
The final candidate used a tuned ESI 5 decision policy rather than blindly accepting every raw class maximum.
The source-of-truth training workflow recorded:
ESI 5 decision threshold = 0.60
ESI 5 class-weight multiplier = 0.75
The purpose was to balance minority-class recognition with protection against unsafe ESI 3-to-ESI 5 downgrades.
Threshold tuning happened on validation data. The final reported metrics came from the held-out test evaluation.
This separation prevented the test set from becoming another tuning dataset.
9. Evaluating calibration without deploying it blindly
I evaluated:
- raw LightGBM probabilities
- Platt calibration
- isotonic calibration
The calibrated candidates improved probability-quality measures such as Brier score or log loss, but probability quality was not the only deployment requirement.
A calibrated candidate also had to preserve:
- Macro F1
- ESI 5 F1
- unsafe-downgrade controls
The calibrated alternatives did not preserve all configured guardrails, particularly minority-class decision performance. The final registry therefore retained:
Probability mode = raw LightGBM probabilities
Deploy calibrated probabilities = false
This was an important engineering lesson: a better calibration metric does not automatically make a better decision-support model.
10. Packaging a complete runtime registry
Saving only the model file would not have been enough.
The runtime registry includes:
model_registry/esi_345_lightgbm_v2/
├── artifact_manifest.json
├── esi_345_deployment_config.json
├── esi_345_label_mapping.json
├── esi_345_lightgbm_v2_feature_list.json
├── esi_345_lightgbm_v2_model.txt
├── esi_345_lightgbm_v2_preprocessing_artifacts.joblib
├── esi_345_lightgbm_v2_threshold_config.json
└── reports/
└── lightgbm_v2_test_metrics.json
Together, these files define:
- exact feature names and order
- imputation and encoding behaviour
- label mapping
- threshold rules
- probability mode
- model version
- verified metrics
- artifact integrity
A backend using a different feature order or preprocessing policy can produce incorrect predictions even when the model file is valid.
11. Separating source of truth from experiments
The final checked notebook lives under:
notebooks/FINAL_SOURCE_OF_TRUTH/
ESI_345_FINAL_DEPLOYMENT_LIGHTGBM_V2_SAFETY_TUNED_FINAL_VERDICT_CHECKED.ipynb
Earlier EDA, training, comparison, and challenger notebooks are preserved under experiment archives.
This separation prevents an old experiment from being mistaken for the deployed runtime source.
It also makes the project easier to explain:
experiments answer: what did I try?
source of truth answers: what was selected?
model registry answers: what does the backend load?
12. Connecting the model to the full workflow
The model is one component of a broader review-first system:
Clinical note or structured intake
→ clinician-reviewed structured fields
→ reproducible feature builder
→ LightGBM ESI 3/4/5 prediction
→ explicit safety-rule escalation
→ probability and explanation view
→ clinician accept / override / needs review
→ server-side audit trail
→ PDF decision-support summary
The model does not independently claim ESI 1 or ESI 2 competence.
Higher-acuity handling remains visible through safety logic and clinician oversight rather than being hidden inside an unsupported classifier claim.
13. What the final selection demonstrates
This work demonstrates several modelling decisions that matter beyond one algorithm comparison:
- start with the decision risk, not the model name
- separate data before fitting preprocessing
- evaluate minority-class behaviour explicitly
- inspect directional confusion, not only aggregate metrics
- tune thresholds on validation data
- evaluate calibration without sacrificing operational guardrails
- preserve every runtime dependency in a versioned registry
- connect model outputs to review, audit, and reporting workflows
- document limitations clearly
Current boundaries
The reported metrics describe one controlled test evaluation.
They do not establish:
- prospective clinical validation
- external hospital generalization
- production safety
- regulatory approval
- real-world clinical effectiveness
Future work should include temporally separated validation, external-site evaluation, subgroup analysis, drift monitoring, and formal clinical governance.
The final lesson is not that LightGBM always beats XGBoost.
The lesson is that model selection should reflect the real cost of being wrong—and that the chosen model must remain reproducible, reviewable, and bounded inside the system that uses it.