{"metadata": {"language_info": {"pygments_lexer": "ipython3", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python", "file_extension": ".py", "nbconvert_exporter": "python", "name": "python", "version": "3.6.3"}, "kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}}, "nbformat": 4, "cells": [{"outputs": [], "cell_type": "code", "source": ["import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "import gc;gc.collect\n", "import datetime\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n"], "execution_count": null, "metadata": {"_uuid": "580eb1b005e1cc88535b2c75366015d5394e660b", "_cell_guid": "ce01e40c-eab9-4169-9ae7-4bb422c46be3"}}, {"outputs": [], "cell_type": "code", "source": ["#holidays_events = pd.read_csv('../input/holidays_events.csv')\n", "#items = pd.read_csv('../input/items.csv')\n", "#oil = pd.read_csv('../input/oil.csv')\n", "sample_submission = pd.read_csv('../input/sample_submission.csv')\n", "#stores = pd.read_csv('../input/stores.csv')\n", "test = pd.read_csv('../input/test.csv')\n", "train = pd.read_csv('../input/train.csv')\n", "#transactions = pd.read_csv('../input/transactions.csv')"], "execution_count": null, "metadata": {"_uuid": "0f257309c76da330cea62f28c7a85514aa8afe00", "_cell_guid": "b1594ce5-f41e-4bd5-b342-6f6fa900e2d5"}}, {"outputs": [], "cell_type": "code", "source": ["train[\"date\"] =  pd.to_datetime(train[\"date\"])\n", "train_2016 = train[train[\"date\"].dt.year == 2016]\n", "date_list=np.unique(train_2016[\"date\"])\n", "del train; gc.collect()\n"], "execution_count": null, "metadata": {"_uuid": "e343a6ec67ebf87bbcd39f0d4ac27ade8e3da8d9", "_cell_guid": "4fd845bb-5caf-4ef9-86c0-89816d5f4bc2"}}, {"cell_type": "markdown", "source": ["## Data Preparation"], "metadata": {"_uuid": "327010b5009e851e97e0ec72d9b0608dba3e78d1", "_cell_guid": "fb35d0e2-97e8-465c-9a9d-a9edbfc6bbba"}}, {"outputs": [], "cell_type": "code", "source": ["#train_2016[\"Year\"]=train_2016[\"date\"].dt.year\n", "train_2016[\"Month\"]=train_2016[\"date\"].dt.month\n", "#train_2016[\"Day\"]=train_2016[\"date\"].dt.day"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "754d4908a3d7de225a3c42eb7d9c4a2ce128fbe4", "_cell_guid": "de177034-7c6c-4f9c-a8e7-2c019ab47ec2"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016.head()\n", "date_list=pd.DataFrame(date_list)\n", "date_list[\"day\"]=range(1,np.shape(date_list)[0]+1)\n", "date_list[\"day\"]=date_list[\"day\"]%7\n", "date_list.columns=[\"date\",\"day of week\"]\n", "\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "3c0469aafaa8d35bf0b4f7d952335e373be790b6", "_cell_guid": "12caeeb6-d5bb-4ad9-bc49-58a6376a86f6"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016=pd.merge(train_2016,date_list,on=[\"date\"],how=\"left\")"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "4d1eef72ac22e123a7a8a241fabc72b23ba7a437", "_cell_guid": "8b563663-a580-46bf-aeb4-cf80e4247a44"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016.drop(\"date\",axis=1,inplace=True)\n", "train_2016.drop(\"id\",axis=1,inplace=True)\n", "train_2016.reset_index(inplace=True)\n", "train_2016.drop(\"index\",axis=1,inplace=True)"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "3c2c0f57659a56920ac859d4e92bb495375031c0", "_cell_guid": "b7eaf46a-a009-4e81-a465-14731df7ec00"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016.head()"], "execution_count": null, "metadata": {"_uuid": "4f4c7b670ec9402ca45dd17ab6216207df9ed35c", "_cell_guid": "92e87d04-2225-4d8f-9592-ae041ecafe19"}}, {"outputs": [], "cell_type": "code", "source": ["#store_list=pd.DataFrame(np.unique(train_2016[\"store_nbr\"]))\n", "#item_list=pd.DataFrame(np.unique(train_2016[\"item_nbr\"]))\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "2f725025f990da49ae150ec215c92fdd98461d31", "_cell_guid": "fef08135-1fbc-4ca7-a252-867bb4e4db81"}}, {"outputs": [], "cell_type": "code", "source": ["#date_list[\"Month\"]=date_list[0].dt.month\n", "#date_list\n", "del date_list"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "563da2f452bcb9f53870394dc67cde91184f2e0d", "_cell_guid": "d97203d5-9e8a-4b7a-ae07-1c690924bf7d"}}, {"cell_type": "markdown", "source": ["## Memory Reduction "], "metadata": {"_uuid": "06357cb0b3475d7f4280a7f8d2b550a51866197b", "_cell_guid": "d14153f5-4ca2-4e03-a5d1-c15594d0d605"}}, {"outputs": [], "cell_type": "code", "source": ["memo = train_2016.memory_usage(index=True).sum()\n", "print(memo/ 1024**2,\" MB\")"], "execution_count": null, "metadata": {"_uuid": "a50337fcbb7492b1f4a8453cecc5d436b28f463a", "_cell_guid": "9b52339f-9e29-4e05-a535-674b047cb0c2"}}, {"outputs": [], "cell_type": "code", "source": ["print(train_2016.dtypes)"], "execution_count": null, "metadata": {"_uuid": "e7715d6c223d447f37e55c7ed5df4ff03333293c", "_cell_guid": "a3f6bc89-cce0-4c1f-a6fb-f91d2de1d07c"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016['store_nbr'] = train_2016['store_nbr'].astype(np.int8)\n", "train_2016['item_nbr'] = train_2016['item_nbr'].astype(np.int32)\n", "train_2016['unit_sales'] = train_2016['unit_sales'].astype(np.int32)\n", "train_2016['Month'] = train_2016['Month'].astype(np.int8)\n", "train_2016['day of week'] = train_2016['day of week'].astype(np.int8)"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "39018a941b7d4d95e941a3a48ef78605ef1a1bdb", "_cell_guid": "6982ae03-15c3-4b16-bd09-208ea1034cae"}}, {"outputs": [], "cell_type": "code", "source": ["memo = train_2016.memory_usage(index=True).sum()\n", "print(memo/ 1024**2,\" MB\")"], "execution_count": null, "metadata": {"_uuid": "248f4bdb9525857443a2b158c6dc13f77e63e90e", "_cell_guid": "45294bd5-b3cb-4261-b631-b28b402205ae"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016.head()"], "execution_count": null, "metadata": {"_uuid": "0dc45e7c38d5663f92798b64cabbda5c642ed8ed", "_cell_guid": "fbbf8bec-d8d7-4a2f-a764-4baa10b34908"}}, {"outputs": [], "cell_type": "code", "source": ["onpromotion={True : 1,False: 0}\n", "train_2016[\"onpromotion\"]=train_2016[\"onpromotion\"].map(onpromotion)\n", "train_2016.head()"], "execution_count": null, "metadata": {"_uuid": "9725ebb8008aa5f82669b116b20047c9fd8ac288", "_cell_guid": "f41d3ffd-da17-4fa7-bb68-2913db9d6558"}}, {"outputs": [], "cell_type": "code", "source": ["memo = train_2016.memory_usage(index=True).sum()\n", "print(memo/ 1024**2,\" MB\")"], "execution_count": null, "metadata": {"_uuid": "ddf24d2671a3d80deaa6038b5e837063841707ce", "_cell_guid": "fb6fd899-3788-4c6d-a6a5-583e5c17d933"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016['onpromotion'] = train_2016['onpromotion'].astype(np.bool)"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "c6260e4eddfc173f6ee5ca963572544bbb8e745a", "_cell_guid": "1c1ee873-02f3-4afc-982e-4579a65c2c69"}}, {"outputs": [], "cell_type": "code", "source": ["memo = train_2016.memory_usage(index=True).sum()\n", "print(memo/ 1024**2,\" MB\")"], "execution_count": null, "metadata": {"_uuid": "47c328fb4200fde80f8d2ae5b1e7dea0f7dd1dc1", "_cell_guid": "d1710f21-8482-4044-82a7-cebfb006b214"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016.head()"], "execution_count": null, "metadata": {"_uuid": "4cb152b3ea588787b040b0d49ae50a54770391f6", "_cell_guid": "3bcc4e1c-43cc-4fb7-99bb-70914c9b068c"}}, {"outputs": [], "cell_type": "code", "source": ["print(np.shape(np.unique(train_2016[\"item_nbr\"]))[0]*np.shape(np.unique(train_2016[\"store_nbr\"]))[0]*365)\n", "np.shape(train_2016)[0]\n", "\n"], "execution_count": null, "metadata": {"_uuid": "64aa512358e24b168a3422ad6145ac578950b28d", "_cell_guid": "50cd8e79-863f-43b1-a737-fb2e7c66513b"}}, {"cell_type": "markdown", "source": ["## Neural Network"], "metadata": {"_uuid": "1c36ffe0d91574459afcf08ac575f615f98cfe8b", "_cell_guid": "5ebd4e85-0648-462f-b32b-be0cb6efd594"}}, {"outputs": [], "cell_type": "code", "source": ["import numpy\n", "import pandas\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Dropout, Conv2D\n", "from keras.wrappers.scikit_learn import KerasRegressor\n", "from sklearn.model_selection import cross_val_score\n", "from sklearn.model_selection import KFold\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.pipeline import Pipeline"], "execution_count": null, "metadata": {"_uuid": "1974ed2627d1a4c7bd16b99d652b4cb975d5b20d", "_cell_guid": "dc1d0d9e-1b38-45fa-9cb9-99c640d14a13"}}, {"cell_type": "markdown", "source": ["Split the data"], "metadata": {"_uuid": "c995db3164504232446ba83b822178bdc1088389", "_cell_guid": "e1c50f69-48a2-4f25-8c99-04c1bf96be1f"}}, {"outputs": [], "cell_type": "code", "source": ["train_2016 = train_2016[train_2016[\"store_nbr\"] <= 25]\n", "train_2016 = train_2016[train_2016[\"item_nbr\"] == 105574]\n", "train, test =train_2016[train_2016[\"Month\"] <= 11], train_2016[train_2016[\"Month\"] > 11]\n", "TrainY=train[\"unit_sales\"].values\n", "TrainX=train.drop(\"unit_sales\",axis=1).values\n", "\n", "TestY=test[\"unit_sales\"].values\n", "TestX=test.drop(\"unit_sales\",axis=1).values\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "673abf6a1bb12dc37217895a647367ea6906a756", "_cell_guid": "32087243-3fd4-47a9-9b79-cb87bf8c7fc4"}}, {"outputs": [], "cell_type": "code", "source": ["    model = Sequential()\n", "    model.add(Dense(50, input_dim=5, activation='relu'))\n", "    model.add(Dense(1,))\n", "    # Compile model\n", "    model.compile(loss='mean_squared_error', optimizer='adam')\n", "\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "d578a17a4e1ad547244a4cd3968a2763ebd5d1eb", "_cell_guid": "0baceed2-b11c-4ae9-ac40-a613d2e33801"}}, {"outputs": [], "cell_type": "code", "source": ["model.fit(TrainX, TrainY,\n", "          batch_size=10,epochs=20,verbose=1,\n", "          validation_data=(TestX, TestY))"], "execution_count": null, "metadata": {"_uuid": "002832a279e1940a5a754f090a224427367863bb", "_cell_guid": "56410949-6756-4975-be18-4c52db65c038"}}, {"outputs": [], "cell_type": "code", "source": ["testPredict = model.predict(TestX)\n", "result=pd.DataFrame(testPredict)\n", "result[\"TestY\"]=TestY\n", "print(result)"], "execution_count": null, "metadata": {"_uuid": "53c5d0c116b85ffefde517d4084158c559b49724", "_cell_guid": "8093ffb2-d01b-450d-88b6-bd3a7db05bdd"}}, {"outputs": [], "cell_type": "code", "source": [], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "48178bd0896336c65c3ca2f11519af12b897bf4c", "_cell_guid": "96da2993-1431-4052-bb44-02dde06d4a26"}}, {"outputs": [], "cell_type": "code", "source": [], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "b35895c0928dcf270040858047ebef4c8137b551", "_cell_guid": "a4bb018f-0563-4444-9668-f912abd3b869"}}], "nbformat_minor": 1}