{"metadata": {"language_info": {"pygments_lexer": "ipython3", "version": "3.6.3", "name": "python", "file_extension": ".py", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "nbconvert_exporter": "python"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat_minor": 1, "cells": [{"cell_type": "markdown", "source": ["Let's analyse new items in stores by date !"], "metadata": {}}, {"cell_type": "code", "source": ["import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline"], "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "bff0410e-4e1c-4db5-8eff-1375240a56b4", "collapsed": true, "_uuid": "961283656e17f0abe4d9ce847e91260fa0c2d825"}}, {"cell_type": "code", "source": ["# load train\n", "types = {'id': 'int64','item_nbr': 'int32','store_nbr': 'int8','unit_sales': 'float32','onpromotion': bool,}\n", "df_train= pd.read_csv(\"../input/train.csv\", parse_dates = ['date'], dtype = types, infer_datetime_format = True)"], "outputs": [], "execution_count": null, "metadata": {"_kg_hide-output": true}}, {"cell_type": "code", "source": ["# load test\n", "types = {'id': 'int64','item_nbr': 'int32','store_nbr': 'int8','onpromotion': bool,}\n", "df_test = pd.read_csv(\"../input/test.csv\", parse_dates = ['date'], dtype = types, infer_datetime_format = True)"], "outputs": [], "execution_count": null, "metadata": {"collapsed": true, "_kg_hide-output": true}}, {"cell_type": "code", "source": ["# append dataframes and filter columns\n", "df_t = df_train.append(df_test)\n", "df_t = df_t.filter(items=['date', 'item_nbr', 'store_nbr'])"], "outputs": [], "execution_count": null, "metadata": {"collapsed": true}}, {"cell_type": "markdown", "source": ["## Count of new (items, stores) by date"], "metadata": {}}, {"cell_type": "code", "source": ["f, ax = plt.subplots(1, 1, figsize=(20, 10))\n", "df_t_is = df_t.groupby(by=['item_nbr', 'store_nbr']).agg({'date':'min'}).groupby(by='date').size()\n", "df_t_is.plot(ax=ax)"], "outputs": [], "execution_count": null, "metadata": {"scrolled": false}}, {"cell_type": "code", "source": ["print(\"median count of new (item, store) by day: {}\".format(df_t_is.median()))\n", "df_t_is.sort_values(ascending=False).head(10)"], "outputs": [], "execution_count": null, "metadata": {}}, {"cell_type": "markdown", "source": ["As we can see there is an unusual number of new items in stores the first day of the test dataset.  \n", "It seems that history of sales for a subset of stores was removed from the train set to simulate new item in store.  \n", "Let's have a quick look at new item across all stores."], "metadata": {}}, {"cell_type": "markdown", "source": ["## Count of new items by date"], "metadata": {}}, {"cell_type": "code", "source": ["f, ax = plt.subplots(1, 1, figsize=(20, 10))\n", "df_t_i = df_t.groupby(by=['item_nbr']).agg({'date':'min'}).groupby(by='date').size()\n", "df_t_i.plot(ax=ax)"], "outputs": [], "execution_count": null, "metadata": {}}, {"cell_type": "code", "source": ["print(\"median count of new items by day: {}\".format(df_t_i.median()))\n", "df_t_i.sort_values(ascending=False).head(10)"], "outputs": [], "execution_count": null, "metadata": {}}, {"cell_type": "markdown", "source": ["There are not a lot of new items across all stores  on the first day of the test dataset (compared to new items in some stores)!"], "metadata": {}}], "nbformat": 4}