.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python/dask-examples/cpu_training.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_dask-examples_cpu_training.py: Example of training with Dask on CPU ==================================== .. GENERATED FROM PYTHON SOURCE LINES 6-51 .. code-block:: Python from dask import array as da from dask.distributed import Client, LocalCluster from xgboost import dask as dxgb from xgboost.dask import DaskDMatrix def main(client: Client) -> None: # generate some random data for demonstration m = 100000 n = 100 rng = da.random.default_rng(1) X = rng.normal(size=(m, n), chunks=(10000, -1)) y = X.sum(axis=1) # DaskDMatrix acts like normal DMatrix, works as a proxy for local # DMatrix scatter around workers. dtrain = DaskDMatrix(client, X, y) # Use train method from xgboost.dask instead of xgboost. This # distributed version of train returns a dictionary containing the # resulting booster and evaluation history obtained from # evaluation metrics. output = dxgb.train( client, {"verbosity": 1, "tree_method": "hist"}, dtrain, num_boost_round=4, evals=[(dtrain, "train")], ) bst = output["booster"] history = output["history"] # you can pass output directly into `predict` too. prediction = dxgb.predict(client, bst, dtrain) print("Evaluation history:", history) print("Error:", da.sqrt((prediction - y) ** 2).mean().compute()) if __name__ == "__main__": # or use other clusters for scaling with LocalCluster(n_workers=7, threads_per_worker=4) as cluster: with Client(cluster) as client: main(client) .. _sphx_glr_download_python_dask-examples_cpu_training.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: cpu_training.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: cpu_training.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: cpu_training.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_