XGBoost provides a Dask interface to run training and prediction across a distributed cluster. A Dask cluster consists of a scheduler, workers, and a client (the user entry point).
When using the XGBoost Dask interface, you must pass the client object as an argument to most functions. If client is set to None, XGBoost will attempt to use the default client returned by Dask.
Key Requirements:
- Data (
X and y) must be Dask DataFrames or Dask Arrays. - Cluster construction should be guarded by
if __name__ == "__main__": to avoid errors in distributed environments.
from xgboost import dask as dxgb
import dask.array as da
import dask.distributed
if __name__ == "__main__":
cluster = dask.distributed.LocalCluster()
client = dask.distributed.Client(cluster)
# X and y must be Dask dataframes or arrays
num_obs = 1e5
num_features = 20
X = da.random.random(size=(num_obs, num_features), chunks=(1000, num_features))
y = da.random.random(size=(num_obs, 1), chunks=(1000, 1))
dtrain = dxgb.DaskDMatrix(client, X, y)
output = dxgb.train(
client,
{"verbosity": 2, "tree_method": "hist", "objective": "reg:squarederror"},
dtrain,
num_boost_round=4,
evals=[(dtrain, "train")],
)