{"nbformat_minor": 1, "nbformat": 4, "metadata": {"language_info": {"pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "version": "3.6.3"}, "kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}}, "cells": [{"execution_count": null, "outputs": [], "metadata": {"_uuid": "d51fbf4d9ec4e9759471105a636f58cef0149d45", "_cell_guid": "9910308a-051b-4680-a793-26cd4b53d678"}, "cell_type": "code", "source": ["# Importing the relevant libraries\n", "import IPython.display\n", "import pandas as pd\n", "import seaborn as sns\n", "import plotly.offline as py\n", "py.init_notebook_mode(connected=True)\n", "import plotly.graph_objs as go\n", "from matplotlib import pyplot as plt"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "e64a4746b9f17aa07b09ce1b5c2bbe877277ec69", "_cell_guid": "62a7f5f4-7e97-4f25-b3b1-2d2eb877a28e"}, "cell_type": "code", "source": ["items = pd.read_csv(\"../input/items.csv\")\n", "holiday_events = pd.read_csv(\"../input/holidays_events.csv\")\n", "stores = pd.read_csv(\"../input/stores.csv\")\n", "oil = pd.read_csv(\"../input/oil.csv\")\n", "transactions = pd.read_csv(\"../input/transactions.csv\",parse_dates=['date'])\n", "train = pd.read_csv(\"../input/train.csv\", parse_dates=['date'])"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "0a4a85727cf77c4e5b999284dc92b79c29542f4e", "_cell_guid": "ab77f658-fa78-4457-8c0f-f4d1fab86b9a"}, "cell_type": "code", "source": ["print(train.shape)\n", "train.head()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "b44de04acae83e693d7d1a722040e48023280788", "_cell_guid": "c65068ee-6c56-45c5-bc77-9540566aeaec"}, "cell_type": "code", "source": ["# Check for NULL values in all files\n", "print(\"Nulls in train: {0} => {1}\".format(train.columns.values, train.isnull().any().values))\n", "print('---')\n", "print(\"Nulls in oil: {0} => {1}\".format(oil.columns.values,oil.isnull().any().values))\n", "print('---')\n", "print(\"Nulls in holiday_events: {0} => {1}\".format(holiday_events.columns.values,holiday_events.isnull().any().values))\n", "print('---')\n", "print(\"Nulls in stores: {0} => {1}\".format(stores.columns.values,stores.isnull().any().values))\n", "print('---')\n", "print(\"Nulls in transactions: {0} => {1}\".format(transactions.columns.values,transactions.isnull().any().values))"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "fc063ffd06e810a63adda8c3de5097a957f50db9", "_cell_guid": "90851902-7917-4600-bf9c-bf963a21b141"}, "cell_type": "code", "source": ["# EDA for oil.csv begins here #\n", "oil.head(3)"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "7ac984f10728dabb353b78f2da01eb81438f12af", "_cell_guid": "336afd48-8532-441b-932c-6add917898b0"}, "cell_type": "code", "source": ["# Plotting graph for oil price trends\n", "trace = go.Scatter(\n", "    name='Oil prices',\n", "    x=oil['date'],\n", "    y=oil['dcoilwtico'].dropna(),\n", "    mode='lines',\n", "   )\n", "\n", "data = [trace]\n", "\n", "layout = go.Layout(\n", "    yaxis = dict(title = 'Daily Oil price'),\n", "    showlegend = True)\n", "fig = go.Figure(data = data, layout = layout)\n", "py.iplot(fig, filename='pandas-time-series-error-bars')"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "27cd069a0b914d08f33eb5f766c6a8c3a6fa380e", "_cell_guid": "b5ed6b45-f4e5-4add-bd6a-5de7625fc55a"}, "cell_type": "code", "source": ["# EDA for holiday_events.csv begins here #\n", "holiday_events.head(3)"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "2db66220fd854c439e724bffcef35a2a3ca8ad4d", "_cell_guid": "923f6465-8fcb-42e7-960e-ecacb4b78b4a"}, "cell_type": "code", "source": ["holiday_events.type.unique()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "18be8815e1fb11d7c105bcdce3e1f3dc220a53b2", "_cell_guid": "642d07c6-3cf3-43b8-8731-393d83d6c9c6"}, "cell_type": "code", "source": ["plt.style.use('seaborn-white')\n", "holiday_local_type = holiday_events.groupby(['locale_name', 'type']).size()\n", "holiday_local_type.unstack().plot(kind='bar',stacked=True, colormap= 'magma_r', figsize=(12,10),  grid=False)\n", "plt.ylabel('Count of entries')\n", "plt.show()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "a130897eb999c2f15909380f2ed26dd5962574e7", "_cell_guid": "048ec8b0-a444-4b93-b97a-3891934e256e"}, "cell_type": "code", "source": ["# EDA for items.csv begins here\n", "items.head()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "bb0afa33f761092b5dd3bd345be17432644cddbc", "_cell_guid": "a0b57e5e-0baa-44cf-a853-f4576ed709d7"}, "cell_type": "code", "source": ["# BAR PLOT FOR ITEMS V/S FAMILY TYPE\n", "x, y = (list(x) for x in zip(*sorted(zip(items.family.value_counts().index, \n", "                                         items.family.value_counts().values), \n", "                                        reverse = False)))\n", "trace2 = go.Bar(\n", "    y = items.family.value_counts().values,\n", "    x = items.family.value_counts().index\n", ")\n", "\n", "layout = dict(\n", "    title='Counts of items per family category',\n", "     width = 900, height = 600,\n", "    yaxis=dict(\n", "        showgrid = True,\n", "        showline = True,\n", "        showticklabels = True\n", "    ))\n", "\n", "fig1 = go.Figure(data=[trace2])\n", "fig1['layout'].update(layout)\n", "py.iplot(fig1, filename='plots')"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "4550e91b7c39d92ed34d627ff18ee15bda2fd885", "_cell_guid": "43402a66-bc8b-4c78-befd-a976adfbb809"}, "cell_type": "code", "source": ["# Persihable or not \n", "plt.style.use('seaborn-white')\n", "fam_perishable = items.groupby(['family', 'perishable']).size()\n", "fam_perishable.unstack().plot(kind='bar',stacked=True, colormap = 'coolwarm', figsize=(12,10),  grid = True)\n", "plt.ylabel('count')\n", "plt.show()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "1cd0972f901006b33d9760fab9040a8bf397f26c", "_cell_guid": "072525b1-ec04-4e95-a927-931e2cf9e6ca"}, "cell_type": "code", "source": ["# EDA for stores.csv begins here #\n", "stores.head(3)"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "26d8c335ed77ac0058e3d1e78b4d3730bb6046f8", "_cell_guid": "fc8e82db-fc29-4fd3-a304-c1b01becb6ab"}, "cell_type": "code", "source": ["# store distribution across states\n", "fig, ax = plt.subplots()\n", "fig.set_size_inches(8, 8)\n", "ax = sns.countplot(y = stores['state'], data = stores) "]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "13a168ce7e24934b2789278354ba249f0b10664c", "_cell_guid": "2d9d72fd-4364-46ea-8a33-5897d2652053"}, "cell_type": "code", "source": ["# store distribution across cities\n", "fig, ax = plt.subplots()\n", "fig.set_size_inches(8, 8)\n", "ax = sns.countplot(y = stores['city'], data = stores) "]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "b6657455bc76faed9f606a2320558ac7288211bd", "_cell_guid": "82e578ea-ab90-477f-91d9-91dd6f95f642"}, "cell_type": "code", "source": ["# Unique state names\n", "stores.state.unique()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "5b6538a1995ebd311fdc9680cf09c6e1479445c0", "_cell_guid": "67c5b349-5352-4887-b8d7-2c8d16b67037"}, "cell_type": "code", "source": ["# Unique state names\n", "stores.city.unique()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "158daee93a9f7ac6917b3e750d9e6299149c5eb3", "_cell_guid": "1e11ed34-bbdd-4fb6-a1b2-d256e830541e"}, "cell_type": "code", "source": ["# Various types of stores and their count\n", "fig, ax = plt.subplots()\n", "fig.set_size_inches(8, 5)\n", "ax = sns.countplot(x = \"type\", data = stores, palette=\"Paired\")"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "6b4aa931ba25cf56a4c539a063c554fa38780ffb", "_cell_guid": "5153da18-a299-4d19-89b4-2445858f01be"}, "cell_type": "code", "source": ["ct = pd.crosstab(stores.state, stores.type)\n", "ct.plot.bar(figsize = (12, 6), stacked=True)\n", "plt.legend(title='type')\n", "plt.show()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "1186e5c10b0d47b272509bb48e82d309825d211a", "_cell_guid": "593bc9b4-dd2a-4938-855f-1072b4f2cfee"}, "cell_type": "code", "source": ["ct = pd.crosstab(stores.city, stores.type)\n", "\n", "ct.plot.bar(figsize = (12, 6), stacked=True)\n", "plt.legend(title='type')\n", "\n", "plt.show()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "28800d4aeab1c4f8e12d684460b17848f402617e", "_cell_guid": "44a747aa-54fd-44a9-bab4-c761da6e3e93"}, "cell_type": "code", "source": ["# total no. of unique stores \n", "stores.store_nbr.nunique()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "8e607d171930981c5014a0124439a6a7042f6a2c", "_cell_guid": "d61c768e-d85e-4e33-93ea-58e810ec146d"}, "cell_type": "code", "source": ["# total no. of stores (including non-unique)\n", "stores.cluster.sum()"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "9e24d32d7ec7ec9c0e879e704fb9d739e1455a74", "_cell_guid": "49f28af0-2c7b-4424-93ff-e86163b6ee52"}, "cell_type": "code", "source": ["fig, ax = plt.subplots()\n", "fig.set_size_inches(12, 7)\n", "ax = sns.countplot(x = \"cluster\", data = stores)"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "7462bbe8c0220f46ed917263eccebf1a1c007e50", "_cell_guid": "8354cd2b-2b46-4d10-b909-e76c49fd6d36"}, "cell_type": "code", "source": ["# EDA for transactions.csv begins here #\n", "transactions.head(3)"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "7590391d6915671a416b9ceebe03600adfa06005", "_cell_guid": "6c9c9e19-921d-4f89-bc0c-fec02a07492a"}, "cell_type": "code", "source": ["# Finding out the no.of transactions (rows)\n", "print(\"There are {0} transactions\".\n", "      format(transactions.shape[0], transactions.shape[1]))"]}, {"execution_count": null, "outputs": [], "metadata": {"_uuid": "6514a0e76fc749613c7d04567ca84f75fceeddcf", "_cell_guid": "c4e1330b-f57e-43d7-88ac-e5a67b79edc6"}, "cell_type": "code", "source": ["# Time series plot for transaction #\n", "plt.style.use('seaborn-white')\n", "plt.figure(figsize=(13,11))\n", "plt.plot(transactions.date.values, transactions.transactions.values, color='grey')\n", "plt.axvline(x='2015-12-23',color='red',alpha=0.3)\n", "plt.axvline(x='2016-12-23',color='red',alpha=0.3)\n", "plt.axvline(x='2014-12-23',color='red',alpha=0.3)\n", "plt.axvline(x='2013-12-23',color='red',alpha=0.3)\n", "plt.axvline(x='2013-05-12',color='green',alpha=0.2, linestyle= '--')\n", "plt.axvline(x='2015-05-10',color='green',alpha=0.2, linestyle= '--')\n", "plt.axvline(x='2016-05-08',color='green',alpha=0.2, linestyle= '--')\n", "plt.axvline(x='2014-05-11',color='green',alpha=0.2, linestyle= '--')\n", "plt.axvline(x='2017-05-14',color='green',alpha=0.2, linestyle= '--')\n", "plt.ylabel('Transactions per day', fontsize= 16)\n", "plt.xlabel('Date', fontsize= 16)\n", "plt.show()"]}, {"execution_count": null, "outputs": [], "metadata": {"collapsed": true}, "cell_type": "code", "source": []}]}