Implementation plan: XGBoost¶
This document is for contributors and maintainers adding first-class XGBoost support to TrustLens. It covers only the XGBoost library and its interaction with the existing analyze() pipeline.
Related plans (other libraries): Keras · TensorFlow
Status target: Stable — same public analyze() entry point as scikit-learn–compatible estimators, optional install extra, no xgboost import when users run import trustlens.
Executive summary¶
Item |
Decision |
|---|---|
Goal |
Users with |
Where it lives |
|
Dependencies |
Optional extra: |
Import rule |
Lazy |
TrustLens metrics already consume NumPy y_true, y_pred, y_prob. XGBoost work is boundary normalization only.
Objectives (XGBoost)¶
Binary classification —
XGBClassifierwithobjectiveimplying probabilities;predict_proba/predictreturn shapes compatible withtrustlens.apibinary calibration (y_prob[:, 1]when two columns).Multiclass —
predict_probashape(n_samples, n_classes); no crashes in calibration / failure / bias modules.Documentation —
docs/getting_started.mdanddocs/api_reference.md: minimal example withXGBClassifier+analyze().Discoverability — Optional extra documented in README / PyPI classifiers if appropriate.
Non-goals (v1)¶
XGBRegressoror non-classification tasks (unless calibration semantics are extended project-wide).Raw
xgboost.Booster+DMatrixwithout an sklearn-style wrapper (document workaround: wrap or passy_prob/y_predmanually).GPU-only CI as a merge blocker.
XGBoost training utilities inside TrustLens (users train outside; TrustLens analyzes).
Technical design¶
1. Detection¶
Pick one primary strategy and document it:
Explicit:
analyze(..., framework="xgboost")forces the XGBoost branch.Implicit: After lazy
import xgboost,isinstance(model, xgboost.XGBClassifier).
Avoid duck-typing that could mis-classify other estimators.
2. Prediction path¶
y_prob = np.asarray(model.predict_proba(X), dtype=np.float64)
y_pred = np.asarray(model.predict(X))
Binary edge case: If predict_proba ever returns shape (n_samples,), reshape to (n_samples, 2) using [1 - p, p] or match the convention already used in trustlens/api.py for positive-class scores. Add a unit test that mirrors XGBoost’s actual output for a toy fixture.
3. Sparse features¶
If X is scipy.sparse and XGBoost accepts it, either document “dense NumPy only for v1” or thread sparse through only where analyze() already allows it; do not silently densify huge matrices without documentation.
4. Metadata (optional v1)¶
Record in saved report metadata, e.g. framework: xgboost, xgboost_version, booster params summary string — helpful for audits.
Files to add or change (checklist)¶
Path |
Action |
|---|---|
|
|
|
|
|
Call resolver from |
|
Short XGBoost example. |
|
|
|
Mocks + shape normalization without |
|
|
|
|
Testing strategy¶
Default CI: Either skip XGBoost tests when extra not installed, or add a single optional job (Linux, one Python)
pip install -e ".[dev,xgboost]"and run marked tests — pick one and document inCONTRIBUTING.md.Markers: Register
requires_xgboostin[tool.pytest.ini_options]markers.Import smoke: Subprocess or
sys.modulesassertion:import trustlensdoes not loadxgboost.
Acceptance criteria (XGBoost complete)¶
[ ]
analyze(xgb_clf, X_val, y_val, verbose=False)works on a binarymake_classificationfixture withouty_prob.[ ] Three-class fixture completes without error.
[ ] No
xgboostimport atimport trustlenstime.[ ] Ruff + mypy clean for new code.
[ ] Docs and changelog updated.
Suggested PR breakdown¶
PR A — Resolver + sklearn parity Introduce
resolve_predictions; wireanalyze(); no XGBoost yet; tests prove identical behavior to today.PR B — XGBoost Optional extra, branch, tests, docs — isolated revert surface.
Maintainer sign-off¶
Gate |
☐ |
|---|---|
API / docs reviewed |
☐ |
Tests (default + optional) |
☐ |
No optional dep on core import |
☐ |
CHANGELOG entry |
☐ |
FAQ¶
Q: LightGBM / CatBoost?
A: Often work today via predict / predict_proba. Add library-specific tests in follow-up PRs; open a dedicated plan only if a quirk requires a new branch.
Q: Order relative to Keras / TensorFlow? A: Independent once PR A exists. XGBoost is lighter and is a good first optional backend.
Document history¶
Scope: XGBoost only; Keras and TensorFlow have separate plans in this directory.