NAME
PDL::ML::Engine::XGBoost - XGBoost gradient boosting C API bindings for PDL
SYNOPSIS
use PDL;
use PDL::ML::Engine::XGBoost qw(dmatrix train load_model xgboost_version);
my $X = float([[1, 2], [2, 3], [3, 4], [4, 5]]);
my $y = float([3, 5, 7, 9]);
my $booster = train(
$X, $y,
num_boost_round => 50,
params => {
objective => 'reg:squarederror',
max_depth => 3,
eta => 0.2,
},
);
my $preds = $booster->predict(dmatrix($X));
$booster->save_model('model.ubj');
DESCRIPTION
PDL::ML::Engine::XGBoost binds PDL ndarrays directly to XGBoost's pure-C API (xgboost/c_api.h) via C99 XS, enabling fast gradient-boosted decision tree training, inference, and model persistence directly from Perl.
FUNCTIONS
dmatrix
Create a PDL::ML::Engine::XGBoost::DMatrix from a 2D PDL feature matrix (and optional 1D label ndarray).
$dm = dmatrix($X);
$dm = dmatrix($X, $y);
train
Train a PDL::ML::Engine::XGBoost::Booster for num_boost_round iterations with the given XGBoost parameter hashref.
$booster = train($X, $y, num_boost_round => 50, params => \%params);
load_model
Load a saved XGBoost model (JSON/UBJ) from disk into a PDL::ML::Engine::XGBoost::Booster.
$booster = load_model($path);
xgboost_version
Return the linked libxgboost version string (e.g. 3.0.4).
$ver = xgboost_version();
TUTORIAL
This tutorial adapts the official XGBoost quickstart (constructing a DMatrix, training a gradient-boosted Booster, predicting, and saving / reloading the model) to idiomatic Perl and PDL.
1. Build a DMatrix from PDL ndarrays
Convert a 2D feature matrix $X and 1D label vector $y into an XGBoost DMatrix using dmatrix:
use strict;
use warnings;
use File::Temp qw(tempfile);
use PDL;
use PDL::ML::Engine::XGBoost qw(dmatrix train load_model xgboost_version);
print 'XGBoost version: ', xgboost_version(), "\n";
my $X = float([
[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6],
[6, 7],
]);
my $y = float([3, 5, 7, 9, 11, 13]);
my $dm = dmatrix($X, $y);
print 'DMatrix shape: ', $dm->num_row(), ' x ', $dm->num_col(), "\n";
2. Train a Booster and Predict
Pass either ($dm, undef) or raw ($X, $y) to train along with num_boost_round and a params hashref, then call predict on a DMatrix:
my $booster = train(
$dm, undef,
num_boost_round => 60,
params => {
objective => 'reg:squarederror',
max_depth => 3,
eta => 0.3,
},
);
my $preds = $booster->predict($dm);
my $max_err = abs($preds - $y)->max();
print 'Predictions: ', $preds, "\n";
print 'Max abs error: ', $max_err, "\n";
3. Save and Reload a Model
Persist the trained booster in Universal Binary JSON (.ubj) or JSON format with save_model and reload it later with load_model:
my ($fh, $model_path) = tempfile('pdl_xgb_XXXXXX', SUFFIX => '.ubj', UNLINK => 1);
close $fh;
$booster->save_model($model_path);
my $reloaded = load_model($model_path);
my $preds2 = $reloaded->predict($dm);
print 'Reload diff: ', abs($preds - $preds2)->max(), "\n";
See examples/quickstart.pl in the distribution for a complete runnable script.
TYPES
Supports standard real numeric PDL types (byte, short, long, longlong, float, double as supported by the underlying library). Complex types (cfloat, cdouble) are not supported by this backend.
SEE ALSO
PDL, PDL::Torch, PDL::IO::Arrow, PDL::IO::Protobuf
AUTHOR
C.J. Collier <cjac@google.com>
COPYRIGHT AND LICENSE
Copyright 2026 Google LLC
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.