{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Example of training survival model with Dask on CPU\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport dask.array as da\nimport dask.dataframe as dd\nfrom dask.distributed import Client, LocalCluster\n\nfrom xgboost import dask as dxgb\nfrom xgboost.dask import DaskDMatrix\n\n\ndef main(client: Client) -> da.Array:\n    # Load an example survival data from CSV into a Dask data frame.\n    # The Veterans' Administration Lung Cancer Trial\n    # The Statistical Analysis of Failure Time Data by Kalbfleisch J. and Prentice R (1980)\n    CURRENT_DIR = os.path.dirname(__file__)\n    df = dd.read_csv(\n        os.path.join(CURRENT_DIR, os.pardir, \"data\", \"veterans_lung_cancer.csv\")\n    )\n\n    # DaskDMatrix acts like normal DMatrix, works as a proxy for local\n    # DMatrix scatter around workers.\n    # For AFT survival, you'd need to extract the lower and upper bounds for the label\n    # and pass them as arguments to DaskDMatrix.\n    y_lower_bound = df[\"Survival_label_lower_bound\"]\n    y_upper_bound = df[\"Survival_label_upper_bound\"]\n    X = df.drop([\"Survival_label_lower_bound\", \"Survival_label_upper_bound\"], axis=1)\n    dtrain = DaskDMatrix(\n        client, X, label_lower_bound=y_lower_bound, label_upper_bound=y_upper_bound\n    )\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    params = {\n        \"verbosity\": 1,\n        \"objective\": \"survival:aft\",\n        \"eval_metric\": \"aft-nloglik\",\n        \"learning_rate\": 0.05,\n        \"aft_loss_distribution_scale\": 1.20,\n        \"aft_loss_distribution\": \"normal\",\n        \"max_depth\": 6,\n        \"lambda\": 0.01,\n        \"alpha\": 0.02,\n    }\n    output = dxgb.train(\n        client, params, dtrain, num_boost_round=100, 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\n    # Uncomment the following line to save the model to the disk\n    # bst.save_model('survival_model.json')\n\n    return prediction\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
}