{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ba593408-6551-546f-d010-fd2201549edf"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import xgboost as xgb\n",
        "import pandas as pd\n",
        "import math\n",
        "import os\n",
        "import sys\n",
        "%matplotlib inline\n",
        "\n",
        "from sklearn.cross_validation import train_test_split\n",
        "from ml_metrics import rmsle"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8797990-f3b7-5f68-e7fd-dee5e7b48464"
      },
      "outputs": [],
      "source": [
        "def getVariables(value=1000):\n",
        "    for var, obj in globals().items():\n",
        "        try:\n",
        "            if(sys.getsizeof(obj) > value and not var.startswith(\"_\")):\n",
        "                    print (\"{0:30} {1:5}\".format(var, sys.getsizeof(obj)))\n",
        "        except:\n",
        "            continue"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "24dbf952-7c06-8edb-cae5-eec991cf61ac"
      },
      "outputs": [],
      "source": [
        "def evalerror(preds, dtrain):\n",
        "\n",
        "    labels = dtrain.get_label()\n",
        "    assert len(preds) == len(labels)\n",
        "    labels = labels.tolist()\n",
        "    preds = preds.tolist()\n",
        "    terms_to_sum = [(math.log(labels[i] + 1) - math.log(max(0,preds[i]) + 1)) ** 2.0 for i,pred in enumerate(labels)]\n",
        "    return 'error', (sum(terms_to_sum) * (1.0/len(preds))) ** 0.5"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "594437cb-f0b5-873e-2395-87dd37e0eb40"
      },
      "outputs": [],
      "source": [
        "print ('Loading Test...')\n",
        "dtype_test = {'id':np.uint32,\n",
        "              'Semana': np.uint8, \n",
        "              'Agencia_ID': np.uint16, \n",
        "              'Canal_ID': np.uint8,\n",
        "              'Ruta_SAK': np.uint16, \n",
        "              'Cliente_ID': np.uint32, \n",
        "              'Producto_ID': np.uint16}\n",
        "\n",
        "%time test = pd.read_csv('../input/test.csv', usecols=dtype_test.keys(), dtype=dtype_test)\n",
        "test.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ddeda89d-bcf9-04aa-a07f-2930a3f52197"
      },
      "outputs": [],
      "source": [
        "test.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bfdd3a7e-3813-1839-21f2-b37c9e4b05a6"
      },
      "outputs": [],
      "source": [
        "dtype = {'Semana': np.uint8, \n",
        "         'Agencia_ID': np.uint16, \n",
        "         'Canal_ID': np.uint8,\n",
        "         'Ruta_SAK': np.uint16, \n",
        "         'Cliente_ID': np.uint32, \n",
        "         'Producto_ID': np.uint16,\n",
        "         'Demanda_uni_equil': np.uint16}\n",
        "\n",
        "filename='../input/train.csv'\n",
        "\n",
        "\n",
        "%time train = pd.read_csv(filename, usecols=dtype.keys(), dtype=dtype, warn_bad_lines= True,engine='c')\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e442b1e1-9768-a5dc-273d-466d85bd1522"
      },
      "outputs": [],
      "source": [
        "train = train[train[\"Semana\"]>8]\n",
        "print ('Training_Shape:', train.shape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28b19c48-627b-a1e4-4cb8-b83f5a72a1ac"
      },
      "outputs": [],
      "source": [
        "ids = test['id']\n",
        "test = test.drop(['id'],axis = 1)\n",
        "\n",
        "y = train['Demanda_uni_equil']\n",
        "X = train[test.columns.values]\n",
        "\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1729)\n",
        "del(train)\n",
        "print ('Division_Set_Shapes:', X.shape, y.shape)\n",
        "print ('Validation_Set_Shapes:', X_train.shape, X_test.shape)\n",
        "del(X)\n",
        "del(y)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "18eda159-5875-58a9-cbb5-92f38c52ccbb"
      },
      "outputs": [],
      "source": [
        "params = {}\n",
        "params['objective'] = \"reg:linear\"\n",
        "params['eta'] = 0.1\n",
        "params['max_depth'] = 5\n",
        "params['subsample'] = 0.8\n",
        "params['colsample_bytree'] = 0.6\n",
        "params['silent'] = True\n",
        "#params['nthread']= 4\n",
        "params['booster'] = \"gbtree\"\n",
        "\n",
        "\n",
        "test_preds = np.zeros(test.shape[0])\n",
        "xg_train = xgb.DMatrix(X_train, label=y_train)\n",
        "del(X_train)\n",
        "del(y_train)\n",
        "xg_test = xgb.DMatrix(X_test)\n",
        "del(X_test)\n",
        "watchlist = [(xg_train, 'train')]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1f1d57cd-19dc-c8ad-d6dc-8196ca51714a"
      },
      "outputs": [],
      "source": [
        "num_rounds = 20\n",
        "%time xgclassifier = xgb.train(params, xg_train, num_rounds, watchlist, feval = evalerror, early_stopping_rounds= 20, verbose_eval = 10)\n",
        "del(xg_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a7fa74fe-ef3f-29e4-c126-22140752a1da"
      },
      "outputs": [],
      "source": [
        "preds = xgclassifier.predict(xg_test, ntree_limit=xgclassifier.best_iteration)\n",
        "print ('RMSLE Score:', rmsle(y_test, preds)) "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "38d2a42d-c5d0-e038-2c6c-581a38262981"
      },
      "source": [
        "If you want to load the data and train the XGBoost Chunkwise\n",
        "xgb_model=xgclassifier argument is used\n",
        "eg:\n",
        "chunksize = 2000000\n",
        "num_rounds = 50\n",
        "\n",
        "    enter code here\n",
        "\n",
        "    for train in pd.read_csv('../input/train.csv', chunksize=chunksize,     iterator=True,\n",
        "                 dtype  = dtype,warn_bad_lines= True, engine='c'):\n",
        "\n",
        "                 y = train['Demanda_uni_equil']\n",
        "                 X = train[test.columns.values]\n",
        "\n",
        "                 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1809)\n",
        "                 test_preds = np.zeros(test.shape[0])\n",
        "                 xg_train = xgb.DMatrix(X_train, label=y_train)\n",
        "                 xg_test = xgb.DMatrix(X_test)\n",
        "                 watchlist = [(xg_train, 'train')]\n",
        "\n",
        "                 xgclassifier = xgb.train(params, xg_train, num_rounds, watchlist, feval = evalerror, \n",
        "       early_stopping_rounds= 30, verbose_eval = 10, xgb_model=xgclassifier)\n",
        "                preds = xgclassifier.predict(xg_test, ntree_limit=xgclassifier.best_iteration)\n",
        "                print ('RMSLE Score:', rmsle(y_test, preds))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e3b17a23-a3ca-3156-f17f-d8dda463978c"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0fe149a6-4628-65f3-1540-251e49387d94"
      },
      "outputs": [],
      "source": [
        "print ('RMSLE Score:', rmsle(y_test, preds)) \n",
        "del(preds)\n",
        "del(y_test)"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}