{"cells": [{"metadata": {"_cell_guid": "ac31f742-2777-4fe4-9046-f35aa9200774", "_uuid": "0d7bd3bf62a6baacb92058d846e7b10456ecf949"}, "source": ["#### Last Update: 2017-10-21"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "c3476291-72f9-4287-bbe4-71a5271088ef", "_uuid": "6b5da72d1621f7ee928720652aab433b6bbc0fda"}, "source": ["# Introduction\n", "***\n", "On this Kernel, we will explore the data given to us by the Favorita Corporation, and how this data impacts on the sales.\n", "\n", "We will be seing some graphics and try to understand what's happening below the surface.<br>\n", "\n", "## Tidy Notebook\n", "***\n", "The idea for this notebook, is to also try to keep it as tidy as possible, in order to make it easy to understand for the reader. This includes trying to write every analysis we do on the markdown cells, and explain the code as much as possible on the code comments. ** COMMENTS ARE EVERY PROGRAMMERS FRIENDS **\n", "\n", "## About me\n", "***\n", "I've been a developer for more than 12 years and recently, just a few months ago, I got captivated by the magic of machine learning, and how easy it got for us programmers to get into it. My main motivation is learning and grow in the Machine Learning area, but in the process, I'd love to help anyone who's not been on the programming field, to make their career path easier. <br>\n", "> Finally we get to make AI!\n", "\n", "I'm from Buenos Aires, Argentina, 33 years old.<br>\n", "If you want; you can reach me on:\n", "* Linked In: https://www.linkedin.com/in/juanuhaedo/\n", "* Twitter: https://www.twitter.com/juanumusic/\n", "* GitHub: https://github.com/HarkDev/\n", "\n", "\n", "# First Steps\n", "***\n", "First we will take the mandatory steps of importing the required libaries as well as reading the CSVs into Pandas DataFrames:\n", "* Import required libraries\n", "* Read datasets"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "b6e31032-ca31-49c1-b33a-c0d5630aa3cf", "_uuid": "8fa1b57c72b97c085bfdffbb0b7b18793437fb9e"}, "source": ["**Import Libraries**"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "31f3ad56-9db0-4362-bd33-b88ef9727205", "collapsed": true, "_uuid": "67ba257f7c79a1716c8c7081beed6088b5dca020", "_kg_hide-input": false}, "source": ["import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "%matplotlib inline"], "cell_type": "code"}, {"metadata": {"_cell_guid": "a1d41d9e-1e01-41af-b2d9-fd9e266ecf65", "_uuid": "58791e22c0b01b26f206e60b032e3e5b8e5c64da"}, "source": ["**Read CSVs**\n", "***\n", "We will read the csv files, explicitly passing the datatypes of each column for memory performance. This is done with the **dtype** parameter of the `read_csv` method.<br>\n", "For dates, we will pass the `parse_dates` parameters so that the read_csv turns it's type into `datetime` type"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "a238f039-6025-4c09-bbe3-f430349d3b6b", "_kg_hide-output": false, "_uuid": "bf089f1863c89c410cb790d7c2bc2976ee48bc95", "_kg_hide-input": false}, "source": ["# For simplicity, we will create a variable that indicates the location of our input files.\n", "INPUT_PATH = '../input/'\n", "\n", "# Read the CSV files\n", "# Holiday Events Dataset\n", "df_holiday_events = pd.read_csv(INPUT_PATH + 'holidays_events.csv', parse_dates=['date'])\n", "\n", "# Items dataset\n", "df_items = pd.read_csv(INPUT_PATH + 'items.csv', dtype={'item_nbr':np.uint32, 'class': np.uint16, 'perishable': np.bool})\n", "\n", "# Oil dataset\n", "df_oil = pd.read_csv(INPUT_PATH + 'oil.csv', parse_dates=['date'], dtype={'dcoilwtico':np.float16})\n", "\n", "# Stores dataset\n", "df_stores = pd.read_csv(INPUT_PATH + 'stores.csv', dtype={'store_nbr':np.uint8, 'cluster':np.uint8})\n", "\n", "# Transactions dataset\n", "df_transactions = pd.read_csv(INPUT_PATH + 'transactions.csv', parse_dates=['date'], dtype={'store_nbr':np.uint8, 'transactions':np.uint16})\n", "\n", "# Train dataset\n", "df_train = pd.read_csv(INPUT_PATH + 'train.csv', parse_dates=['date'], dtype={'id':np.uint32, 'store_nbr':np.uint8, 'item_nbr': np.uint32, 'onpromotion': np.bool, 'unit_sales': np.float32})\n", "\n", "# Test dataset\n", "df_test = pd.read_csv(INPUT_PATH + 'test.csv', parse_dates=['date'], dtype={'id':np.uint32, 'store_nbr':np.uint8, 'item_nbr': np.uint32, 'onpromotion': np.bool})\n", "\n", "# Sample Submission dataset\n", "df_sample_submission = pd.read_csv(INPUT_PATH + 'sample_submission.csv', dtype={'id':np.uint32, 'unit_sales':np.float32})"], "cell_type": "code"}, {"metadata": {"_cell_guid": "243cfea4-f9fb-49fb-bf2d-abdc2ce1638b", "_uuid": "93f544ec3fab384631ffe8564142daa4352cd859"}, "source": ["# Exploratory Analysis\n", "***\n", "Time to get into the data world and see what we can find. We will be exploring each dataset on its own, and then we will do some cross exploration between a lot of them...\n", "\n"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "ec880dfb-0ec6-4321-b43a-ce4abb9f693a", "_uuid": "852c709d7d6ba0379c80ca52f840766223a0e1f1"}, "source": ["## Train\n", "***\n", "The train dataset contains the raw data we need to use to train our model. We will first explore this dataset in order to gain some information about how are their numbers achieved.<br>"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "46bf0482-aedc-4e2f-9cda-7d550d96062f", "_uuid": "484e7d952aa698bea6cd91ebe6debe7d8d116d72"}, "source": ["### Unit sales by Date\n", "***\n", "We can see a clear yearly growth on unit sales. This could be because of the company's growth. We'll see this more in detail on the next plots.<br>\n", "Also, an because we plotted the unit sales for products on promotion and not on promotion, we discover that not always product were on promotion, or at least this wasn't recorded. **Unit sales of Products on promotion started on about the second quarter of year 2014**"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "126c301f-7ec5-49e7-bfba-ea8ed2405fb4", "_uuid": "ed4102f06e147a3bfd95a842935d81bbabf28e9b", "_kg_hide-input": true}, "source": ["df_plot = df_train.groupby(by=['date']).agg({'unit_sales':'sum'}).reset_index()\n", "# turn date into datetime type\n", "df_plot.date = pd.to_datetime(df_plot.date)\n", "\n", "# Unit Sales by Date\n", "fig, ax = plt.subplots(1)\n", "fig.autofmt_xdate()\n", "#Set the plot figure\n", "fig.set_figheight(10)\n", "fig.set_figwidth(16)\n", "# Create the plot title\n", "plt.title('Unit Sales by Date (Total, On promotion and Not on promotion)', fontsize=20)\n", "# Plot total unit sales\n", "plt.plot(df_plot.date, df_plot.unit_sales)\n", "\n", "# plot unit sales on promotion\n", "df_plot = df_train.loc[df_train['onpromotion'] == True].groupby(by=['date']).agg({'unit_sales':'sum'}).reset_index()\n", "plt.plot(df_plot.date, df_plot.unit_sales)\n", "\n", "# plot unit sales not on promotion\n", "df_plot = df_train.loc[df_train['onpromotion'] == False].groupby(by=['date']).agg({'unit_sales':'sum'}).reset_index()\n", "plt.plot(df_plot.date, df_plot.unit_sales)\n", "\n", "# Set the legend\n", "plt.legend(['Total unit sales','On Promotion','Not On Promotion'], fontsize=14)\n", "\n", "# Set the labels\n", "plt.xlabel('Date',fontsize=14)\n", "plt.ylabel('Unit Sales',fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "57667bf8-1b21-4ecd-a369-98f97c5ac867", "_uuid": "5a9744fb17de3497d3e27557c412aa34e01a979a"}, "source": ["### Number of records by \"onpromotion.\"\n", "***\n", "We can see that there are some NaN values for on promotion. Could those be the dates where promotions were not implemented?"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "5f98e0b0-34e6-481b-8e9e-c921c51a3e37", "_uuid": "1ff2be81c790279aea52f9ee41a748742dd4ea8a", "_kg_hide-input": true}, "source": ["# Group the data by the \"onpromotion\" field. Make a count of the values and reset the index.\n", "# reset_index converts the index created (the by parameter) back into a column\n", "df_plot = df_train.fillna(-1).groupby(by=['onpromotion']).agg({'store_nbr':'count'}).reset_index()\n", "\n", "# Replace the numeric values with text (for plot readability)\n", "df_plot.onpromotion = df_plot.onpromotion.replace({0:'Not on promotion', 1:'On promotion', -1: 'Missing'})\n", "\n", "# Set the plot size\n", "plt.figure(figsize=(10,5))\n", "\n", "# Create the barplot\n", "sns.barplot(data=df_plot, x='onpromotion', y='store_nbr')\n", "\n", "# Set the plot title\n", "plt.title('# of records by On Promotion', fontsize=16)\n", "\n", "# Set the axes labels\n", "plt.xlabel('On Promotion', fontsize=14)\n", "plt.ylabel('# of records', fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "93ab4a13-85c5-4678-ab5e-3a65d304a989", "_uuid": "94e94fedc5e45daa3141121ebd8839bf1a6e8756"}, "source": ["### Number of nan \"onpromotion\" per year\n", "***\n", "Plotting the onpromotion values per year, we can see that the MISSING values of \"onpromotion\" correspond to the time when promotions weren't implemented. **This validates the first plot of Unit Sales by date**"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3d2bf93b-6331-4b11-b68f-5b9d2dcebc25", "_uuid": "f842ce46198c7cafa3cc9556d9346545201d950a", "_kg_hide-input": true}, "source": ["# Group the data by the \"onpromotion\" and date year fields. Make a count of the values and reset the index.\n", "# reset_index converts the index created (the by parameter) back into a column\n", "df_plot = df_train.fillna(-1).groupby(by=[df_train.date.dt.year, 'onpromotion']).agg({'store_nbr':'count'}).reset_index()\n", "\n", "# Replace the numeric values with text (for plot readability)\n", "df_plot.onpromotion = df_plot.onpromotion.replace({0:'Not on promotion', 1:'On promotion', -1: 'Missing'})\n", "\n", "plt.figure(figsize=(16,6))\n", "sns.barplot(data=df_plot, x='date', y='store_nbr', hue='onpromotion')\n", "plt.title('# of records by On Promotion by Year', fontsize=16)\n", "plt.xlabel('Year', fontsize=14)\n", "plt.ylabel('# of records', fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "e4955e61-bef2-4042-8dcb-5e5078246517", "_uuid": "9551330044ba0d3ddf9564e3be32942b34e5fdf9"}, "source": ["### Stores by Year\n", "***\n", "By looking at the number of stores per year, we can see that there was an increae of stores from the beggining to the last registered date, so we can infer that the increase in sales is also linked to the increase of stores."], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "1808b1ed-6d97-49ea-b676-46d8cf41f23f", "_uuid": "79a8405fb44060cc221a4f1b2128bfd5bd52513c", "_kg_hide-input": true}, "source": ["# Group by year and get the count of unique values (nunique) of store_nbr\n", "df_plot = df_train.groupby(df_train.date.dt.year)['store_nbr'].nunique().reset_index()\n", "#Set the plot figure\n", "plt.figure(figsize=(12,6))\n", "# Create the plot title\n", "plt.title('Number of Stores per Year', fontsize=20)\n", "sns.barplot(data=df_plot, x='date', y='store_nbr')\n", "plt.xlabel('Year', fontsize=14)\n", "plt.ylabel('# of Stores', fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "937435a8-d6b5-4775-a0c1-ecb0142bddfb", "_uuid": "a16eb17fb46f173bcd97072f5356f47eb89a4233"}, "source": ["## Items\n", "***\n", "The items datas|et contains information about each item. Let's take a look at the data.\n", "\n", "### Family Count\n", "***\n", "We can see that the family with most items is *GROCERY I*.<br>"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "4d8f4731-c863-4b15-bdbf-99c858137001", "_kg_hide-output": false, "_uuid": "7df96de1d86f12bc067bc1629f2fc2f1356448c6", "_kg_hide-input": true}, "source": ["#Set the plot figure\n", "plt.figure(figsize=(15,6))\n", "# Create the plot title\n", "plt.title('Item Family Distribution', fontsize=20)\n", "# Plot the data\n", "sns.countplot(data=df_items, x='family')\n", "# Rotate the name of the states\n", "plt.xticks(rotation=75, fontsize=12)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "9ec2a3bc-ebb6-49d8-99e6-6663e230ed83", "_uuid": "acdebdd71fe13ffd892fb900144309ef8cc0fff2"}, "source": ["### Perishable Families\n", "***\n", "Now, let's take a look at the families and how many perishable products each contain.<br>\n", "As we can see, there are NO families that contain BOTH perishable and non-perishable items<br> \n", "This means that the perishable feature is also a feature of the family, and not only of the product."], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "d5b938a9-392b-48a9-815b-119b3739d37a", "_kg_hide-output": false, "_uuid": "1c98085c42a6e53d25d8311a1099ef093ce42122", "_kg_hide-input": true}, "source": ["# Get the data grouping by family an6d perishable, and count each occurrency\n", "df_plot = df_items.groupby(['family','perishable']).agg({'item_nbr':'count'}).reset_index()\n", "#Set the plot figure\n", "plt.figure(figsize=(15,6))\n", "# Create the plot title\n", "plt.title('Perishable Items by Family', fontsize=20)\n", "# Plot the data\n", "sns.barplot(data=df_plot, y='item_nbr', x='family', hue='perishable')\n", "# Rotate the name of the states\n", "plt.xticks(rotation=75, fontsize=12)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "4041baf2-e550-4970-875b-afe58c48e836", "_uuid": "0de06c0e018f488ca8143eaf145f66507463efc2"}, "source": ["### Ammount of perishable items and non perishable items\n", "***\n", "Let's check the balance between the perishable items and non perishable items.<br>\n", "By also trying to plot the missings as a 3rd category (Missing), we automatically verify that this column doesn't have missing values! "], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "d7f5637a-5710-4d06-809d-4db240f41f90", "_uuid": "da69fa15a33b56bd11379158d714249adcc49776", "_kg_hide-input": true}, "source": ["# Group the data by the \"perishable\" field. Make a count of the values and reset the index.\n", "# reset_index converts the index created (the \"by\" parameter) back into a column\n", "df_plot = df_items.fillna(-1).groupby(by=['perishable']).agg({'item_nbr':'count'}).reset_index()\n", "\n", "# Replace the numeric values with text (for plot readability)\n", "df_plot.perishable = df_plot.perishable.replace({0:'Non perishable', 1:'Perishable', -1: 'Missing'})\n", "\n", "# Set the plot size\n", "plt.figure(figsize=(10,5))\n", "\n", "# Create the barplot\n", "sns.barplot(data=df_plot, x='perishable', y='item_nbr')\n", "\n", "# Set the plot title\n", "plt.title('# of perishables and non-perishables', fontsize=16)\n", "\n", "# Set the axes labels\n", "plt.xlabel('Is perishable?', fontsize=14)\n", "plt.ylabel('# of items', fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "412f68c9-ad02-483d-9f46-764c52986238", "_uuid": "f0916e5673fcb6121c7f3e3897ae21262e8dde8b"}, "source": ["## Oil\n", "***\n", "This is an interesting dataset. It contains the daily oil price, since ecuador is an oil dependent, and we are trying to understand about sales, which has to do something with economics.<br>\n"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3045ee81-9590-4e7b-90cd-79795a41e40d", "_uuid": "ce19ff7dc692ff11bdfb46d4dc1a8766ecc1fbf8", "_kg_hide-input": true}, "source": ["# set the plot size\n", "plt.figure(figsize=(14,6))\n", "# Create the plot title\n", "plt.title('Oil Price by Date', fontsize=20)\n", "# Plot total unit sales\n", "plt.plot(df_oil.date, df_oil.dcoilwtico)\n", "\n", "# Set the labels\n", "plt.xlabel('Date',fontsize=14)\n", "plt.ylabel('Oil Price',fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "e684aab2-1fbf-4165-b763-43b610a3c8e2", "_uuid": "9fd65ff957ed07800999bed12855424201beb36d"}, "source": ["### A key discovery\n", "The promotions were implemented almost at the same time that the oil started to drop! "], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "7b4ebcc1-17d6-4fc5-862c-274e6e097a1c", "_uuid": "6cad935f22e076068def95b11af1be6636868a76"}, "source": [" ## Stores\n", " ***\n", " Let's see what's going on with stores...\n", " \n", " ### Stores by state\n", " ***\n", " How many stores by state are there?<br>\n", " Pichincha and Guayas are the leaders in number of Stores!"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "f4925043-b719-4ee1-bbcb-b86ae08a3e16", "_uuid": "eb6e4ed111ea3e60fc56eb711b316e41142d8f42", "_kg_hide-input": true}, "source": ["# Group by state column and count on the store_nbr column\n", "df_plot = df_stores.groupby('state').agg({'store_nbr':'count'}).reset_index()\n", "\n", "# Set the plot size\n", "plt.figure(figsize=(16,8))\n", "# Create the Plot \n", "sns.barplot(data=df_plot, x='state',y='store_nbr')\n", "# Set the plot title\n", "plt.title('Stores by State', fontsize=18)\n", "#Set the axis text\n", "plt.xlabel('States',fontsize=14)\n", "plt.ylabel('# of stores', fontsize=14)\n", "\n", "# Rotate the name of the states\n", "plt.xticks(rotation=75, fontsize=12)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "3e5290dc-ce7a-4415-a28c-4e65ea67dc7a", "_uuid": "f183d253464ac6559caf1e993e767d2cecf2eca5", "collapsed": true}, "source": [" ### Stores by city\n", " ***\n", " How many stores by city are there?<br>\n", " Quito and Guayaquil are the leaders in number of Stores by city!"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3bf823dc-e95c-44ee-be85-6cde015ad9f6", "_uuid": "0a254314631d6f79f5eb04abdcb3b73b5254e147", "_kg_hide-input": true}, "source": ["# Group by state column and count on the store_nbr column\n", "df_plot = df_stores.groupby('city').agg({'store_nbr':'count'}).reset_index()\n", "\n", "# Set the plot size\n", "plt.figure(figsize=(16,8))\n", "# Create the Plot \n", "sns.barplot(data=df_plot, x='city',y='store_nbr')\n", "# Set the plot title\n", "plt.title('Stores by City', fontsize=18)\n", "#Set the axis text\n", "plt.xlabel('Cities',fontsize=14)\n", "plt.ylabel('# of stores', fontsize=14)\n", "\n", "# Rotate the name of the states\n", "plt.xticks(rotation=75, fontsize=12)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "ccda50b8-cc53-4b49-9bab-5682d90d4324", "_uuid": "186b7a8d0a9c9b7906ce18c572286ff2f33b19cd"}, "source": ["# Holiday Events\n", "***\n", "Let's take a look at the holiday events dataset.\n", "\n", "## How many holidays?\n", "*** \n", "An overview of the total number of holidays by Locale.<br>\n", "By the looks of it, we can say that most holidays, happen on a local scale, and also, this is the only time that they happen on a regional scale.\n", "\n", "The rest of the holiday event types happen eithe"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "f66ce6f8-cafc-4785-8315-6143701edab2", "_uuid": "3a19654b6a92ce44dcebb712b7b541380363b142", "_kg_hide-input": true}, "source": ["# Set the plot size\n", "plt.figure(figsize=(10,5))\n", "\n", "# Create the barplot\n", "sns.countplot(data=df_holiday_events, x='type', hue='locale')\n", "\n", "# Set the plot title\n", "plt.title('Ammount of holiday events per type and locale', fontsize=16)\n", "\n", "# Set the axes labels\n", "plt.xlabel('Holiday EventType', fontsize=14)\n", "plt.ylabel('# of Holiday Events', fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "4f548976-3516-4877-8373-48ac47631031", "_uuid": "9be4eaeaa759af138b3eb30699bd53557680e314"}, "source": ["# Cross Analysis\n", "***\n", "Let's merge some datasets and see what we can find!\n", "\n", "## Merge Train with Items\n", "***\n", "Let's merge the items and train datasets"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "4d9d2afc-5e92-4457-a98a-3095bdcb7732", "_uuid": "fff47b7c371a8ffbc0668b16b299455b8e82ed27", "collapsed": true}, "source": ["# Merge train and items and store it on the train variable\n", "df_train = pd.merge(df_train, df_items, on='item_nbr', how='left')"], "cell_type": "code"}, {"metadata": {"_cell_guid": "d0e38d6b-0c81-4d12-a6ff-7400245d5b1e", "_uuid": "61b50b5bda27f5485369fefe7da2630da99afe57"}, "source": ["## Unit Sales by Date (perishable  and non perishable)\n", "***\n", "We will now plot the unit sales separating perishable and non perishable items to see how those move."], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "f9273ef7-632f-403d-a1d5-dd523544f024", "collapsed": true, "_uuid": "e66146a10ac8a3af38e8ffbc9d8581f6eab2a022", "_kg_hide-input": true}, "source": ["df_plot = df_train.groupby(by=['date','perishable']).agg({'unit_sales':'sum'}).reset_index()\n", "# turn date into datetime type\n", "df_plot.date = pd.to_datetime(df_plot.date)\n", "\n", "# Unit Sales by Date\n", "fig, ax = plt.subplots(1)\n", "fig.autofmt_xdate()\n", "#Set the plot figure\n", "fig.set_figheight(10)\n", "fig.set_figwidth(16)\n", "# Create the plot title\n", "plt.title('Unit Sales by Date (Perishable and non perishable)', fontsize=20)\n", "\n", "# Plot total perishable unit sales\n", "plt.plot(df_plot.loc[df_plot['perishable'] == True,'date'], df_plot.loc[df_plot['perishable'] == True,'unit_sales'])\n", "# Plot total non-perishable unit sales\n", "plt.plot(df_plot.loc[df_plot['perishable'] == False,'date'], df_plot.loc[df_plot['perishable'] == False,'unit_sales'])\n", "\n", "# Set the legend\n", "plt.legend(['Perishable','Non Perishable'], fontsize=14)\n", "\n", "# Set the labels\n", "plt.xlabel('Date',fontsize=14)\n", "plt.ylabel('Unit Sales',fontsize=14)"], "cell_type": "code"}, {"metadata": {"_cell_guid": "7153934e-b05c-48f2-bfd4-b2d52fdcff0f", "_uuid": "9d9b49f4e9e62e710ca50978cbf96cbcc689b978"}, "source": ["## Unit Sales by Item Family (perishable and non perishable)\n", "***\n", "How do sales variate on each product family?\n", "\n", "Now this is a really messy plot, but we can see some information about the life of some item families.<br>\n", "**Perishables**:\n", "* The `PRODUCE` family was sold at certain points, and after that it started to be sold continuously.\n", "* The `DAIRY` family is the most sold product after `PRODUCE`\n", "\n", "**Non-Perishables**:\n", "* As we saw earlier, the `GROCERY I` family is the one sold the most."], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "d8fcfa92-8daa-4e94-94dc-8e39f3ed6ab6", "collapsed": true, "_uuid": "d11d6099b18c63521e997444189cf267d6218139", "scrolled": true, "_kg_hide-input": true}, "source": ["df_plot = df_train.groupby(by=['date','family','perishable']).agg({'unit_sales':'sum'}).reset_index()\n", "# turn date into datetime type\n", "df_plot.date = pd.to_datetime(df_plot.date)\n", "\n", "# Unit Sales by Date\n", "fig, ax = plt.subplots(1)\n", "fig.autofmt_xdate()\n", "#Set the plot figure\n", "fig.set_figheight(20)\n", "fig.set_figwidth(16)\n", "\n", "# Set the first subplot\n", "plt.subplot(2,1,1)\n", "\n", "# Create the plot title\n", "plt.title('Unit Sales by Item Family (Perishables)', fontsize=20)\n", "\n", "# Get each item family.\n", "families = df_plot.loc[df_plot['perishable'] == True].family.unique()\n", "\n", "# For each family\n", "for family in families:\n", "    \n", "    # Filter the dataframe by the family\n", "    df_curr_plot = df_plot.loc[(df_plot['family'] == family)]\n", "    \n", "    # Plot unit sales of the current item family\n", "    plt.plot(df_curr_plot.date, df_curr_plot.unit_sales, label=family)\n", "\n", "# Set the legends\n", "plt.legend(families, fontsize=8)\n", "\n", "# Set the labels\n", "plt.xlabel('Date',fontsize=14)\n", "plt.ylabel('Unit Sales',fontsize=14)\n", "\n", "# Set the second subplot\n", "plt.subplot(2,1,2)\n", "\n", "# Create the plot title\n", "plt.title('Unit Sales by Item Family (Non Perishables)', fontsize=20)\n", "\n", "# Get each item family.\n", "families = df_plot.loc[df_plot['perishable'] == False].family.unique()\n", "\n", "# For each family\n", "for family in families:\n", "    \n", "    # Filter the dataframe by the family\n", "    df_curr_plot = df_plot.loc[(df_plot['family'] == family)]\n", "    \n", "    # Plot unit sales of the current item family\n", "    plt.plot(df_curr_plot.date, df_curr_plot.unit_sales, label=family)\n", "\n", "# Set the legends\n", "plt.legend(fontsize=10)#, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\n", "\n", "# Set the labels\n", "plt.xlabel('Date',fontsize=14)\n", "plt.ylabel('Unit Sales',fontsize=14)\n", "\n", "plt.tight_layout()"], "cell_type": "code"}, {"metadata": {"_cell_guid": "f9eb3d47-5e3e-444b-bef9-529b2d930494", "_uuid": "add98041060e5219ffa515272541d57863b8fbfb"}, "source": ["# To be continued...\n", "***\n", "We still have a lot more data and datsets to explore, so keep coming back for new insights!"], "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_cell_guid": "969fc482-1a55-4197-a34d-05e92724d5da", "_uuid": "05d4d081223930d977b6d60044d853ce226f8567", "collapsed": true}, "source": [], "cell_type": "code"}], "nbformat": 4, "nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "name": "python", "file_extension": ".py", "version": "3.6.3", "mimetype": "text/x-python"}}}