{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4a6169ea-950f-8362-b9fa-2dd466f4176a"
      },
      "source": [
        "code from Faron's script :\n",
        "https://www.kaggle.com/mmueller/bosch-production-line-performance/road-2-0-4/code"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "471df311-59f1-2db3-63a1-e3b98d84dec2"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import xgboost as xgb\n",
        "\n",
        "DATA_DIR = \"../input\"\n",
        "\n",
        "ID_COLUMN = 'Id'\n",
        "TARGET_COLUMN = 'Response'\n",
        "\n",
        "SEED = 0\n",
        "CHUNKSIZE = 50000\n",
        "NROWS = 250000\n",
        "\n",
        "TRAIN_NUMERIC = \"{0}/train_numeric.csv\".format(DATA_DIR)\n",
        "TRAIN_DATE = \"{0}/train_date.csv\".format(DATA_DIR)\n",
        "\n",
        "TEST_NUMERIC = \"{0}/test_numeric.csv\".format(DATA_DIR)\n",
        "TEST_DATE = \"{0}/test_date.csv\".format(DATA_DIR)\n",
        "\n",
        "FILENAME = \"etimelhoods\"\n",
        "\n",
        "train = pd.read_csv(TRAIN_NUMERIC, usecols=[ID_COLUMN, TARGET_COLUMN], nrows=NROWS)\n",
        "test = pd.read_csv(TEST_NUMERIC, usecols=[ID_COLUMN], nrows=NROWS)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ff6e610c-8ff1-94db-5bc6-6c97940761e8"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "16d66683-658d-23eb-31b9-484c9468dac1"
      },
      "outputs": [],
      "source": [
        "train[\"StartTime\"] = -1\n",
        "test[\"StartTime\"] = -1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ff9f0f77-f280-bc6f-f69c-2f1d18c8a2bf"
      },
      "outputs": [],
      "source": [
        "nrows = 0\n",
        "for tr, te in zip(pd.read_csv(TRAIN_DATE, chunksize=CHUNKSIZE), pd.read_csv(TEST_DATE, chunksize=CHUNKSIZE)):\n",
        "    feats = np.setdiff1d(tr.columns, [ID_COLUMN])\n",
        "\n",
        "    stime_tr = tr[feats].min(axis=1).values\n",
        "    stime_te = te[feats].min(axis=1).values\n",
        "\n",
        "    train.loc[train.Id.isin(tr.Id), 'StartTime'] = stime_tr\n",
        "    test.loc[test.Id.isin(te.Id), 'StartTime'] = stime_te\n",
        "\n",
        "    nrows += CHUNKSIZE\n",
        "    if nrows >= NROWS:\n",
        "        break"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c3909723-a44a-6fac-133a-b87afba4d38d"
      },
      "outputs": [],
      "source": [
        "stime_te"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d8db82ce-2bd0-44db-c8fc-645a9e065898"
      },
      "outputs": [],
      "source": [
        "ntrain = train.shape[0]\n",
        "train_test = pd.concat((train, test)).reset_index(drop=True).reset_index(drop=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b689901b-49a0-ce77-0dba-3aea28bc7cc0"
      },
      "outputs": [],
      "source": [
        "train_test.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "66bdfd23-cc36-469e-1b1b-1768987df427"
      },
      "outputs": [],
      "source": [
        "train_test.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9b0865fd-d183-2d96-e40f-7766685c7b0f"
      },
      "outputs": [],
      "source": [
        "train_test['0_\u00af\\_(\u30c4)_/\u00af_1'] = train_test[ID_COLUMN].diff().fillna(9999999).astype(int)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d5c4f2ad-755a-6733-4de2-d625184f0eb3"
      },
      "outputs": [],
      "source": [
        "train_test['0_\u00af\\_(\u30c4)_/\u00af_2'] = train_test[ID_COLUMN].iloc[::-1].diff().fillna(9999999).astype(int)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b33ce023-864d-1a76-81ef-f026c0e38734"
      },
      "outputs": [],
      "source": [
        "train_test = train_test.sort_values(by=['StartTime', 'Id'], ascending=True)\n",
        "\n",
        "train_test['0_\u00af\\_(\u30c4)_/\u00af_3'] = train_test[ID_COLUMN].diff().fillna(9999999).astype(int)\n",
        "train_test['0_\u00af\\_(\u30c4)_/\u00af_4'] = train_test[ID_COLUMN].iloc[::-1].diff().fillna(9999999).astype(int)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0b7c631d-08a4-a53e-5edf-b5952aca00ea"
      },
      "outputs": [],
      "source": [
        "train_test.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a114ef80-4ad3-8eec-88b5-01f3aee5d32f"
      },
      "outputs": [],
      "source": [
        "train_test = train_test.sort_values(by=['index']).drop(['index'], axis=1)\n",
        "train = train_test.iloc[:ntrain, :]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e67f645f-df6a-4da7-9036-99d1dedc0273"
      },
      "outputs": [],
      "source": [
        "features = np.setdiff1d(list(train.columns), [TARGET_COLUMN, ID_COLUMN])\n",
        "\n",
        "y = train.Response.ravel()\n",
        "train = train[features]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9598e24f-f343-c5ac-184c-e3842a881276"
      },
      "outputs": [],
      "source": [
        "features"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "38ecd8d9-465b-e7d7-46ee-2978d5d32b99"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b1f03623-041b-1248-6bca-da86fb1f281a"
      },
      "outputs": [],
      "source": [
        "\n",
        "print('train: {0}'.format(train.shape))\n",
        "prior = np.sum(y) / (1.*len(y))\n",
        "\n",
        "xgb_params = {\n",
        "    'seed': 0,\n",
        "    'silent': 1,\n",
        "    'subsample': 0.7,\n",
        "    'learning_rate': 0.1,\n",
        "    'objective': 'binary:logistic',\n",
        "    'max_depth': 4,\n",
        "    'num_parallel_tree': 1,\n",
        "    'min_child_weight': 2,\n",
        "    'eval_metric': 'auc',\n",
        "    'base_score': prior\n",
        "}\n",
        "\n",
        "\n",
        "dtrain = xgb.DMatrix(train[[\"0_\u00af\\_(\u30c4)_/\u00af_3\", \"0_\u00af\\_(\u30c4)_/\u00af_4\"]], label=y)\n",
        "res = xgb.cv(xgb_params, dtrain, num_boost_round=10, nfold=4, seed=0, stratified=True,\n",
        "             early_stopping_rounds=10, verbose_eval=1, show_stdv=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e8f31c0e-5ce1-9f60-d1f1-7cba25cb4583"
      },
      "outputs": [],
      "source": [
        "res"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8bea55d-2536-b191-6a8a-7bd0b8b39a28"
      },
      "outputs": [],
      "source": [
        "model = xgb.train(xgb_params, dtrain, num_boost_round=10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5fcfa95-8e94-4e53-87ed-e3d1b8046f98"
      },
      "outputs": [],
      "source": [
        "model.get_fscore()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c815494d-07ab-7870-b09a-8a86fbd1420f"
      },
      "outputs": [],
      "source": [
        "xgb.plot_importance(model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b12f903d-d654-4223-dc6e-ea3068ccc5fb"
      },
      "source": [
        "Looking at StartTime:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4505c57b-1ea3-f21b-8838-048370e399d2"
      },
      "outputs": [],
      "source": [
        "train[\"Response\"] = y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d2b97d64-012c-1e9b-feb5-650819829e83"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "70ae1d7a-8ed5-61a0-5d84-e9d6527a44c7"
      },
      "outputs": [],
      "source": [
        "np.log(1000000 + train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"]).hist()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "565ac640-aae0-4c42-61c6-bf95025d0f89"
      },
      "outputs": [],
      "source": [
        "import seaborn as sns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d0dbbdc9-2ffe-07d2-11ae-494230c47395"
      },
      "outputs": [],
      "source": [
        "train = train[train[\"0_\u00af\\_(\u30c4)_/\u00af_3\"] < 1000000]\n",
        "train = train[train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"] < 1000000]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d2823ef5-81bc-56f7-333b-2428b12d71b0"
      },
      "outputs": [],
      "source": [
        "train[\"log_feat_4\"] = np.log(1000000 + train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "991ab15e-4f9f-87ff-f5a7-d9ddcf84cdb4"
      },
      "outputs": [],
      "source": [
        "train.hist(column =\"log_feat_4\", by = \"Response\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1392c844-0f31-9cc1-c81f-6c99ed3bb3da"
      },
      "outputs": [],
      "source": [
        "train.hist(column =\"0_\u00af\\_(\u30c4)_/\u00af_3\", by = \"Response\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "27de3425-47f8-6064-daa9-311e8f6cdf59"
      },
      "outputs": [],
      "source": [
        "train[\"0_\u00af\\_(\u30c4)_/\u00af_3\"][:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bbf19ed5-f213-9da0-2068-76c3713e5067"
      },
      "outputs": [],
      "source": [
        "train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"].value_counts()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5413151a-7764-670a-6278-836165984e97"
      },
      "outputs": [],
      "source": [
        "train[train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"] != -1].hist(column =\"0_\u00af\\_(\u30c4)_/\u00af_4\", by = \"Response\", bins = 30)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7a4efa41-97fa-4689-3afb-93115ab0e31f"
      },
      "outputs": [],
      "source": [
        "sns.boxplot(x = \"Response\", y = \"0_\u00af\\_(\u30c4)_/\u00af_4\",\n",
        "            data = train[train[\"0_\u00af\\_(\u30c4)_/\u00af_4\"] != -1])"
      ]
    }
  ],
  "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
}