{"cells": [{"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "e4f0ef24-1da4-49be-8057-7eac4ef6173a", "_uuid": "238d61d2dfc66561a5e388d888b3fc638e8dc386"}, "source": ["# This Python 3 environment comes with many helpful analytics libraries installed\n", "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n", "# For example, here's several helpful packages to load in \n", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "execution_count": 1}, {"cell_type": "markdown", "metadata": {"_cell_guid": "bc78407e-862b-4650-8c8b-44ca5197adc9", "_uuid": "eb4d5e5b21fc3c928a73b4704aca0c90b9f876cd"}, "source": ["First of all we are going to check out the train.csv which, according to the input data description:\n", "\n", "includes the target unit_sales by date, store_nbr, and item_nbr and a unique id to label rows.\n", "\n", "The target unit_sales can be integer (e.g., a bag of chips) or float (e.g., 1.5 kg of cheese).\n", "Negative values of unit_sales represent returns of that particular item.\n", "\n", "The onpromotion column tells whether that item_nbr was on promotion for a specified date and store_nbr.\n", "Approximately 16% of the onpromotion values in this file are NaN.\n", "\n", "NOTE: The training data does not include rows for items that had zero unit_sales for a store/date combination. There is no information as to whether or not the item was in stock for the store on the date, and teams will need to decide the best way to handle that situation. Also, there are a small number of items seen in the training data that aren't seen in the test data."]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "e9f76522-93fc-4140-85a0-74fc56cecee1", "_uuid": "b2dd4a0c1706d874f4f88ac27b50dea5c8a20c8b"}, "source": ["train = pd.read_csv('../input/train.csv')"], "execution_count": 2}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "734421fa-f8f8-4d13-80c1-fa9a747cb280", "_uuid": "bda149e2ddb87887802d9ee90db712de34044167"}, "source": ["train.head()"], "execution_count": 3}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "39673ce6-a7a5-4766-a528-d8aebf87b3ff", "_uuid": "2813f883d5962c848775cada7543db7752bb2890"}, "source": ["print(\"we are dealing with \",len(train), \"rows\")"], "execution_count": 4}, {"cell_type": "markdown", "metadata": {"_cell_guid": "c660728a-2823-4c45-bcbb-32cba7d77023", "_uuid": "b9a0884f06797d96608678c5b5ae764e23f8d690"}, "source": ["which are TOOOO much ! so we are going to work with  sample"]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "1f2240f4-e6b9-4b6b-9361-ee046b7767ee", "_uuid": "2c17ffed9532b7f8c10fb276575d122acf3e36ee", "collapsed": true}, "source": ["strain = train.sample(frac=0.01,replace=True)"], "execution_count": 5}, {"cell_type": "markdown", "metadata": {"_cell_guid": "a84e0ac4-0fd6-403f-b23f-0a7419d7481e", "_uuid": "475796fbc6c9dfa56d00023c8e240d8aa7641bcc"}, "source": ["## Items"]}, {"cell_type": "markdown", "metadata": {"_cell_guid": "10fb335c-03bd-4bb4-8c1d-9760071f0987", "_uuid": "a410d91eff9dfbf745c98da65d3beadb39d0880c"}, "source": []}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "dc1dc9f8-4162-4958-868f-059aec0b0149", "_uuid": "80cb85b4cab69456977a5c08905a845340f724d3", "collapsed": true}, "source": ["items = pd.read_csv('../input/items.csv')"], "execution_count": 6}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "97585d83-ccdd-467f-9c2b-8e529a0ca5ea", "_uuid": "6da07f6dadbf9316f58aa1a1051fff22e2573537"}, "source": ["items.head()"], "execution_count": 7}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "f8d8ce9f-e634-46c3-820b-130486e337c9", "_uuid": "ad4dd1e03a75d7aee49861078129d5e8fb78ba9b"}, "source": ["print(\"we are dealing with\",len(items['family'].unique()),\"families of products\")"], "execution_count": 8}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "2874d030-1e84-4742-85f7-5ee29dcbe462", "_uuid": "bb474ac58d8fd57d2cad7b4b10562d7ae4bca89f"}, "source": ["f,axarr = plt.subplots(1,1,figsize=(15,10))\n", "plt.xticks(rotation='vertical')\n", "fhist = items.groupby(['family'],as_index=False).count()\n", "sns.barplot(x=fhist['family'],y=fhist['class'])\n"], "execution_count": 9}, {"cell_type": "markdown", "metadata": {"_cell_guid": "720e98c7-58bc-489d-b76a-7efdc9296a5d", "_uuid": "2898a37dfcd903a469f29d32c0c177a9f899bff3"}, "source": ["## Stores"]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "e8f6d82c-cbb6-4946-a34a-a8b50c2a0713", "_uuid": "600421d065816ad97f841a8e7b818f7b767d0bfd"}, "source": ["stores = pd.read_csv('../input/stores.csv')\n", "stores.head()"], "execution_count": 10}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "b42ae389-aee3-4d3a-b794-aad9515771a1", "_uuid": "8ad4b3dac23eb1eebf28b788558e352c97a2c7f8"}, "source": ["print(\"we are dealing with\",len(stores['city'].unique()),'cities and',len(stores['state'].unique()),'states')"], "execution_count": 11}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "3c5ab231-9943-4281-bf84-121802a7a9ca", "_uuid": "a6b5a9f9ed91b3db9332b41ba9a63269c83bbb33"}, "source": ["f,axar = plt.subplots(1,1,figsize=(15,10))\n", "plt.xticks(rotation='vertical')\n", "shist = stores.groupby(['city'],as_index=False).count()\n", "sns.barplot(x=shist['city'],y=shist['state'])"], "execution_count": 12}, {"cell_type": "markdown", "metadata": {"_cell_guid": "d3c5ac76-7cbe-4d96-854d-b24ff1a7c0eb", "_uuid": "0d88e652d74cd5b3049fe2d497f8d28ac6e16043"}, "source": ["## Transactions"]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "ca6274a3-6a7e-48de-98d1-ad5734191750", "_uuid": "77f560ef597e14a2a7963f198b6675b7a9650478", "collapsed": true}, "source": ["trans = pd.read_csv('../input/transactions.csv')"], "execution_count": 13}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "b6915b27-81e6-485f-85e6-fade60208e07", "_uuid": "0c92f7e41cb1c5592074285768c1bca85a6163cf"}, "source": ["trans.head()"], "execution_count": 14}, {"cell_type": "markdown", "metadata": {"_cell_guid": "86027534-3d2e-42ee-ab27-23dd2d589265", "_uuid": "ef20afe0722c29ecd84dc8ad4c2ed87bc3043851"}, "source": ["## Join Data "]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "4e8912ab-c3fb-411f-aea7-2bb52f3201f6", "_uuid": "92b1c868bca6a549d103f5ebf10d39cd64a62ab3", "collapsed": true}, "source": ["df = strain.merge(right = items, on='item_nbr')"], "execution_count": 15}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "708f00e4-45f3-49f2-b542-c7486aab0f6b", "_uuid": "8da3476c57f497bc8c12d7bdfd28ab9962dc9338", "collapsed": true}, "source": ["df = df.merge(right=stores,on='store_nbr')"], "execution_count": 16}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "b9bbcfff-ebe9-4c93-81f5-9363a70c9acc", "_uuid": "c9b8c6961687088d42c534c7421c0293168ebffd"}, "source": ["df.head()"], "execution_count": 17}, {"cell_type": "markdown", "metadata": {}, "source": ["Now I am interested to see how things change in time or if there are any possible recurring patterns. So I m going to turn dates from strings to datetime objects which enables for same interesting query like day of th year, day of the week, etc."]}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "98150a77-f04d-4854-b940-41f06bf41bc9", "_uuid": "2bb80e1fd20081262bd56dc8dcb36f3727c39bf4", "collapsed": true}, "source": ["df['date']=pd.to_datetime(df['date'])"], "execution_count": 18}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "89ae1eda-10ce-4ba3-b6bd-867e6a41a26c", "_uuid": "6998ebea5c6e96a4bcea5b75b059b6a1324970e7", "collapsed": true}, "source": ["df['yday']=df['date'].dt.dayofyear"], "execution_count": 19}, {"outputs": [], "cell_type": "code", "metadata": {"collapsed": true}, "source": ["df['year']=df['date'].dt.year"], "execution_count": 22}, {"outputs": [], "cell_type": "code", "metadata": {"collapsed": true}, "source": ["df['month']=df['date'].dt.month"], "execution_count": 24}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "60396da3-a0a0-4744-a863-5ecb0df821e6", "_uuid": "35019e2c8a21c7f8a7e03a467f7f5018f8a8a146", "collapsed": true}, "source": ["stores=[df['store_nbr'].unique()]"], "execution_count": 20}, {"outputs": [], "cell_type": "code", "metadata": {"_cell_guid": "aedae270-c00f-4dc8-94b4-98fdad26c3f7", "_uuid": "fd1aa0c7dac1a04a10bf51bb7e656c10a7a6d083"}, "source": ["df.head()"], "execution_count": 26}, {"cell_type": "markdown", "metadata": {}, "source": ["Let's create groups for store, year, month and product family."]}, {"outputs": [], "cell_type": "code", "metadata": {}, "source": ["grouped = df.groupby(['store_nbr','year','month','family']).sum().drop(['id','item_nbr','class','perishable','cluster','yday'],axis=1)"], "execution_count": 30}, {"outputs": [], "cell_type": "code", "metadata": {}, "source": ["grouped.head(20)"], "execution_count": 34}, {"cell_type": "markdown", "metadata": {"_cell_guid": "59bd3eed-66d9-4913-8086-1c5f77d05a1a", "_uuid": "87cfa831777c4bdf2cd25e78c646f1e3ae6d168f"}, "source": ["As you can see we now have a table in which for each store we know how many products of each family have been sold ordered by years and months. "]}, {"outputs": [], "cell_type": "code", "metadata": {"collapsed": true}, "source": [], "execution_count": null}], "metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"name": "python", "nbconvert_exporter": "python", "file_extension": ".py", "pygments_lexer": "ipython3", "version": "3.6.3", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python"}}, "nbformat": 4, "nbformat_minor": 1}