On this page:
<r00-require>
<r00-provide>
<r00-data>
<r00-build>
<r00-run>
<*>

2.1 Building a DMatrix🔗ℹ

The smallest interesting thing you can do with the bindings is build a DMatrix XGBoost’s container for a feature matrix plus its per-row metadata — and read it back. Everything else (training, prediction) takes a DMatrix as input, so this is the foundation.

We hand make-dmatrix a flat f32vector of feature values in row-major order, tell it the shape with #:nrow/#:ncol, and attach a #:labels vector (the regression target) and an optional #:weights vector (per-row importance). The underlying native handle is reclaimed by Racket’s garbage collector when the value becomes unreachable, so there is nothing to free by hand.

(require ffi/vector
         xgboost)

Every example in this chapter exports a run-example thunk so the test harness in "examples/test/" and this documentation can drive the same code.

(provide run-example)

The data. Four rows of three features each, laid out row by row. The labels are the regression target; the weights up-weight the third row:

(define features
  (f32vector 1.0 2.0 0.5
             2.0 1.0 1.5
             3.0 0.5 0.0
             0.5 3.0 2.0))
(define labels  (f32vector 3.5 3.5 6.5 2.0))
(define weights (f32vector 1.0 1.0 2.0 0.5))

The matrix. make-dmatrix copies the data into a native DMatrix:

(make-dmatrix features
              #:nrow 4
              #:ncol 3
              #:labels labels
              #:weights weights)

run-example returns that DMatrix. The companion harness "test/00-print-dmatrix.rkt" renders it with dmatrix-show and materializes the rows with dmatrix->list (missing entries would show as +nan.0); its test submodule checks the shape and labels (run with raco test). You should see a 4×3 table whose labels read back as:

(dmatrix-label (run-example))   ; '(3.5 3.5 6.5 2.0)

(define (run-example)
  <r00-data>
  <r00-build>)

<*> ::=