{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. _california_housing_dataset:\n",
      "\n",
      "California Housing dataset\n",
      "--------------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 20640\n",
      "\n",
      "    :Number of Attributes: 8 numeric, predictive attributes and the target\n",
      "\n",
      "    :Attribute Information:\n",
      "        - MedInc        median income in block\n",
      "        - HouseAge      median house age in block\n",
      "        - AveRooms      average number of rooms\n",
      "        - AveBedrms     average number of bedrooms\n",
      "        - Population    block population\n",
      "        - AveOccup      average house occupancy\n",
      "        - Latitude      house block latitude\n",
      "        - Longitude     house block longitude\n",
      "\n",
      "    :Missing Attribute Values: None\n",
      "\n",
      "This dataset was obtained from the StatLib repository.\n",
      "http://lib.stat.cmu.edu/datasets/\n",
      "\n",
      "The target variable is the median house value for California districts.\n",
      "\n",
      "This dataset was derived from the 1990 U.S. census, using one row per census\n",
      "block group. A block group is the smallest geographical unit for which the U.S.\n",
      "Census Bureau publishes sample data (a block group typically has a population\n",
      "of 600 to 3,000 people).\n",
      "\n",
      "It can be downloaded/loaded using the\n",
      ":func:`sklearn.datasets.fetch_california_housing` function.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n",
      "      Statistics and Probability Letters, 33 (1997) 291-297\n",
      "\n",
      "Wall time: 28.9 s\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import xgboost as xgb\n",
    "from sklearn.datasets import fetch_california_housing\n",
    "\n",
    "# Fetch dataset using sklearn\n",
    "data = fetch_california_housing()\n",
    "print( data.DESCR)\n",
    "X = data.data\n",
    "y = data.target\n",
    "\n",
    "num_round = 500\n",
    "\n",
    "param = {\n",
    "    \"eta\": 0.05,\n",
    "    \"max_depth\": 10,\n",
    "    \"tree_method\": \"gpu_hist\",\n",
    "}\n",
    "\n",
    "# GPU accelerated training\n",
    "dtrain = xgb.DMatrix(X, label=y, feature_names=data.feature_names)\n",
    "%time model = xgb.train(param, dtrain,num_round)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3.73 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Compute shap values using GPU with xgboost\n",
    "# model.set_param({\"predictor\":\"cpu_predictor\"})\n",
    "model.set_param({\"predictor\": \"gpu_predictor\"})\n",
    "shap_values = model.predict(dtrain, pred_contribs=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 49.3 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Compute shap interaction values using GPU\n",
    "shap_interaction_values = model.predict(dtrain, pred_interactions=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3.69 s\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 31368 characters]\n",
      "text/plain": [
       "<Figure size 1440x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# We can use the shap package\n",
    "import shap\n",
    "\n",
    "\n",
    "# shap will call the GPU accelerated version as long as the predictor parameter is set to \"gpu_predictor\"\n",
    "model.set_param({\"predictor\": \"gpu_predictor\"})\n",
    "explainer = shap.TreeExplainer(model)\n",
    "%time shap_values = explainer.shap_values(X)\n",
    "\n",
    "# visualize the first prediction's explanation\n",
    "shap.force_plot(\n",
    "    explainer.expected_value,\n",
    "    shap_values[0, :],\n",
    "    X[0, :],\n",
    "    feature_names=data.feature_names,\n",
    "    matplotlib=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16696 characters]\n",
      "text/plain": [
       "<Figure size 576x338.4 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Show a summary of feature importance\n",
    "shap.summary_plot(shap_values, X, plot_type=\"bar\", feature_names=data.feature_names)"
   ]
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
