{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This notebook presents some basic Exploratory data analysis (EDA) on the [beachfront demand forecasting dataset](https://www.kaggle.com/competitions/applications-of-deep-learning-wustlfall-2022). We begin by loading the tabular data.\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os\n\nPATH_CSV = '/kaggle/input/applications-of-deep-learning-wustlfall-2022/beach_demand_forecast/'\n\ndf_sales = pd.read_csv(os.path.join(PATH_CSV,\"sales_train.csv\"))\ndf_items = pd.read_csv(os.path.join(PATH_CSV,\"items.csv\"))\ndf_resturant = pd.read_csv(os.path.join(PATH_CSV,\"resturants.csv\"))\n\ndf_sales.date = pd.to_datetime(df_sales.date, errors='coerce') ","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:09:59.604377Z","iopub.execute_input":"2022-10-25T22:09:59.605397Z","iopub.status.idle":"2022-10-25T22:09:59.746071Z","shell.execute_reply.started":"2022-10-25T22:09:59.605356Z","shell.execute_reply":"2022-10-25T22:09:59.744929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we add two fields, weekday and week that represent the day of the week and the day of the current year.","metadata":{}},{"cell_type":"code","source":"df_sales.date = pd.to_datetime(df_sales.date, errors='coerce') \ndf_sales['weekday'] = df_sales.date.dt.day_name()\n\nstart_date = df_sales.date.min()\ndf_sales['week'] = (((df_sales.date - start_date).dt.days)/7).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:13:35.068814Z","iopub.execute_input":"2022-10-25T22:13:35.069261Z","iopub.status.idle":"2022-10-25T22:13:35.137277Z","shell.execute_reply.started":"2022-10-25T22:13:35.069225Z","shell.execute_reply":"2022-10-25T22:13:35.136094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Begin by producing a line graph of all sales over the provided 2-year timespan.","metadata":{}},{"cell_type":"code","source":"import plotly.express as px\n\ndf_plot = df_sales[['date','item_count']].groupby(['date']).sum().reset_index()\nfig = px.line(df_plot, x=\"date\", y=\"item_count\", title='Total Sales by Date')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:11:59.489050Z","iopub.execute_input":"2022-10-25T22:11:59.489602Z","iopub.status.idle":"2022-10-25T22:12:02.080449Z","shell.execute_reply.started":"2022-10-25T22:11:59.489566Z","shell.execute_reply":"2022-10-25T22:12:02.079234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we see the amount of sales by day of the week.","metadata":{}},{"cell_type":"code","source":"day_order = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n\n# Obtain data aggregated by weekday.\ndf_plot = df_sales[['weekday','item_count']].groupby(['weekday']).sum().reset_index()\n# Sort by day of week\ndf_plot = df_plot.set_index(\"weekday\").loc[day_order].reset_index()\n\nfig = px.bar(df_plot, x='weekday', y='item_count', title='Total Sales by Day of Week')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:13:39.637301Z","iopub.execute_input":"2022-10-25T22:13:39.637814Z","iopub.status.idle":"2022-10-25T22:13:39.740910Z","shell.execute_reply.started":"2022-10-25T22:13:39.637767Z","shell.execute_reply":"2022-10-25T22:13:39.739982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Display total sales for a year.","metadata":{}},{"cell_type":"code","source":"SINGLE_YEAR = 2020\n\ndf_sales_single_year = df_sales[df_sales['date'].dt.year == SINGLE_YEAR]\ndf_plot = df_sales_single_year[['date','item_count']].groupby(['date']).sum().reset_index() \nfig = px.bar(df_plot, x='date', y='item_count',facet_col_spacing=0, title=f'Total Sales by Day {SINGLE_YEAR}')\nfig.update_layout(bargap=0.0,bargroupgap=0.0)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:14:00.471501Z","iopub.execute_input":"2022-10-25T22:14:00.472103Z","iopub.status.idle":"2022-10-25T22:14:00.567944Z","shell.execute_reply.started":"2022-10-25T22:14:00.472035Z","shell.execute_reply":"2022-10-25T22:14:00.566849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Most Popular Items\n\nUnique item id's.","metadata":{}},{"cell_type":"code","source":"df_sales.item_id.unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:15:17.023238Z","iopub.execute_input":"2022-10-25T22:15:17.023635Z","iopub.status.idle":"2022-10-25T22:15:17.034352Z","shell.execute_reply.started":"2022-10-25T22:15:17.023604Z","shell.execute_reply":"2022-10-25T22:15:17.032989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most popular items.","metadata":{}},{"cell_type":"code","source":"df2 = df_sales[['item_id','item_count']].groupby(['item_id']).sum().reset_index().sort_values('item_count',ascending=False)\ndf_items2 = df_items[['id','name']]\ndf_items2.columns = ['item_id','name']\ndf_popular = df2.merge(df_items2)\nprint(df_popular.to_string())","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:15:44.562845Z","iopub.execute_input":"2022-10-25T22:15:44.563268Z","iopub.status.idle":"2022-10-25T22:15:44.600786Z","shell.execute_reply.started":"2022-10-25T22:15:44.563230Z","shell.execute_reply":"2022-10-25T22:15:44.599486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Individual Item Plots\n\nPlot items per day.","metadata":{}},{"cell_type":"code","source":"df_plot = df_sales_single_year[['date','item_id','item_count']].groupby(['date','item_id']).sum().reset_index()\ndf_plot = df_plot.merge(df_items,left_on=\"item_id\",right_on=\"id\")[['date','item_count','name']]\nfig = px.bar(df_plot, x='date', y='item_count',color=\"name\", title=f'Item Sales by Date - {SINGLE_YEAR}')\nfig.update_layout(bargap=0.0,bargroupgap=0.0)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:16:18.686104Z","iopub.execute_input":"2022-10-25T22:16:18.686527Z","iopub.status.idle":"2022-10-25T22:16:20.018033Z","shell.execute_reply.started":"2022-10-25T22:16:18.686493Z","shell.execute_reply":"2022-10-25T22:16:20.017139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot items per week.","metadata":{}},{"cell_type":"code","source":"df_plot = df_sales_single_year[['week','item_id','item_count']].groupby(['week','item_id']).sum().reset_index()\ndf_plot = df_plot.merge(df_items,left_on=\"item_id\",right_on=\"id\")[['week','item_count','name']]\nfig = px.bar(df_plot, x='week', y='item_count',color=\"name\", title=f'Total Sales by Week - {SINGLE_YEAR}')\nfig.update_layout(bargap=0.0,bargroupgap=0.0)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:17:04.488639Z","iopub.execute_input":"2022-10-25T22:17:04.489021Z","iopub.status.idle":"2022-10-25T22:17:04.928022Z","shell.execute_reply.started":"2022-10-25T22:17:04.488992Z","shell.execute_reply":"2022-10-25T22:17:04.926875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot items per day of week.","metadata":{}},{"cell_type":"code","source":"day_order = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n\ndf_plot = df_sales[['weekday','item_id','item_count']].groupby(['weekday','item_id']).sum().reset_index()\ndf_plot = df_plot.set_index('weekday').loc[day_order].reset_index()\ndf_plot = df_plot.merge(df_items,left_on=\"item_id\",right_on=\"id\")[['weekday','item_count','name']]\nfig = px.bar(df_plot, x='weekday', y='item_count',color=\"name\", title=f'Total Sales by Day of Week - {SINGLE_YEAR}')\nfig.update_layout(bargap=0.0,bargroupgap=0.0)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-25T22:17:39.052295Z","iopub.execute_input":"2022-10-25T22:17:39.052692Z","iopub.status.idle":"2022-10-25T22:17:39.521976Z","shell.execute_reply.started":"2022-10-25T22:17:39.052660Z","shell.execute_reply":"2022-10-25T22:17:39.520849Z"},"trusted":true},"execution_count":null,"outputs":[]}]}