{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Use scikit-learn regressor interface with CPU histogram tree method\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\n\n\ndef main(client: Client) -> dxgb.Booster:\n    # generate some random data for demonstration\n    n = 100\n    m = 10000\n    partition_size = 100\n    X = da.random.random((m, n), partition_size)\n    y = da.random.random(m, partition_size)\n\n    regressor = dxgb.DaskXGBRegressor(verbosity=1, n_estimators=2)\n    regressor.set_params(tree_method=\"hist\")\n    # assigning client here is optional\n    regressor.client = client\n\n    regressor.fit(X, y, eval_set=[(X, y)])\n    prediction = regressor.predict(X)\n\n    bst = regressor.get_booster()\n    history = regressor.evals_result()\n\n    print(\"Evaluation history:\", history)\n    # returned prediction is always a dask array.\n    assert isinstance(prediction, da.Array)\n    return bst  # returning the trained model\n\n\nif __name__ == \"__main__\":\n    # or use other clusters for scaling\n    with LocalCluster(n_workers=4, threads_per_worker=1) 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
}