{"metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "pygments_lexer": "ipython3", "file_extension": ".py", "nbconvert_exporter": "python", "version": "3.6.3", "name": "python"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}, "nbformat_minor": 1, "nbformat": 4, "cells": [{"metadata": {"_uuid": "6916b0077c4dc12b7b3b7ad29a0a55282407a2cf", "collapsed": true, "_cell_guid": "8581ea99-fbf2-46f2-8aa6-40b34267d56d"}, "cell_type": "markdown", "source": ["### This is a kernel to learn the basics of an end-to-end pipeline for Corparacion Favorita Compeition\n", "### Source code credit: https://www.kaggle.com/ceshine/lgbm-starter/ \n", "### Referencing: https://www.kaggle.com/aharless/dissecting-ceshine-lee-s-lgbm-kernel"]}, {"metadata": {"_uuid": "c0a410fb5d56846bd5f11fe0d55d33e2829a24ee", "collapsed": true, "_cell_guid": "c0273c3b-fe7b-47f8-9802-709de69abff2"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["from datetime import date, timedelta\n", "\n", "import pandas as pd\n", "import numpy as np\n", "from sklearn.metrics import mean_squared_error\n", "import lightgbm as lgb"]}, {"metadata": {"_uuid": "3a6a8b0d611d3c21d975691c5f2a0bb66a43b0f9", "collapsed": true, "_cell_guid": "cfbfbd6c-8759-4272-9f52-80bef407644a"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_train = pd.read_csv(\n", "    '../input/train.csv', usecols=[1, 2, 3, 4, 5],\n", "    dtype={'onpromotion': bool},\n", "    converters={'unit_sales': lambda u: np.log1p(\n", "        float(u)) if float(u) > 0 else 0},\n", "    parse_dates=[\"date\"],\n", "    skiprows=range(1, 66458909)  # 2016-01-01\n", ")\n", "df_test = pd.read_csv(\n", "    \"../input/test.csv\", usecols=[0, 1, 2, 3, 4],\n", "    dtype={'onpromotion': bool},\n", "    parse_dates=[\"date\"]  # , date_parser=parser\n", ").set_index(\n", "    ['store_nbr', 'item_nbr', 'date']\n", ")\n", "items = pd.read_csv(\n", "    \"../input/items.csv\",\n", ").set_index(\"item_nbr\")"]}, {"metadata": {"_uuid": "c5ac7495265364f2581e52910ef552db07c97c08", "_cell_guid": "26edab45-b539-4547-87d4-9a97300f7da5"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["items.head()"]}, {"metadata": {"_uuid": "fbc633e4fb765a0b2c3dc06d807e29bd7ab28e5f", "collapsed": true, "_cell_guid": "3d5f82f7-66ce-49f6-bae4-769e181539ac"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["# Getting the observation from the 11 weeks after 2017-5-31\n", "df_2017 = df_train[df_train.date.isin(\n", "    pd.date_range(\"2017-05-31\", periods=7 * 11))]\n", "df_2017_0 = df_train[df_train.date.isin(\n", "    pd.date_range(\"2017-05-31\", periods=7 * 11))].copy()\n", "del df_train\n", "df_2017.head()"]}, {"metadata": {"_uuid": "62fd5d481d40e7a8a33b90a24dc1ce74238a8b33", "_cell_guid": "a64b6b23-33f7-43d7-8b88-f5d04cce012e"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["promo_2017_train = df_2017.set_index(\n", "    [\"store_nbr\", \"item_nbr\", \"date\"])[[\"onpromotion\"]].unstack(\n", "        level=-1).fillna(False)\n", "promo_2017_train.head()"]}, {"metadata": {"_uuid": "893a06196e40459854243d8113a4dd3661a9ae4b", "_cell_guid": "1d34c047-d33b-4676-81b2-4923f4e59042"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["promo_2017_train.columns = promo_2017_train.columns.get_level_values(1)\n", "promo_2017_train.head()"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["promo_2017_test = df_test[[\"onpromotion\"]].unstack(level=-1).fillna(False)\n", "promo_2017_test.columns = promo_2017_test.columns.get_level_values(1)\n", "promo_2017_test.head()"]}, {"metadata": {"_uuid": "4cb8a4eb9780a576dc95f9a9fb3b0ca74aa82a0c", "_cell_guid": "68be0696-1146-4b61-aea6-9c385a39a361"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["promo_2017_test = promo_2017_test.reindex(promo_2017_train.index).fillna(False)\n", "promo_2017_test.head()"]}, {"metadata": {"_uuid": "0f3d54fb3b38ab9e62b6bbf8a80a7c1c1a0982b8", "_cell_guid": "66f58ff0-5632-4069-bdca-a6c9d532e3da"}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["# create a big table of on-promotion history, by adding the train to the test\n", "promo_2017 = pd.concat([promo_2017_train, promo_2017_test], axis=1)\n", "promo_2017.head()\n", "del promo_2017_test, promo_2017_train"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_2017 = df_2017.set_index(\n", "    [\"store_nbr\", \"item_nbr\", \"date\"])[[\"unit_sales\"]].unstack(\n", "        level=-1).fillna(0)\n", "df_2017.columns = df_2017.columns.get_level_values(1)\n", "# get_level_values(1) remove the level 0 index - unit_sales, so that the table could be isomorphic\n", "df_2017.head()"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_2017.head()"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_2017.shape"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["# items = items.reindex(df_2017.index.get_level_values(1))\n", "items = items.reindex(df_2017.index)\n", "items.head()"]}, {"metadata": {}, "execution_count": null, "cell_type": "code", "outputs": [], "source": ["items.shape"]}, {"metadata": {"collapsed": true}, "execution_count": null, "cell_type": "code", "outputs": [], "source": []}]}