> Every page of the ganymede website is listed in https://eugo.io/docs/ganymede/llms.txt.

# Ray Train: XGBoostTrainer

> An XGBoostTrainer runs a gradient-boosted tree training loop on a Ray Dataset shard.

Web page: https://eugo.io/docs/ganymede/ray_patterns/train/38_train-xgboost/
Notebook: https://eugo.io/docs/ganymede/ray_patterns/train/38_train-xgboost/38_train-xgboost.ipynb (source: https://github.com/eugo-inc/ganymede/blob/main/ray_patterns/train/38_train-xgboost.ipynb)

An XGBoostTrainer runs a gradient-boosted tree training loop on a Ray Dataset shard.

**When to use it.** Tabular models with XGBoost across several workers.

The picture below is the graph drawn in Eugo's visual Ray builder, and the cell under *Generated code* is the Ray code the builder produced from it. The code is plain Ray: it runs without the builder.

[Back to the gallery](https://eugo.io/docs/ganymede/ray_patterns/) · Tested with Ray 2.55.1

![Ray Train: XGBoostTrainer, drawn in Eugo's visual Ray builder](https://eugo.io/docs/ganymede/ray_patterns/images/train-xgboost.png)

*The graph in Eugo's visual Ray builder.*

The trainer type (XGBoost here) is a setting of the Trainer node, so the canvas looks the same for Torch, LightGBM and XGBoost.

## Setup

Start a local Ray instance. The options keep the notebook quiet: no dashboard, and worker logs stay out of the notebook. Ray is capped at four CPUs so the example behaves the same on any machine. The generated code below only calls `ray.init()` when Ray is not running yet, so it reuses this instance.

```python
import ray
ray.init(include_dashboard=False, log_to_driver=False, logging_level="ERROR", num_cpus=4)
```

```text title="Output"
RayContext(dashboard_url='', python_version='3.12.3', ray_version='2.55.1', ray_commit='237c2455ebb1ea15a32dd9e1fdeb2d617badc37f')
```

## User functions

The nodes in the graph call these ordinary Python functions and classes. In the builder you write them in a normal notebook cell, and the generated code refers to them by name.

```python
import os
import tempfile

import ray
import xgboost


def train_loop():
    # Runs on every training worker. Fits label = 2 * id from the id column.
    df = ray.train.get_dataset_shard("train").materialize().to_pandas()
    dtrain = xgboost.DMatrix(df[["id"]], label=df["id"] * 2)
    booster = xgboost.train({"objective": "reg:squarederror"}, dtrain, num_boost_round=5)
    with tempfile.TemporaryDirectory() as tmp:
        booster.save_model(os.path.join(tmp, "model.json"))
        ray.train.report(
            {"rows": len(df), "rounds": booster.num_boosted_rounds()},
            checkpoint=ray.train.Checkpoint.from_directory(tmp),
        )
```

## Generated code

This is the cell the visual builder generated from the graph above.

```python
# Generated with Eugo's visual Ray builder

import ray
from ray.train import ScalingConfig
from ray.train.xgboost import XGBoostTrainer

if not ray.is_initialized():
    ray.init()

ds = ray.data.range(8)

_trainer_n2 = XGBoostTrainer(train_loop, scaling_config=ScalingConfig(num_workers=1, use_gpu=False), datasets={"train": ds})
result = _trainer_n2.fit()
```

## Check the result

The cell below checks that the generated code did what the graph describes.

```python
import os

assert result.error is None, result.error
assert result.metrics["rows"] == 8 and result.metrics["rounds"] == 5, result.metrics
print("metrics:", result.metrics)
print("checkpoint saved:", os.path.basename(result.checkpoint.path))
```

```text title="Output"
metrics: {'rows': 8, 'rounds': 5}
checkpoint saved: checkpoint_2026-11-07_20-30-26.431863
```

## How the graph maps to the code

Each step names a node in the picture by its label, the lines of the generated cell it produced, and what those lines do. Node ids such as `n1` are not drawn on the canvas. Where the builder names a variable after a node, the id is its suffix (for example `_trainer_n2`).

**Setup** (lines 1, 3–5 and 7–8)

```python
# Generated with Eugo's visual Ray builder
...
import ray
from ray.train import ScalingConfig
from ray.train.xgboost import XGBoostTrainer
...
if not ray.is_initialized():
    ray.init()
```

The first line marks the cell as generated. The builder then adds an import for every Ray API the graph uses and a guarded ray.init(), so re-running the cell reuses the running cluster.

**Source** (node `n1`, line 10)

```python
ds = ray.data.range(8)
```

An eight-row dataset fed in as the "train" split.

**Trainer** (node `n2`, lines 12–13)

```python
_trainer_n2 = XGBoostTrainer(train_loop, scaling_config=ScalingConfig(num_workers=1, use_gpu=False), datasets={"train": ds})
result = _trainer_n2.fit()
```

XGBoostTrainer(train_loop, scaling_config=ScalingConfig(num_workers=1, use_gpu=False), datasets=...) runs the loop and returns a Result.

## Clean up

Shut down Ray, so the next notebook starts fresh.

```python
ray.shutdown()
```
