{"metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"name": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "version": "3.6.3", "mimetype": "text/x-python", "file_extension": ".py"}}, "nbformat": 4, "cells": [{"metadata": {"_cell_guid": "f9f71093-d034-496c-837c-34f7ff152550", "_kg_hide-output": true, "collapsed": true, "_uuid": "fc8226e152911db70d02ab360e8b285dcb3dfb2d", "_kg_hide-input": true}, "execution_count": null, "source": ["import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn\n", "import math"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "a430986c-0fe6-4fb5-9fba-c7726698499b", "_kg_hide-output": true, "collapsed": true, "_uuid": "74d3b979d24ea25b9dcd7a7e6a53f8a2ed4d3c26", "_kg_hide-input": true}, "execution_count": null, "source": ["### load data\n", "\n", "sales = pd.read_csv(\n", "    '../input/transactions.csv', \n", "    parse_dates=['date'],\n", "    dtype={'store_nbr':np.uint8, 'transactions':np.uint16}\n", ")\n", "\n", "items = pd.read_csv(\n", "    '../input/items.csv',\n", "    dtype={\n", "        'item_nbr':np.uint32, \n", "        'class': np.uint16, \n", "        'perishable': np.bool\n", "    }\n", ")\n", "\n", "stores = pd.read_csv(\n", "    '../input/stores.csv',\n", ")\n", "\n", "\n", "oil = pd.read_csv(\n", "    '../input/oil.csv',\n", "    parse_dates=['date'],\n", "    dtype={'dcoilwtico':np.float16}\n", ")\n", "\n", "holidays = pd.read_csv(\n", "    '../input/holidays_events.csv',\n", "    parse_dates=['date']\n", ")\n", "\n", "train = pd.read_csv(\n", "    '../input/train.csv',\n", "    nrows=6000000,\n", "    parse_dates=['date'],\n", "    dtype={\n", "        'id':np.uint32,\n", "        'store_nbr':np.uint8,\n", "        'item_nbr': np.uint32,\n", "        'onpromotion': np.bool,\n", "        'unit_sales': np.float32\n", "    }\n", ")"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "373ed764-575b-4162-9f75-38b3e82fea2e", "_uuid": "e8482d8bf00cd3d3cd89ae024e4aada5d4668fc8"}, "cell_type": "markdown", "source": ["Let's join up the training data to the stores/items/transactions:"]}, {"metadata": {"_cell_guid": "c785e002-0d01-4abf-bab5-eea6f6d1c9de", "_kg_hide-output": true, "collapsed": true, "_uuid": "3e6014f58f369c8628b32b6db7953d065c4596c9", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items = train.merge(items, right_on='item_nbr', left_on='item_nbr', how='left')\n", "train_items_stores = train_items.merge(stores, right_on='store_nbr', left_on='store_nbr', how='left')\n", "train_items_stores_sales = train_items.merge(sales, right_on=['store_nbr', 'date'], left_on=['store_nbr', 'date'], how='left')"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "4297b99a-d460-4728-8850-131b618c6553", "_uuid": "d5fb7bfb161af11a27990f5252fd5751a1fa1859"}, "cell_type": "markdown", "source": ["First a breakdown on the number of products in each family:"]}, {"metadata": {"_cell_guid": "d0a8d3af-6efd-4ed0-89e8-b552dc2a0dcd", "collapsed": true, "_uuid": "ff05c82141fdb0e0eae928a08c076c54720aedaf", "_kg_hide-input": true}, "execution_count": null, "source": ["items.groupby(['family']).size().plot(kind='bar',stacked=True, figsize=(13,6),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "76eed1d3-656a-4b57-ac39-aed5a0b4b206", "_kg_hide-output": false, "collapsed": true, "_uuid": "0f99f6f159d34993d8596c3811b369d2d9f5e03f", "_kg_hide-input": false}, "execution_count": null, "source": ["def calc_percent(row):\n", "    total = row.sum()\n", "    percents = []\n", "    for sales in row:\n", "        if math.isnan(sales):\n", "            percents.append(0.0)\n", "        else:\n", "            percents.append((sales/total) * 100)\n", "    return percents"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "2d403723-797c-4bc5-94dc-64a136f85cd6", "_kg_hide-output": false, "collapsed": true, "_uuid": "73553ce472d23616efa8e9bed610d99d1cb5224f", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['type', 'family']\n", ").size().unstack().apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "3dbb05b8-0fe4-4355-a0f6-75dc582e420e", "collapsed": true, "_uuid": "4ee67a3156cdade5fdfe863ed7d540d563989166"}, "cell_type": "markdown", "source": ["Same graph this time along store cluster:"]}, {"metadata": {"_cell_guid": "3bc64710-6890-4b08-9a99-f3e1929b5266", "_kg_hide-output": false, "collapsed": true, "_uuid": "621fbe1d255862c5f5cd4cf83ed9b7a4a542c7ef", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['cluster', 'family']\n", ").size().unstack().apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "fd126771-531d-4f26-a5b9-9b7136551524", "_uuid": "cc6dd87740ae15454490ec85b68b52be365e2877"}, "cell_type": "markdown", "source": ["And one more time by state:"]}, {"metadata": {"_cell_guid": "7ed695c3-5f66-4862-8b81-26f7d1500c73", "_kg_hide-output": false, "collapsed": true, "_uuid": "a9ea720b1a5194fa726941dce0e1b445e1cf0f32", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['state', 'family']\n", ").size().unstack().apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "c7f12757-79e8-4df7-8b49-7757e57cd5bc", "collapsed": true, "_uuid": "013339787bbaa5db8a2e564ba1879603b729fca5"}, "cell_type": "markdown", "source": ["The Grocery I makes up about half of all sales across the different dimiensions we've looked at... Let remove it and take a look a the rest of the catagories."]}, {"metadata": {"_cell_guid": "7ed1218b-8fa9-47a2-b61f-a5b594aa99f7", "_kg_hide-output": false, "collapsed": true, "_uuid": "527215780c2ea19fd691839c42d7a057e808d86a", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['type', 'family']\n", ").size().unstack().drop('GROCERY I', 1).apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "41abb26e-9965-4c45-873b-f7027cfdd5af", "_kg_hide-output": false, "collapsed": true, "_uuid": "5253aaaaa1408dc1327c06dd36a594066b89e9c8", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['cluster', 'family']\n", ").size().unstack().drop('GROCERY I', 1).apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "6afb2f2f-7588-4a55-afde-a8497e1d2e11", "_kg_hide-output": false, "collapsed": true, "_uuid": "2a6e7ca66bb352635ebe19a6cff6809293bd342d", "_kg_hide-input": true}, "execution_count": null, "source": ["train_items_stores.groupby(\n", "    ['state', 'family']\n", ").size().unstack().drop('GROCERY I', 1).apply(calc_percent, axis=1).plot(\n", "    kind='bar', stacked=True, colormap= 'tab20c', figsize=(12,10),  grid=False)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "401d92da-371b-4d92-b749-0282bfe51da5", "collapsed": true, "_uuid": "0d81b26940ef7c2a142ee1e33c928ba1fdc515a9"}, "execution_count": null, "source": [], "cell_type": "code", "outputs": []}], "nbformat_minor": 1}