{"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 makes use of Meta (aka Facebook) Prophet AutoML to forecast time series.","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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-30T02:30:34.539794Z","iopub.execute_input":"2022-10-30T02:30:34.540299Z","iopub.status.idle":"2022-10-30T02:30:34.763858Z","shell.execute_reply.started":"2022-10-30T02:30:34.540199Z","shell.execute_reply":"2022-10-30T02:30:34.762867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Begin by producing a line graph of all sales over the provided 3-year timespan. We will use this to train Prophet.","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-30T02:32:48.388828Z","iopub.execute_input":"2022-10-30T02:32:48.389234Z","iopub.status.idle":"2022-10-30T02:32:50.688828Z","shell.execute_reply.started":"2022-10-30T02:32:48.389201Z","shell.execute_reply":"2022-10-30T02:32:50.687452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Construct prophet and put the previous data into the form expected by prophet.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom prophet import Prophet\n\ndf_plot.columns = ['ds','y']\n\nm = Prophet()\nm.fit(df_plot)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:33:21.738597Z","iopub.execute_input":"2022-10-30T02:33:21.739556Z","iopub.status.idle":"2022-10-30T02:33:22.509486Z","shell.execute_reply.started":"2022-10-30T02:33:21.739499Z","shell.execute_reply":"2022-10-30T02:33:22.508115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predict one year into the future.","metadata":{}},{"cell_type":"code","source":"future = m.make_future_dataframe(periods=365)\nfuture.tail()","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:33:59.256105Z","iopub.execute_input":"2022-10-30T02:33:59.256515Z","iopub.status.idle":"2022-10-30T02:33:59.274664Z","shell.execute_reply.started":"2022-10-30T02:33:59.256480Z","shell.execute_reply":"2022-10-30T02:33:59.273351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Display the predicted tabular data.","metadata":{}},{"cell_type":"code","source":"forecast = m.predict(future)\nforecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:34:35.390139Z","iopub.execute_input":"2022-10-30T02:34:35.390559Z","iopub.status.idle":"2022-10-30T02:34:36.422866Z","shell.execute_reply.started":"2022-10-30T02:34:35.390525Z","shell.execute_reply":"2022-10-30T02:34:36.421570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the future predictions.","metadata":{}},{"cell_type":"code","source":"fig1 = m.plot(forecast)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:35:12.904259Z","iopub.execute_input":"2022-10-30T02:35:12.904729Z","iopub.status.idle":"2022-10-30T02:35:13.393178Z","shell.execute_reply.started":"2022-10-30T02:35:12.904680Z","shell.execute_reply":"2022-10-30T02:35:13.391792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"See what seasonality and trend were detected.","metadata":{}},{"cell_type":"code","source":"fig2 = m.plot_components(forecast)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:35:46.471014Z","iopub.execute_input":"2022-10-30T02:35:46.471467Z","iopub.status.idle":"2022-10-30T02:35:47.187649Z","shell.execute_reply.started":"2022-10-30T02:35:46.471425Z","shell.execute_reply":"2022-10-30T02:35:47.186288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Use Plotly to generate an interactive plot.","metadata":{}},{"cell_type":"code","source":"from prophet.plot import plot_plotly, plot_components_plotly\nplot_plotly(m, forecast)","metadata":{"execution":{"iopub.status.busy":"2022-10-30T02:36:23.139137Z","iopub.execute_input":"2022-10-30T02:36:23.139580Z","iopub.status.idle":"2022-10-30T02:36:23.309013Z","shell.execute_reply.started":"2022-10-30T02:36:23.139545Z","shell.execute_reply":"2022-10-30T02:36:23.307764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}