On this page:
8.1 Dumping trees
8.2 Feature importance
8.3 Other inspection
9.2

8 Inspecting a Model🔗ℹ

The Python package’s Plotting section renders feature importance and tree diagrams with matplotlib and graphviz. The Racket bindings don’t ship a plotting layer; instead they expose the underlying data — model dumps and importance scores — that you can render with whatever tooling you prefer.

Future work: a native rendering built on the Racket plot library (feature-importance bar charts, and possibly tree diagrams) would be a natural companion to the booster-dump / booster-feature-score data shown here.

8.1 Dumping trees🔗ℹ

booster-dump returns one string per tree. #:format selects "text" (the default), "json", or "dot". The "dot" form is graphviz source you can pipe straight to dot to draw the tree:

(require xgboost)
 
(booster-dump booster)                    ; list of text trees
(booster-dump booster #:format "json")
(define dots (booster-dump booster #:format "dot"))

Passing #:feature-names (and #:feature-types) substitutes readable names into the dump in place of f0, f1, … :

(booster-dump booster
              #:format "text"
              #:feature-names '("x0" "x1" "x2")
              #:feature-types '("q" "q" "q"))

8.2 Feature importance🔗ℹ

booster-feature-score computes per-feature importance. Choose the mode with #:importance-type"weight" (the default, split counts), "gain", "cover", or their totals. The result is a hash with 'features, 'scores (an f32vector?), and 'shape:

(define scores
  (booster-feature-score booster
                         #:importance-type "weight"
                         #:feature-names '("x0" "x1" "x2")))
 
(hash-ref scores 'features)   ; feature names in score order
(hash-ref scores 'scores)     ; f32vector of importances

To set readable feature names once so every dump and score uses them, call booster-set-feature-names! (and booster-set-feature-types!) on the booster after training.

8.3 Other inspection🔗ℹ

booster-num-feature and booster-boosted-rounds report the model’s width and how many rounds it has been trained for. booster-config returns XGBoost’s full configuration as a JSON string (treat it as opaque; round-trip it with booster-set-config!), and booster-attr / booster-set-attr! read and write arbitrary string attributes you want to travel with the model.