{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Example of training with Dask on CPU\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from dask import array as da\nfrom dask.distributed import Client, LocalCluster\n\nfrom xgboost import dask as dxgb\nfrom xgboost.dask import DaskDMatrix\n\n\ndef main(client: Client) -> None:\n    # generate some random data for demonstration\n    m = 100000\n    n = 100\n    rng = da.random.default_rng(1)\n    X = rng.normal(size=(m, n), chunks=(10000, -1))\n    y = X.sum(axis=1)\n\n    # DaskDMatrix acts like normal DMatrix, works as a proxy for local\n    # DMatrix scatter around workers.\n    dtrain = DaskDMatrix(client, X, y)\n\n    # Use train method from xgboost.dask instead of xgboost.  This\n    # distributed version of train returns a dictionary containing the\n    # resulting booster and evaluation history obtained from\n    # evaluation metrics.\n    output = dxgb.train(\n        client,\n        {\"verbosity\": 1, \"tree_method\": \"hist\"},\n        dtrain,\n        num_boost_round=4,\n        evals=[(dtrain, \"train\")],\n    )\n    bst = output[\"booster\"]\n    history = output[\"history\"]\n\n    # you can pass output directly into `predict` too.\n    prediction = dxgb.predict(client, bst, dtrain)\n    print(\"Evaluation history:\", history)\n    print(\"Error:\", da.sqrt((prediction - y) ** 2).mean().compute())\n\n\nif __name__ == \"__main__\":\n    # or use other clusters for scaling\n    with LocalCluster(n_workers=7, threads_per_worker=4) as cluster:\n        with Client(cluster) as client:\n            main(client)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}