{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "db9cf912-1f7b-431d-9628-1afdea311779",
     "showTitle": false,
     "title": ""
    },
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "library/dask",
     "library/dask-cudf",
     "library/xgboost",
     "library/dask-deltatable",
     "library/dask-databricks",
     "library/dask-ml",
     "workflow/xgboost",
     "dataset/higgs",
     "data-format/csv",
     "data-storage/databricks-delta-lake",
     "platforms/databricks"
    ]
   },
   "source": [
    "# Training XGBoost with Dask RAPIDS in Databricks\n",
    "\n",
    "_January, 2024_\n",
    "\n",
    "This notebook shows how to deploy Dask RAPIDS workflow in Databricks. We will focus on the HIGGS dataset, a moderately sized classification problem from the [UCI Machine Learning repository.](https://archive.ics.uci.edu/dataset/280/higgs)\n",
    "\n",
    "In the following sections, we will begin by loading the dataset from [Delta Lake](https://delta.io/) and performing preprocessing with [Dask](https://github.com/dask/dask). Then train an [XGBoost](https://xgboost.readthedocs.io/en/stable/) model with various configurations and explore techniques for optimizing inference.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "1518eb70-21f5-4768-b06a-b376ec379335",
     "showTitle": false,
     "title": ""
    },
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "data-storage/databricks-delta-lake"
    ]
   },
   "source": [
    "## Launch multi-node Dask Cluster\n",
    "\n",
    "This workflow example can be ran on GPU, and you don't even need to have the GPU locally since Databricks can provide one for you. Whereas Dask enables users to easily distribute or scale up computation tasks within a single GPU or across multiple GPUs.\n",
    "\n",
    "Dask  recently introduced [**dask-databricks**](https://github.com/dask-contrib/dask-databricks) (available via [conda](https://github.com/conda-forge/dask-databricks-feedstock) and [pip](https://pypi.org/project/dask-databricks/)). With this CLI tool, the `dask databricks run --cuda` command will launch a Dask scheduler in the driver node and [`cuda` workers](https://docs.rapids.ai/api/dask-cuda/~~~rapids_api_docs_version~~~) in the remaining nodes.\n",
    "\n",
    "From a high level, we could break down this section into the following steps:\n",
    "* Create a new [init script](https://docs.databricks.com/en/init-scripts/index.html) that installs [RAPIDS](https://rapids.ai/) and runs `dask-databricks`\n",
    "* Create a new multi-node cluster that uses the init script\n",
    "* Once the cluster is running upload this notebook to Databricks and continue running these cells on there\n",
    "\n",
    "```{docref} https://github.com/rapidsai/deployment/blob/v25.08.00/source/platforms/databricks.md#multi-node-dask-cluster\n",
    "For more detailed information on launching Dask-RAPIDS in Databricks see the documentation.\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "dd79b033-6013-471d-a70c-a55d55fe52c1",
     "showTitle": false,
     "title": ""
    },
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## Import packages \n",
    "\n",
    "Once your cluster has launched, start by importing all necessary libraries and dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "109cfcaa-f2e4-446a-a3fa-c4025e0b70a4",
     "showTitle": false,
     "title": ""
    },
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "import dask_cudf\n",
    "import dask_databricks\n",
    "import dask_deltatable as ddt\n",
    "import numpy as np\n",
    "import xgboost as xgb\n",
    "from dask_ml.model_selection import train_test_split\n",
    "from distributed import wait\n",
    "from xgboost import dask as dxgb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "90de1e5e-d645-46db-85ac-874f0a191412",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Connect to Dask Client\n",
    "\n",
    "Connect to the client (and optionally Dashboard) to submit tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "0b6e31e3-d4da-4c86-9b10-d9eac4e4a40f",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\"> </div>\n",
       "    <div style=\"margin-left: 48px;\">\n",
       "        <h3 style=\"margin-bottom: 0px;\">Client</h3>\n",
       "        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Client-23114b4f-b7aa-11ee-87d9-9a67d50005f3</p>\n",
       "        <table style=\"width: 100%; text-align: left;\">\n",
       "\n",
       "        <tr>\n",
       "        \n",
       "            <td style=\"text-align: left;\"><strong>Connection method:</strong> Cluster object</td>\n",
       "            <td style=\"text-align: left;\"><strong>Cluster type:</strong> dask_databricks.DatabricksCluster</td>\n",
       "        \n",
       "        </tr>\n",
       "\n",
       "        \n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Dashboard: </strong> <a href=\"https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status\" target=\"_blank\">https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status</a>\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\"></td>\n",
       "            </tr>\n",
       "        \n",
       "\n",
       "        </table>\n",
       "\n",
       "        \n",
       "\n",
       "        \n",
       "            <details>\n",
       "            <summary style=\"margin-bottom: 20px;\"><h3 style=\"display: inline;\">Cluster Info</h3></summary>\n",
       "            <div class=\"jp-RenderedHTMLCommon jp-RenderedHTML jp-mod-trusted jp-OutputArea-output\">\n",
       "    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\">\n",
       "    </div>\n",
       "    <div style=\"margin-left: 48px;\">\n",
       "        <h3 style=\"margin-bottom: 0px; margin-top: 0px;\">DatabricksCluster</h3>\n",
       "        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">1031-230718-l2ubf858</p>\n",
       "        <table style=\"width: 100%; text-align: left;\">\n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Dashboard:</strong> <a href=\"https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status\" target=\"_blank\">https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status</a>\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Workers:</strong> 2\n",
       "                </td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Total threads:</strong> 2\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Total memory:</strong> 30.65 GiB\n",
       "                </td>\n",
       "            </tr>\n",
       "            \n",
       "        </table>\n",
       "\n",
       "        <details>\n",
       "            <summary style=\"margin-bottom: 20px;\">\n",
       "                <h3 style=\"display: inline;\">Scheduler Info</h3>\n",
       "            </summary>\n",
       "\n",
       "            <div style=\"\">\n",
       "    <div>\n",
       "        <div style=\"width: 24px; height: 24px; background-color: #FFF7E5; border: 3px solid #FF6132; border-radius: 5px; position: absolute;\"> </div>\n",
       "        <div style=\"margin-left: 48px;\">\n",
       "            <h3 style=\"margin-bottom: 0px;\">Scheduler</h3>\n",
       "            <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Scheduler-f908617a-76cd-4f5b-8fc9-fb04a02e0c99</p>\n",
       "            <table style=\"width: 100%; text-align: left;\">\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Comm:</strong> tcp://10.59.146.44:8786\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Workers:</strong> 2\n",
       "                    </td>\n",
       "                </tr>\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Dashboard:</strong> <a href=\"http://10.59.146.44:8087/status\" target=\"_blank\">http://10.59.146.44:8087/status</a>\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Total threads:</strong> 2\n",
       "                    </td>\n",
       "                </tr>\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Started:</strong> 22 minutes ago\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Total memory:</strong> 30.65 GiB\n",
       "                    </td>\n",
       "                </tr>\n",
       "            </table>\n",
       "        </div>\n",
       "    </div>\n",
       "\n",
       "    <details style=\"margin-left: 48px;\">\n",
       "        <summary style=\"margin-bottom: 20px;\">\n",
       "            <h3 style=\"display: inline;\">Workers</h3>\n",
       "        </summary>\n",
       "\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: tcp://10.59.135.19:33999</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://10.59.135.19:33999\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 1\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://10.59.135.19:35075/status\" target=\"_blank\">http://10.59.135.19:35075/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 15.33 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://10.59.135.19:41477\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> /tmp/dask-scratch-space/worker-639byx42\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>GPU: </strong>Tesla T4\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>GPU memory: </strong> 15.00 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: tcp://10.59.155.0:45293</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://10.59.155.0:45293\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 1\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://10.59.155.0:44287/status\" target=\"_blank\">http://10.59.155.0:44287/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 15.33 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://10.59.155.0:35699\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> /tmp/dask-scratch-space/worker-i0pmkkyv\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>GPU: </strong>Tesla T4\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>GPU memory: </strong> 15.00 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "\n",
       "    </details>\n",
       "</div>\n",
       "\n",
       "        </details>\n",
       "    </div>\n",
       "</div>\n",
       "            </details>\n",
       "        \n",
       "\n",
       "    </div>\n",
       "</div>"
      ]
     },
     "metadata": {
      "application/vnd.databricks.v1+output": {
       "addedWidgets": {},
       "arguments": {},
       "data": "<div>\n    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\"> </div>\n    <div style=\"margin-left: 48px;\">\n        <h3 style=\"margin-bottom: 0px;\">Client</h3>\n        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Client-23114b4f-b7aa-11ee-87d9-9a67d50005f3</p>\n        <table style=\"width: 100%; text-align: left;\">\n\n        <tr>\n        \n            <td style=\"text-align: left;\"><strong>Connection method:</strong> Cluster object</td>\n            <td style=\"text-align: left;\"><strong>Cluster type:</strong> dask_databricks.DatabricksCluster</td>\n        \n        </tr>\n\n        \n            <tr>\n                <td style=\"text-align: left;\">\n                    <strong>Dashboard: </strong> <a href=\"https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status\" target=\"_blank\">https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status</a>\n                </td>\n                <td style=\"text-align: left;\"></td>\n            </tr>\n        \n\n        </table>\n\n        \n\n        \n            <details>\n            <summary style=\"margin-bottom: 20px;\"><h3 style=\"display: inline;\">Cluster Info</h3></summary>\n            <div class=\"jp-RenderedHTMLCommon jp-RenderedHTML jp-mod-trusted jp-OutputArea-output\">\n    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\">\n    </div>\n    <div style=\"margin-left: 48px;\">\n        <h3 style=\"margin-bottom: 0px; margin-top: 0px;\">DatabricksCluster</h3>\n        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">1031-230718-l2ubf858</p>\n        <table style=\"width: 100%; text-align: left;\">\n            <tr>\n                <td style=\"text-align: left;\">\n                    <strong>Dashboard:</strong> <a href=\"https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status\" target=\"_blank\">https://dbc-dp-8721196619973675.cloud.databricks.com/driver-proxy/o/8721196619973675/1031-230718-l2ubf858/8087/status</a>\n                </td>\n                <td style=\"text-align: left;\">\n                    <strong>Workers:</strong> 2\n                </td>\n            </tr>\n            <tr>\n                <td style=\"text-align: left;\">\n                    <strong>Total threads:</strong> 2\n                </td>\n                <td style=\"text-align: left;\">\n                    <strong>Total memory:</strong> 30.65 GiB\n                </td>\n            </tr>\n            \n        </table>\n\n        <details>\n            <summary style=\"margin-bottom: 20px;\">\n                <h3 style=\"display: inline;\">Scheduler Info</h3>\n            </summary>\n\n            <div style=\"\">\n    <div>\n        <div style=\"width: 24px; height: 24px; background-color: #FFF7E5; border: 3px solid #FF6132; border-radius: 5px; position: absolute;\"> </div>\n        <div style=\"margin-left: 48px;\">\n            <h3 style=\"margin-bottom: 0px;\">Scheduler</h3>\n            <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Scheduler-f908617a-76cd-4f5b-8fc9-fb04a02e0c99</p>\n            <table style=\"width: 100%; text-align: left;\">\n                <tr>\n                    <td style=\"text-align: left;\">\n                        <strong>Comm:</strong> tcp://10.59.146.44:8786\n                    </td>\n                    <td style=\"text-align: left;\">\n                        <strong>Workers:</strong> 2\n                    </td>\n                </tr>\n                <tr>\n                    <td style=\"text-align: left;\">\n                        <strong>Dashboard:</strong> <a href=\"http://10.59.146.44:8087/status\" target=\"_blank\">http://10.59.146.44:8087/status</a>\n                    </td>\n                    <td style=\"text-align: left;\">\n                        <strong>Total threads:</strong> 2\n                    </td>\n                </tr>\n                <tr>\n                    <td style=\"text-align: left;\">\n                        <strong>Started:</strong> 22 minutes ago\n                    </td>\n                    <td style=\"text-align: left;\">\n                        <strong>Total memory:</strong> 30.65 GiB\n                    </td>\n                </tr>\n            </table>\n        </div>\n    </div>\n\n    <details style=\"margin-left: 48px;\">\n        <summary style=\"margin-bottom: 20px;\">\n            <h3 style=\"display: inline;\">Workers</h3>\n        </summary>\n\n        \n        <div style=\"margin-bottom: 20px;\">\n            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n            <div style=\"margin-left: 48px;\">\n            <details>\n                <summary>\n                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: tcp://10.59.135.19:33999</h4>\n                </summary>\n                <table style=\"width: 100%; text-align: left;\">\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Comm: </strong> tcp://10.59.135.19:33999\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>Total threads: </strong> 1\n                        </td>\n                    </tr>\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Dashboard: </strong> <a href=\"http://10.59.135.19:35075/status\" target=\"_blank\">http://10.59.135.19:35075/status</a>\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>Memory: </strong> 15.33 GiB\n                        </td>\n                    </tr>\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Nanny: </strong> tcp://10.59.135.19:41477\n                        </td>\n                        <td style=\"text-align: left;\"></td>\n                    </tr>\n                    <tr>\n                        <td colspan=\"2\" style=\"text-align: left;\">\n                            <strong>Local directory: </strong> /tmp/dask-scratch-space/worker-639byx42\n                        </td>\n                    </tr>\n\n                    \n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>GPU: </strong>Tesla T4\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>GPU memory: </strong> 15.00 GiB\n                        </td>\n                    </tr>\n                    \n\n                    \n\n                </table>\n            </details>\n            </div>\n        </div>\n        \n        <div style=\"margin-bottom: 20px;\">\n            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n            <div style=\"margin-left: 48px;\">\n            <details>\n                <summary>\n                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: tcp://10.59.155.0:45293</h4>\n                </summary>\n                <table style=\"width: 100%; text-align: left;\">\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Comm: </strong> tcp://10.59.155.0:45293\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>Total threads: </strong> 1\n                        </td>\n                    </tr>\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Dashboard: </strong> <a href=\"http://10.59.155.0:44287/status\" target=\"_blank\">http://10.59.155.0:44287/status</a>\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>Memory: </strong> 15.33 GiB\n                        </td>\n                    </tr>\n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>Nanny: </strong> tcp://10.59.155.0:35699\n                        </td>\n                        <td style=\"text-align: left;\"></td>\n                    </tr>\n                    <tr>\n                        <td colspan=\"2\" style=\"text-align: left;\">\n                            <strong>Local directory: </strong> /tmp/dask-scratch-space/worker-i0pmkkyv\n                        </td>\n                    </tr>\n\n                    \n                    <tr>\n                        <td style=\"text-align: left;\">\n                            <strong>GPU: </strong>Tesla T4\n                        </td>\n                        <td style=\"text-align: left;\">\n                            <strong>GPU memory: </strong> 15.00 GiB\n                        </td>\n                    </tr>\n                    \n\n                    \n\n                </table>\n            </details>\n            </div>\n        </div>\n        \n\n    </details>\n</div>\n\n        </details>\n    </div>\n</div>\n            </details>\n        \n\n    </div>\n</div>",
       "datasetInfos": [],
       "metadata": {},
       "removedWidgets": [],
       "textData": null,
       "type": "htmlSandbox"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "client = dask_databricks.get_client()\n",
    "client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "c658c7b9-d0ce-43d1-ac5c-78e271bef57f",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "\n",
    "## Download dataset\n",
    "\n",
    "First we download the dataset to Databrick File Storage (DBFS). Alternatively, you could also use cloud storage ([S3](https://aws.amazon.com/s3/), [Google Cloud](https://cloud.google.com/storage?hl=en), [Azure Data Lake](https://learn.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-introduction)\n",
    "Refer to [docs](https://docs.databricks.com/en/storage/index.html#:~:text=Databricks%20uses%20cloud%20object%20storage,storage%20locations%20in%20your%20account.) for  more information\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "373c083a-44a0-4cc3-9ac7-ffc6f927643d",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "import subprocess\n",
    "\n",
    "# Define the directory and file paths\n",
    "directory_path = \"/dbfs/databricks/rapids\"\n",
    "file_path = f\"{directory_path}/HIGGS.csv.gz\"\n",
    "\n",
    "# Check if directory already exists\n",
    "if not os.path.exists(directory_path):\n",
    "    os.makedirs(directory_path)\n",
    "\n",
    "# Check if the file already exists\n",
    "if not os.path.exists(file_path):\n",
    "    # If not, download dataset to the directory\n",
    "    data_url = (\n",
    "        \"https://archive.ics.uci.edu/ml/machine-learning-databases/00280/HIGGS.csv.gz\"\n",
    "    )\n",
    "    download_command = f\"curl {data_url} --output {file_path}\"\n",
    "    subprocess.run(download_command, shell=True)\n",
    "\n",
    "    # decompress the csv file\n",
    "    decompress_command = f\"gunzip {file_path}\"\n",
    "    subprocess.run(decompress_command, shell=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "ea03cd9a-9077-4cfc-a1a0-becdc84efe1a",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "Next we load the data into GPUs.  Because the data is loaded multiple times during parameter tuning, we convert the original CSV file into Parquet format for better performance.  This can be easily done using delta lake as shown in the next steps."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "2a87ba1c-14be-4d99-9b79-15d62052a902",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Integrating Dask and Delta Lake\n",
    "\n",
    "[**Delta Lake**](https://docs.databricks.com/en/delta/index.html) is an optimized storage layer within the Databricks lakehouse that provides a foundational platform for storing data and tables. This open-source software extends Parquet data files by incorporating a file-based transaction log to support [ACID transactions](https://docs.databricks.com/en/lakehouse/acid.html) and scalable metadata handling. \n",
    "\n",
    "Delta Lake is the default storage format for all operations on Databricks, i.e (unless otherwise specified, all tables on Databricks are Delta tables). \n",
    "Check out [tutorial for examples with basic Delta Lake operations](https://docs.databricks.com/en/delta/tutorial.html).\n",
    "\n",
    "Let's explore step-by-step how we can leverage Delta Lake tables with Dask to accelerate data pre-processing with RAPIDS."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "48b26e2b-5563-4836-a67c-dae5c9607db8",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Read from Delta table with Dask\n",
    "\n",
    "With Dask's [**dask-deltatable**](https://github.com/dask-contrib/dask-deltatable/tree/main), we can write the `.csv` file into a Delta table using [**Spark**](https://spark.apache.org/docs/latest/) then read and parallelize with [**Dask**](https://docs.dask.org/en/stable/). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "a2bd85a8-c4e2-419e-885a-b0a64c796dee",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Delta table 'higgs_delta_table' already exists.\n"
     ]
    }
   ],
   "source": [
    "delta_table_name = \"higgs_delta_table\"\n",
    "\n",
    "# Check if the Delta table already exists\n",
    "if spark.catalog.tableExists(delta_table_name):\n",
    "    # If it exists, print a message\n",
    "    print(f\"The Delta table '{delta_table_name}' already exists.\")\n",
    "else:\n",
    "    # If not, Load csv file into a Spark dataframe then\n",
    "    # Write the spark dataframe into delta table\n",
    "    data = spark.read.csv(file_path, header=True, inferSchema=True)\n",
    "    data.write.saveAsTable(delta_table_name)\n",
    "    print(f\"The Delta table '{delta_table_name}' has been created.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "6ebe932f-097a-4b6c-80d2-c4676c807d19",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style scoped>\n",
       "  .table-result-container {\n",
       "    max-height: 300px;\n",
       "    overflow: auto;\n",
       "  }\n",
       "  table, th, td {\n",
       "    border: 1px solid black;\n",
       "    border-collapse: collapse;\n",
       "  }\n",
       "  th, td {\n",
       "    padding: 5px;\n",
       "  }\n",
       "  th {\n",
       "    text-align: left;\n",
       "  }\n",
       "</style><div class='table-result-container'><table class='table-result'><thead style='background-color: white'><tr><th>format</th><th>id</th><th>name</th><th>description</th><th>location</th><th>createdAt</th><th>lastModified</th><th>partitionColumns</th><th>numFiles</th><th>sizeInBytes</th><th>properties</th><th>minReaderVersion</th><th>minWriterVersion</th><th>tableFeatures</th><th>statistics</th></tr></thead><tbody><tr><td>delta</td><td>90cdac79-5500-4a20-914b-47f86b616275</td><td>spark_catalog.default.higgs_delta_table</td><td>null</td><td>dbfs:/user/hive/warehouse/higgs_delta_table</td><td>2024-01-09T15:01:35.629+0000</td><td>2024-01-09T15:04:37.000+0000</td><td>List()</td><td>60</td><td>906326187</td><td>Map()</td><td>1</td><td>2</td><td>List(appendOnly, invariants)</td><td>Map()</td></tr></tbody></table></div>"
      ]
     },
     "metadata": {
      "application/vnd.databricks.v1+output": {
       "addedWidgets": {},
       "aggData": [],
       "aggError": "",
       "aggOverflow": false,
       "aggSchema": [],
       "aggSeriesLimitReached": false,
       "aggType": "",
       "arguments": {},
       "columnCustomDisplayInfos": {},
       "data": [
        [
         "delta",
         "90cdac79-5500-4a20-914b-47f86b616275",
         "spark_catalog.default.higgs_delta_table",
         null,
         "dbfs:/user/hive/warehouse/higgs_delta_table",
         "2024-01-09T15:01:35.629+0000",
         "2024-01-09T15:04:37.000+0000",
         [],
         60,
         906326187,
         {},
         1,
         2,
         [
          "appendOnly",
          "invariants"
         ],
         {}
        ]
       ],
       "datasetInfos": [],
       "dbfsResultPath": null,
       "isJsonSchema": true,
       "metadata": {},
       "overflow": false,
       "plotOptions": {
        "customPlotOptions": {},
        "displayType": "table",
        "pivotAggregation": null,
        "pivotColumns": null,
        "xColumns": null,
        "yColumns": null
       },
       "removedWidgets": [],
       "schema": [
        {
         "metadata": "{}",
         "name": "format",
         "type": "\"string\""
        },
        {
         "metadata": "{}",
         "name": "id",
         "type": "\"string\""
        },
        {
         "metadata": "{}",
         "name": "name",
         "type": "\"string\""
        },
        {
         "metadata": "{}",
         "name": "description",
         "type": "\"string\""
        },
        {
         "metadata": "{}",
         "name": "location",
         "type": "\"string\""
        },
        {
         "metadata": "{}",
         "name": "createdAt",
         "type": "\"timestamp\""
        },
        {
         "metadata": "{}",
         "name": "lastModified",
         "type": "\"timestamp\""
        },
        {
         "metadata": "{}",
         "name": "partitionColumns",
         "type": "{\"type\":\"array\",\"elementType\":\"string\",\"containsNull\":true}"
        },
        {
         "metadata": "{}",
         "name": "numFiles",
         "type": "\"long\""
        },
        {
         "metadata": "{}",
         "name": "sizeInBytes",
         "type": "\"long\""
        },
        {
         "metadata": "{}",
         "name": "properties",
         "type": "{\"type\":\"map\",\"keyType\":\"string\",\"valueType\":\"string\",\"valueContainsNull\":true}"
        },
        {
         "metadata": "{}",
         "name": "minReaderVersion",
         "type": "\"integer\""
        },
        {
         "metadata": "{}",
         "name": "minWriterVersion",
         "type": "\"integer\""
        },
        {
         "metadata": "{}",
         "name": "tableFeatures",
         "type": "{\"type\":\"array\",\"elementType\":\"string\",\"containsNull\":true}"
        },
        {
         "metadata": "{}",
         "name": "statistics",
         "type": "{\"type\":\"map\",\"keyType\":\"string\",\"valueType\":\"long\",\"valueContainsNull\":true}"
        }
       ],
       "type": "table"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(spark.sql(\"DESCRIBE DETAIL higgs_delta_table\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "9e4f2b0f-527e-4672-87c4-9850f3084fc0",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "Calling `dask_deltalake.read_deltalake()` will return a `dask dataframe`. However, our objective is to utilize GPU acceleration for the entire ML pipeline, including data processing, model training and inference. For this reason, we will read the dask dataframe into a `cuDF dask-dataframe`  using `dask_cudf.from_dask_dataframe()`\n",
    "\n",
    "**Note** that these operations will automatically leverage the Dask client we created, ensuring optimal performance boost through parallelism with dask."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "585648f2-1799-45b7-b1ca-a6a74525b136",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1.000000000000000000e+00</th>\n",
       "      <th>8.692932128906250000e-01</th>\n",
       "      <th>-6.350818276405334473e-01</th>\n",
       "      <th>2.256902605295181274e-01</th>\n",
       "      <th>3.274700641632080078e-01</th>\n",
       "      <th>-6.899932026863098145e-01</th>\n",
       "      <th>7.542022466659545898e-01</th>\n",
       "      <th>-2.485731393098831177e-01</th>\n",
       "      <th>-1.092063903808593750e+00</th>\n",
       "      <th>0.000000000000000000e+009</th>\n",
       "      <th>...</th>\n",
       "      <th>-1.045456994324922562e-02</th>\n",
       "      <th>-4.576716944575309753e-02</th>\n",
       "      <th>3.101961374282836914e+00</th>\n",
       "      <th>1.353760004043579102e+00</th>\n",
       "      <th>9.795631170272827148e-01</th>\n",
       "      <th>9.780761599540710449e-01</th>\n",
       "      <th>9.200048446655273438e-01</th>\n",
       "      <th>7.216574549674987793e-01</th>\n",
       "      <th>9.887509346008300781e-01</th>\n",
       "      <th>8.766783475875854492e-01</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.907542</td>\n",
       "      <td>0.329147</td>\n",
       "      <td>0.359412</td>\n",
       "      <td>1.497970</td>\n",
       "      <td>-0.313010</td>\n",
       "      <td>1.095531</td>\n",
       "      <td>-0.557525</td>\n",
       "      <td>-1.588230</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.138930</td>\n",
       "      <td>-0.000819</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.302220</td>\n",
       "      <td>0.833048</td>\n",
       "      <td>0.985700</td>\n",
       "      <td>0.978098</td>\n",
       "      <td>0.779732</td>\n",
       "      <td>0.992356</td>\n",
       "      <td>0.798343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.798835</td>\n",
       "      <td>1.470639</td>\n",
       "      <td>-1.635975</td>\n",
       "      <td>0.453773</td>\n",
       "      <td>0.425629</td>\n",
       "      <td>1.104875</td>\n",
       "      <td>1.282322</td>\n",
       "      <td>1.381664</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.128848</td>\n",
       "      <td>0.900461</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.909753</td>\n",
       "      <td>1.108330</td>\n",
       "      <td>0.985692</td>\n",
       "      <td>0.951331</td>\n",
       "      <td>0.803252</td>\n",
       "      <td>0.865924</td>\n",
       "      <td>0.780118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.344385</td>\n",
       "      <td>-0.876626</td>\n",
       "      <td>0.935913</td>\n",
       "      <td>1.992050</td>\n",
       "      <td>0.882454</td>\n",
       "      <td>1.786066</td>\n",
       "      <td>-1.646778</td>\n",
       "      <td>-0.942383</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.678379</td>\n",
       "      <td>-1.360356</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.946652</td>\n",
       "      <td>1.028704</td>\n",
       "      <td>0.998656</td>\n",
       "      <td>0.728281</td>\n",
       "      <td>0.869200</td>\n",
       "      <td>1.026736</td>\n",
       "      <td>0.957904</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.105009</td>\n",
       "      <td>0.321356</td>\n",
       "      <td>1.522401</td>\n",
       "      <td>0.882808</td>\n",
       "      <td>-1.205349</td>\n",
       "      <td>0.681466</td>\n",
       "      <td>-1.070464</td>\n",
       "      <td>-0.921871</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.373566</td>\n",
       "      <td>0.113041</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.755856</td>\n",
       "      <td>1.361057</td>\n",
       "      <td>0.986610</td>\n",
       "      <td>0.838085</td>\n",
       "      <td>1.133295</td>\n",
       "      <td>0.872245</td>\n",
       "      <td>0.808487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.595839</td>\n",
       "      <td>-0.607811</td>\n",
       "      <td>0.007075</td>\n",
       "      <td>1.818450</td>\n",
       "      <td>-0.111906</td>\n",
       "      <td>0.847550</td>\n",
       "      <td>-0.566437</td>\n",
       "      <td>1.581239</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.654227</td>\n",
       "      <td>-1.274345</td>\n",
       "      <td>3.101961</td>\n",
       "      <td>0.823761</td>\n",
       "      <td>0.938191</td>\n",
       "      <td>0.971758</td>\n",
       "      <td>0.789176</td>\n",
       "      <td>0.430553</td>\n",
       "      <td>0.961357</td>\n",
       "      <td>0.957818</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 29 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {
      "application/vnd.databricks.v1+output": {
       "addedWidgets": {},
       "arguments": {},
       "data": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>1.000000000000000000e+00</th>\n      <th>8.692932128906250000e-01</th>\n      <th>-6.350818276405334473e-01</th>\n      <th>2.256902605295181274e-01</th>\n      <th>3.274700641632080078e-01</th>\n      <th>-6.899932026863098145e-01</th>\n      <th>7.542022466659545898e-01</th>\n      <th>-2.485731393098831177e-01</th>\n      <th>-1.092063903808593750e+00</th>\n      <th>0.000000000000000000e+009</th>\n      <th>...</th>\n      <th>-1.045456994324922562e-02</th>\n      <th>-4.576716944575309753e-02</th>\n      <th>3.101961374282836914e+00</th>\n      <th>1.353760004043579102e+00</th>\n      <th>9.795631170272827148e-01</th>\n      <th>9.780761599540710449e-01</th>\n      <th>9.200048446655273438e-01</th>\n      <th>7.216574549674987793e-01</th>\n      <th>9.887509346008300781e-01</th>\n      <th>8.766783475875854492e-01</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>0.907542</td>\n      <td>0.329147</td>\n      <td>0.359412</td>\n      <td>1.497970</td>\n      <td>-0.313010</td>\n      <td>1.095531</td>\n      <td>-0.557525</td>\n      <td>-1.588230</td>\n      <td>2.173076</td>\n      <td>...</td>\n      <td>-1.138930</td>\n      <td>-0.000819</td>\n      <td>0.000000</td>\n      <td>0.302220</td>\n      <td>0.833048</td>\n      <td>0.985700</td>\n      <td>0.978098</td>\n      <td>0.779732</td>\n      <td>0.992356</td>\n      <td>0.798343</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1.0</td>\n      <td>0.798835</td>\n      <td>1.470639</td>\n      <td>-1.635975</td>\n      <td>0.453773</td>\n      <td>0.425629</td>\n      <td>1.104875</td>\n      <td>1.282322</td>\n      <td>1.381664</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>1.128848</td>\n      <td>0.900461</td>\n      <td>0.000000</td>\n      <td>0.909753</td>\n      <td>1.108330</td>\n      <td>0.985692</td>\n      <td>0.951331</td>\n      <td>0.803252</td>\n      <td>0.865924</td>\n      <td>0.780118</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.0</td>\n      <td>1.344385</td>\n      <td>-0.876626</td>\n      <td>0.935913</td>\n      <td>1.992050</td>\n      <td>0.882454</td>\n      <td>1.786066</td>\n      <td>-1.646778</td>\n      <td>-0.942383</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>-0.678379</td>\n      <td>-1.360356</td>\n      <td>0.000000</td>\n      <td>0.946652</td>\n      <td>1.028704</td>\n      <td>0.998656</td>\n      <td>0.728281</td>\n      <td>0.869200</td>\n      <td>1.026736</td>\n      <td>0.957904</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1.0</td>\n      <td>1.105009</td>\n      <td>0.321356</td>\n      <td>1.522401</td>\n      <td>0.882808</td>\n      <td>-1.205349</td>\n      <td>0.681466</td>\n      <td>-1.070464</td>\n      <td>-0.921871</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>-0.373566</td>\n      <td>0.113041</td>\n      <td>0.000000</td>\n      <td>0.755856</td>\n      <td>1.361057</td>\n      <td>0.986610</td>\n      <td>0.838085</td>\n      <td>1.133295</td>\n      <td>0.872245</td>\n      <td>0.808487</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.0</td>\n      <td>1.595839</td>\n      <td>-0.607811</td>\n      <td>0.007075</td>\n      <td>1.818450</td>\n      <td>-0.111906</td>\n      <td>0.847550</td>\n      <td>-0.566437</td>\n      <td>1.581239</td>\n      <td>2.173076</td>\n      <td>...</td>\n      <td>-0.654227</td>\n      <td>-1.274345</td>\n      <td>3.101961</td>\n      <td>0.823761</td>\n      <td>0.938191</td>\n      <td>0.971758</td>\n      <td>0.789176</td>\n      <td>0.430553</td>\n      <td>0.961357</td>\n      <td>0.957818</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 29 columns</p>\n</div>",
       "datasetInfos": [],
       "metadata": {},
       "removedWidgets": [],
       "textData": null,
       "type": "htmlSandbox"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Read the Delta Lake into a Dask DataFrame using `dask-deltatable`\n",
    "df = ddt.read_deltalake(\"/dbfs/user/hive/warehouse/higgs_delta_table\")\n",
    "\n",
    "# Convert Dask DataFrame to Dask cuDF for GPU acceleration\n",
    "ddf = dask_cudf.from_dask_dataframe(df)\n",
    "\n",
    "ddf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "1cdae47a-46a3-4015-bdc6-836c2823e295",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>feature-01</th>\n",
       "      <th>feature-02</th>\n",
       "      <th>feature-03</th>\n",
       "      <th>feature-04</th>\n",
       "      <th>feature-05</th>\n",
       "      <th>feature-06</th>\n",
       "      <th>feature-07</th>\n",
       "      <th>feature-08</th>\n",
       "      <th>feature-09</th>\n",
       "      <th>...</th>\n",
       "      <th>feature-19</th>\n",
       "      <th>feature-20</th>\n",
       "      <th>feature-21</th>\n",
       "      <th>feature-22</th>\n",
       "      <th>feature-23</th>\n",
       "      <th>feature-24</th>\n",
       "      <th>feature-25</th>\n",
       "      <th>feature-26</th>\n",
       "      <th>feature-27</th>\n",
       "      <th>feature-28</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.907542</td>\n",
       "      <td>0.329147</td>\n",
       "      <td>0.359412</td>\n",
       "      <td>1.497970</td>\n",
       "      <td>-0.313010</td>\n",
       "      <td>1.095531</td>\n",
       "      <td>-0.557525</td>\n",
       "      <td>-1.588230</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.138930</td>\n",
       "      <td>-0.000819</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.302220</td>\n",
       "      <td>0.833048</td>\n",
       "      <td>0.985700</td>\n",
       "      <td>0.978098</td>\n",
       "      <td>0.779732</td>\n",
       "      <td>0.992356</td>\n",
       "      <td>0.798343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.798835</td>\n",
       "      <td>1.470639</td>\n",
       "      <td>-1.635975</td>\n",
       "      <td>0.453773</td>\n",
       "      <td>0.425629</td>\n",
       "      <td>1.104875</td>\n",
       "      <td>1.282322</td>\n",
       "      <td>1.381664</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.128848</td>\n",
       "      <td>0.900461</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.909753</td>\n",
       "      <td>1.108330</td>\n",
       "      <td>0.985692</td>\n",
       "      <td>0.951331</td>\n",
       "      <td>0.803252</td>\n",
       "      <td>0.865924</td>\n",
       "      <td>0.780118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.344385</td>\n",
       "      <td>-0.876626</td>\n",
       "      <td>0.935913</td>\n",
       "      <td>1.992050</td>\n",
       "      <td>0.882454</td>\n",
       "      <td>1.786066</td>\n",
       "      <td>-1.646778</td>\n",
       "      <td>-0.942383</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.678379</td>\n",
       "      <td>-1.360356</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.946652</td>\n",
       "      <td>1.028704</td>\n",
       "      <td>0.998656</td>\n",
       "      <td>0.728281</td>\n",
       "      <td>0.869200</td>\n",
       "      <td>1.026736</td>\n",
       "      <td>0.957904</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.105009</td>\n",
       "      <td>0.321356</td>\n",
       "      <td>1.522401</td>\n",
       "      <td>0.882808</td>\n",
       "      <td>-1.205349</td>\n",
       "      <td>0.681466</td>\n",
       "      <td>-1.070464</td>\n",
       "      <td>-0.921871</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.373566</td>\n",
       "      <td>0.113041</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.755856</td>\n",
       "      <td>1.361057</td>\n",
       "      <td>0.986610</td>\n",
       "      <td>0.838085</td>\n",
       "      <td>1.133295</td>\n",
       "      <td>0.872245</td>\n",
       "      <td>0.808487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.595839</td>\n",
       "      <td>-0.607811</td>\n",
       "      <td>0.007075</td>\n",
       "      <td>1.818450</td>\n",
       "      <td>-0.111906</td>\n",
       "      <td>0.847550</td>\n",
       "      <td>-0.566437</td>\n",
       "      <td>1.581239</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.654227</td>\n",
       "      <td>-1.274345</td>\n",
       "      <td>3.101961</td>\n",
       "      <td>0.823761</td>\n",
       "      <td>0.938191</td>\n",
       "      <td>0.971758</td>\n",
       "      <td>0.789176</td>\n",
       "      <td>0.430553</td>\n",
       "      <td>0.961357</td>\n",
       "      <td>0.957818</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 29 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {
      "application/vnd.databricks.v1+output": {
       "addedWidgets": {},
       "arguments": {},
       "data": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>label</th>\n      <th>feature-01</th>\n      <th>feature-02</th>\n      <th>feature-03</th>\n      <th>feature-04</th>\n      <th>feature-05</th>\n      <th>feature-06</th>\n      <th>feature-07</th>\n      <th>feature-08</th>\n      <th>feature-09</th>\n      <th>...</th>\n      <th>feature-19</th>\n      <th>feature-20</th>\n      <th>feature-21</th>\n      <th>feature-22</th>\n      <th>feature-23</th>\n      <th>feature-24</th>\n      <th>feature-25</th>\n      <th>feature-26</th>\n      <th>feature-27</th>\n      <th>feature-28</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>0.907542</td>\n      <td>0.329147</td>\n      <td>0.359412</td>\n      <td>1.497970</td>\n      <td>-0.313010</td>\n      <td>1.095531</td>\n      <td>-0.557525</td>\n      <td>-1.588230</td>\n      <td>2.173076</td>\n      <td>...</td>\n      <td>-1.138930</td>\n      <td>-0.000819</td>\n      <td>0.000000</td>\n      <td>0.302220</td>\n      <td>0.833048</td>\n      <td>0.985700</td>\n      <td>0.978098</td>\n      <td>0.779732</td>\n      <td>0.992356</td>\n      <td>0.798343</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1.0</td>\n      <td>0.798835</td>\n      <td>1.470639</td>\n      <td>-1.635975</td>\n      <td>0.453773</td>\n      <td>0.425629</td>\n      <td>1.104875</td>\n      <td>1.282322</td>\n      <td>1.381664</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>1.128848</td>\n      <td>0.900461</td>\n      <td>0.000000</td>\n      <td>0.909753</td>\n      <td>1.108330</td>\n      <td>0.985692</td>\n      <td>0.951331</td>\n      <td>0.803252</td>\n      <td>0.865924</td>\n      <td>0.780118</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.0</td>\n      <td>1.344385</td>\n      <td>-0.876626</td>\n      <td>0.935913</td>\n      <td>1.992050</td>\n      <td>0.882454</td>\n      <td>1.786066</td>\n      <td>-1.646778</td>\n      <td>-0.942383</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>-0.678379</td>\n      <td>-1.360356</td>\n      <td>0.000000</td>\n      <td>0.946652</td>\n      <td>1.028704</td>\n      <td>0.998656</td>\n      <td>0.728281</td>\n      <td>0.869200</td>\n      <td>1.026736</td>\n      <td>0.957904</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1.0</td>\n      <td>1.105009</td>\n      <td>0.321356</td>\n      <td>1.522401</td>\n      <td>0.882808</td>\n      <td>-1.205349</td>\n      <td>0.681466</td>\n      <td>-1.070464</td>\n      <td>-0.921871</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>-0.373566</td>\n      <td>0.113041</td>\n      <td>0.000000</td>\n      <td>0.755856</td>\n      <td>1.361057</td>\n      <td>0.986610</td>\n      <td>0.838085</td>\n      <td>1.133295</td>\n      <td>0.872245</td>\n      <td>0.808487</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.0</td>\n      <td>1.595839</td>\n      <td>-0.607811</td>\n      <td>0.007075</td>\n      <td>1.818450</td>\n      <td>-0.111906</td>\n      <td>0.847550</td>\n      <td>-0.566437</td>\n      <td>1.581239</td>\n      <td>2.173076</td>\n      <td>...</td>\n      <td>-0.654227</td>\n      <td>-1.274345</td>\n      <td>3.101961</td>\n      <td>0.823761</td>\n      <td>0.938191</td>\n      <td>0.971758</td>\n      <td>0.789176</td>\n      <td>0.430553</td>\n      <td>0.961357</td>\n      <td>0.957818</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 29 columns</p>\n</div>",
       "datasetInfos": [],
       "metadata": {},
       "removedWidgets": [],
       "textData": null,
       "type": "htmlSandbox"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "colnames = [\"label\"] + [f\"feature-{i:02d}\" for i in range(1, 29)]\n",
    "ddf.columns = colnames\n",
    "ddf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "79d52d53-c22b-485c-82f5-7c6e8e9f486a",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Split data\n",
    "\n",
    "In the preceding step, we used [**`dask-cudf`**](https://docs.rapids.ai/api/dask-cudf/~~~rapids_api_docs_version~~~/) for loading data from the Delta table, now use `train_test_split()` function from [**`dask-ml`**](https://ml.dask.org/modules/api.html) to split up the dataset. \n",
    "\n",
    "Most of the time, the GPU backend of Dask works seamlessly with utilities in `dask-ml` and we can accelerate the entire ML pipeline as such: \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "7f2f4238-a2a0-445a-a5f5-ee1e554a7548",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "def load_higgs(\n",
    "    ddf,\n",
    ") -> tuple[\n",
    "    dask_cudf.core.DataFrame,\n",
    "    dask_cudf.core.Series,\n",
    "    dask_cudf.core.DataFrame,\n",
    "    dask_cudf.core.Series,\n",
    "]:\n",
    "    y = ddf[\"label\"]\n",
    "    X = ddf[ddf.columns.difference([\"label\"])]\n",
    "\n",
    "    X_train, X_valid, y_train, y_valid = train_test_split(\n",
    "        X, y, test_size=0.33, random_state=42\n",
    "    )\n",
    "    X_train, X_valid, y_train, y_valid = client.persist(\n",
    "        [X_train, X_valid, y_train, y_valid]\n",
    "    )\n",
    "    wait([X_train, X_valid, y_train, y_valid])\n",
    "\n",
    "    return X_train, X_valid, y_train, y_valid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "8539a1aa-fc35-4add-a067-81ca7ceaea5a",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/databricks/python/lib/python3.10/site-packages/dask_ml/model_selection/_split.py:462: FutureWarning: The default value for 'shuffle' must be specified when splitting DataFrames. In the future DataFrames will automatically be shuffled within blocks prior to splitting. Specify 'shuffle=True' to adopt the future behavior now, or 'shuffle=False' to retain the previous behavior.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "X_train, X_valid, y_train, y_valid = load_higgs(ddf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "450ae6b7-b33e-490b-a509-9b7a90319422",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature-01</th>\n",
       "      <th>feature-02</th>\n",
       "      <th>feature-03</th>\n",
       "      <th>feature-04</th>\n",
       "      <th>feature-05</th>\n",
       "      <th>feature-06</th>\n",
       "      <th>feature-07</th>\n",
       "      <th>feature-08</th>\n",
       "      <th>feature-09</th>\n",
       "      <th>feature-10</th>\n",
       "      <th>...</th>\n",
       "      <th>feature-19</th>\n",
       "      <th>feature-20</th>\n",
       "      <th>feature-21</th>\n",
       "      <th>feature-22</th>\n",
       "      <th>feature-23</th>\n",
       "      <th>feature-24</th>\n",
       "      <th>feature-25</th>\n",
       "      <th>feature-26</th>\n",
       "      <th>feature-27</th>\n",
       "      <th>feature-28</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.907542</td>\n",
       "      <td>0.329147</td>\n",
       "      <td>0.359412</td>\n",
       "      <td>1.497970</td>\n",
       "      <td>-0.313010</td>\n",
       "      <td>1.095531</td>\n",
       "      <td>-0.557525</td>\n",
       "      <td>-1.588230</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>0.812581</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.138930</td>\n",
       "      <td>-0.000819</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.302220</td>\n",
       "      <td>0.833048</td>\n",
       "      <td>0.985700</td>\n",
       "      <td>0.978098</td>\n",
       "      <td>0.779732</td>\n",
       "      <td>0.992356</td>\n",
       "      <td>0.798343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.798835</td>\n",
       "      <td>1.470639</td>\n",
       "      <td>-1.635975</td>\n",
       "      <td>0.453773</td>\n",
       "      <td>0.425629</td>\n",
       "      <td>1.104875</td>\n",
       "      <td>1.282322</td>\n",
       "      <td>1.381664</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.851737</td>\n",
       "      <td>...</td>\n",
       "      <td>1.128848</td>\n",
       "      <td>0.900461</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.909753</td>\n",
       "      <td>1.108330</td>\n",
       "      <td>0.985692</td>\n",
       "      <td>0.951331</td>\n",
       "      <td>0.803252</td>\n",
       "      <td>0.865924</td>\n",
       "      <td>0.780118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.105009</td>\n",
       "      <td>0.321356</td>\n",
       "      <td>1.522401</td>\n",
       "      <td>0.882808</td>\n",
       "      <td>-1.205349</td>\n",
       "      <td>0.681466</td>\n",
       "      <td>-1.070464</td>\n",
       "      <td>-0.921871</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.800872</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.373566</td>\n",
       "      <td>0.113041</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.755856</td>\n",
       "      <td>1.361057</td>\n",
       "      <td>0.986610</td>\n",
       "      <td>0.838085</td>\n",
       "      <td>1.133295</td>\n",
       "      <td>0.872245</td>\n",
       "      <td>0.808487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.739357</td>\n",
       "      <td>-0.178290</td>\n",
       "      <td>0.829934</td>\n",
       "      <td>0.504539</td>\n",
       "      <td>-0.130217</td>\n",
       "      <td>0.961051</td>\n",
       "      <td>-0.355518</td>\n",
       "      <td>-1.717399</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>0.620956</td>\n",
       "      <td>...</td>\n",
       "      <td>0.774065</td>\n",
       "      <td>0.398820</td>\n",
       "      <td>3.101961</td>\n",
       "      <td>0.944536</td>\n",
       "      <td>1.026261</td>\n",
       "      <td>0.982197</td>\n",
       "      <td>0.542115</td>\n",
       "      <td>1.250979</td>\n",
       "      <td>0.830045</td>\n",
       "      <td>0.761308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1.384098</td>\n",
       "      <td>0.116822</td>\n",
       "      <td>-1.179879</td>\n",
       "      <td>0.762913</td>\n",
       "      <td>-0.079782</td>\n",
       "      <td>1.019863</td>\n",
       "      <td>0.877318</td>\n",
       "      <td>1.276887</td>\n",
       "      <td>2.173076</td>\n",
       "      <td>0.331252</td>\n",
       "      <td>...</td>\n",
       "      <td>0.846521</td>\n",
       "      <td>0.504809</td>\n",
       "      <td>3.101961</td>\n",
       "      <td>0.959325</td>\n",
       "      <td>0.807376</td>\n",
       "      <td>1.191814</td>\n",
       "      <td>1.221210</td>\n",
       "      <td>0.861141</td>\n",
       "      <td>0.929341</td>\n",
       "      <td>0.838302</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {
      "application/vnd.databricks.v1+output": {
       "addedWidgets": {},
       "arguments": {},
       "data": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>feature-01</th>\n      <th>feature-02</th>\n      <th>feature-03</th>\n      <th>feature-04</th>\n      <th>feature-05</th>\n      <th>feature-06</th>\n      <th>feature-07</th>\n      <th>feature-08</th>\n      <th>feature-09</th>\n      <th>feature-10</th>\n      <th>...</th>\n      <th>feature-19</th>\n      <th>feature-20</th>\n      <th>feature-21</th>\n      <th>feature-22</th>\n      <th>feature-23</th>\n      <th>feature-24</th>\n      <th>feature-25</th>\n      <th>feature-26</th>\n      <th>feature-27</th>\n      <th>feature-28</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.907542</td>\n      <td>0.329147</td>\n      <td>0.359412</td>\n      <td>1.497970</td>\n      <td>-0.313010</td>\n      <td>1.095531</td>\n      <td>-0.557525</td>\n      <td>-1.588230</td>\n      <td>2.173076</td>\n      <td>0.812581</td>\n      <td>...</td>\n      <td>-1.138930</td>\n      <td>-0.000819</td>\n      <td>0.000000</td>\n      <td>0.302220</td>\n      <td>0.833048</td>\n      <td>0.985700</td>\n      <td>0.978098</td>\n      <td>0.779732</td>\n      <td>0.992356</td>\n      <td>0.798343</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.798835</td>\n      <td>1.470639</td>\n      <td>-1.635975</td>\n      <td>0.453773</td>\n      <td>0.425629</td>\n      <td>1.104875</td>\n      <td>1.282322</td>\n      <td>1.381664</td>\n      <td>0.000000</td>\n      <td>0.851737</td>\n      <td>...</td>\n      <td>1.128848</td>\n      <td>0.900461</td>\n      <td>0.000000</td>\n      <td>0.909753</td>\n      <td>1.108330</td>\n      <td>0.985692</td>\n      <td>0.951331</td>\n      <td>0.803252</td>\n      <td>0.865924</td>\n      <td>0.780118</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1.105009</td>\n      <td>0.321356</td>\n      <td>1.522401</td>\n      <td>0.882808</td>\n      <td>-1.205349</td>\n      <td>0.681466</td>\n      <td>-1.070464</td>\n      <td>-0.921871</td>\n      <td>0.000000</td>\n      <td>0.800872</td>\n      <td>...</td>\n      <td>-0.373566</td>\n      <td>0.113041</td>\n      <td>0.000000</td>\n      <td>0.755856</td>\n      <td>1.361057</td>\n      <td>0.986610</td>\n      <td>0.838085</td>\n      <td>1.133295</td>\n      <td>0.872245</td>\n      <td>0.808487</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>0.739357</td>\n      <td>-0.178290</td>\n      <td>0.829934</td>\n      <td>0.504539</td>\n      <td>-0.130217</td>\n      <td>0.961051</td>\n      <td>-0.355518</td>\n      <td>-1.717399</td>\n      <td>2.173076</td>\n      <td>0.620956</td>\n      <td>...</td>\n      <td>0.774065</td>\n      <td>0.398820</td>\n      <td>3.101961</td>\n      <td>0.944536</td>\n      <td>1.026261</td>\n      <td>0.982197</td>\n      <td>0.542115</td>\n      <td>1.250979</td>\n      <td>0.830045</td>\n      <td>0.761308</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>1.384098</td>\n      <td>0.116822</td>\n      <td>-1.179879</td>\n      <td>0.762913</td>\n      <td>-0.079782</td>\n      <td>1.019863</td>\n      <td>0.877318</td>\n      <td>1.276887</td>\n      <td>2.173076</td>\n      <td>0.331252</td>\n      <td>...</td>\n      <td>0.846521</td>\n      <td>0.504809</td>\n      <td>3.101961</td>\n      <td>0.959325</td>\n      <td>0.807376</td>\n      <td>1.191814</td>\n      <td>1.221210</td>\n      <td>0.861141</td>\n      <td>0.929341</td>\n      <td>0.838302</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 28 columns</p>\n</div>",
       "datasetInfos": [],
       "metadata": {},
       "removedWidgets": [],
       "textData": null,
       "type": "htmlSandbox"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "62ffc8ef-194c-4664-a45d-1a5bb0733925",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out[14]: 0     1.0\n",
      "1     1.0\n",
      "3     1.0\n",
      "10    0.0\n",
      "11    1.0\n",
      "Name: label, dtype: float64"
     ]
    }
   ],
   "source": [
    "y_train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "9190527c-ad0c-4f96-ac60-d38f6e0fd615",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Model training\n",
    "\n",
    "There are two things to notice here.  Firstly, we specify the number of rounds to trigger early stopping for training.  [XGBoost](https://xgboost.readthedocs.io/en/release_1.7.0/) will stop the training process once the validation metric fails to improve in consecutive X rounds, where **X** is the number of rounds specified for early \n",
    "stopping.  \n",
    "\n",
    "Secondly, we use a data type called `DaskDeviceQuantileDMatrix` for training but `DaskDMatrix` for validation.  `DaskDeviceQuantileDMatrix` is a drop-in replacement of `DaskDMatrix` for GPU-based training inputs that avoids extra data copies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "514a1d11-8257-4920-a25d-bb2401b9dd43",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "def fit_model_es(client, X, y, X_valid, y_valid) -> dxgb.Booster:\n",
    "    early_stopping_rounds = 5\n",
    "    Xy = dxgb.DaskDeviceQuantileDMatrix(client, X, y)\n",
    "    Xy_valid = dxgb.DaskDMatrix(client, X_valid, y_valid)\n",
    "    # train the model\n",
    "    booster = dxgb.train(\n",
    "        client,\n",
    "        {\n",
    "            \"objective\": \"binary:logistic\",\n",
    "            \"eval_metric\": \"error\",\n",
    "            \"tree_method\": \"gpu_hist\",\n",
    "        },\n",
    "        Xy,\n",
    "        evals=[(Xy_valid, \"Valid\")],\n",
    "        num_boost_round=1000,\n",
    "        early_stopping_rounds=early_stopping_rounds,\n",
    "    )[\"booster\"]\n",
    "    return booster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "3f3f8935-4f76-47c1-9280-386df3e6a49d",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/databricks/python/lib/python3.10/site-packages/xgboost/dask.py:703: FutureWarning: Please use `DaskQuantileDMatrix` instead.\n",
      "  warnings.warn(\"Please use `DaskQuantileDMatrix` instead.\", FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out[16]: <xgboost.core.Booster at 0x7f7c5702c4c0>"
     ]
    }
   ],
   "source": [
    "booster = fit_model_es(client, X=X_train, y=y_train, X_valid=X_valid, y_valid=y_valid)\n",
    "booster"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "83932465-f261-4ad8-82da-f696e727edc4",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Train with Customized objective and evaluation metric\n",
    "\n",
    "In the example below the XGBoost model is trained using a custom logistic regression-based objective function (`logit`) and a custom evaluation metric (`error`) along with early stopping.\n",
    "\n",
    "Note that the function returns both gradient and hessian, which XGBoost uses to optimize the model.  Also, the parameter named `metric_name` needs to be specified in our callback. It is used to inform XGBoost that the custom error function should be used for evaluating early stopping criteria."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "2b40ef80-c783-4216-9434-0a55c2eb3a63",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "def fit_model_customized_objective(client, X, y, X_valid, y_valid) -> dxgb.Booster:\n",
    "    def logit(predt: np.ndarray, Xy: xgb.DMatrix) -> tuple[np.ndarray, np.ndarray]:\n",
    "        predt = 1.0 / (1.0 + np.exp(-predt))\n",
    "        labels = Xy.get_label()\n",
    "        grad = predt - labels\n",
    "        hess = predt * (1.0 - predt)\n",
    "        return grad, hess\n",
    "\n",
    "    def error(predt: np.ndarray, Xy: xgb.DMatrix) -> tuple[str, float]:\n",
    "        label = Xy.get_label()\n",
    "        r = np.zeros(predt.shape)\n",
    "        predt = 1.0 / (1.0 + np.exp(-predt))\n",
    "        gt = predt > 0.5\n",
    "        r[gt] = 1 - label[gt]\n",
    "        le = predt <= 0.5\n",
    "        r[le] = label[le]\n",
    "        return \"CustomErr\", float(np.average(r))\n",
    "\n",
    "    # Use early stopping with custom objective and metric.\n",
    "    early_stopping_rounds = 5\n",
    "    # Specify the metric we want to use for early stopping.\n",
    "    es = xgb.callback.EarlyStopping(\n",
    "        rounds=early_stopping_rounds, save_best=True, metric_name=\"CustomErr\"\n",
    "    )\n",
    "\n",
    "    Xy = dxgb.DaskDeviceQuantileDMatrix(client, X, y)\n",
    "    Xy_valid = dxgb.DaskDMatrix(client, X_valid, y_valid)\n",
    "    booster = dxgb.train(\n",
    "        client,\n",
    "        {\"eval_metric\": \"error\", \"tree_method\": \"gpu_hist\"},\n",
    "        Xy,\n",
    "        evals=[(Xy_valid, \"Valid\")],\n",
    "        num_boost_round=1000,\n",
    "        obj=logit,  # pass the custom objective\n",
    "        feval=error,  # pass the custom metric\n",
    "        callbacks=[es],\n",
    "    )[\"booster\"]\n",
    "    return booster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "2fa0482c-319a-412a-a156-e03fc0186982",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/databricks/python/lib/python3.10/site-packages/xgboost/dask.py:703: FutureWarning: Please use `DaskQuantileDMatrix` instead.\n",
      "  warnings.warn(\"Please use `DaskQuantileDMatrix` instead.\", FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out[18]: <xgboost.core.Booster at 0x7f7c5702cd30>"
     ]
    }
   ],
   "source": [
    "booster_custom = fit_model_customized_objective(\n",
    "    client, X=X_train, y=y_train, X_valid=X_valid, y_valid=y_valid\n",
    ")\n",
    "booster_custom"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "746097aa-6b0a-43b8-be1a-fe476206f057",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Running inference\n",
    "\n",
    "After some tuning, we arrive at the final model for performing inference on new data. \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "4ad9abd0-10b2-46e5-8d57-3b163b421888",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "def predict(client, model, X):\n",
    "    predt = dxgb.predict(client, model, X)\n",
    "    return predt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "1bd1cdcc-5296-4671-b339-87862db38fde",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out[20]: 0     0.843650\n",
      "1     0.975618\n",
      "3     0.378462\n",
      "10    0.293985\n",
      "11    0.966303\n",
      "Name: 0, dtype: float32"
     ]
    }
   ],
   "source": [
    "preds = predict(client, booster, X_train)\n",
    "preds.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "c000a28d-473b-487b-92c1-1f5965f1898b",
     "showTitle": false,
     "title": ""
    }
   },
   "source": [
    "## Clean up\n",
    "\n",
    "When finished, be sure to destroy your cluster to avoid incurring extra costs for idle resources.\n",
    "\n",
    "**Note** If you forget to destroy the cluster manually, it's important to note that Databricks clusters will automatically time out after a period (specified during cluster creation)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "191ca13c-1678-4b9e-9235-6ef2b4f1dbb9",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "client.close()"
   ]
  }
 ],
 "metadata": {
  "application/vnd.databricks.v1+notebook": {
   "dashboards": [],
   "language": "python",
   "notebookMetadata": {
    "mostRecentlyExecutedCommandWithImplicitDF": {
     "commandId": -1,
     "dataframes": [
      "_sqldf"
     ]
    },
    "pythonIndentUnit": 2
   },
   "notebookName": "xgboost-dask",
   "widgets": {}
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
