{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport os\nimport seaborn as sns\nimport datetime\nimport lightgbm as lgb\nimport time\nimport matplotlib.dates as mdates\nimport datetime as dt\n\nfrom tqdm import tqdm\nfrom random import choice\n!pip uninstall --yes fbprophet\n!pip install fbprophet --no-cache-dir --no-binary :all:\n#!pip3 install numpy 1.14.5\n\nfrom fbprophet import Prophet\n\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import learning_curve\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import OneHotEncoder\n\nprint(os.listdir(\"../input\")) ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')\ndf_test = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1927113b213c296aad904fc9304e2750bc64c7ec"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00f07f8ce1f6547ed5cc0b7e0f8834aadf07c3b9"},"cell_type":"code","source":"df.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11f1906aecedc02e84b8c6953e426b60ac090043"},"cell_type":"code","source":"df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b3331a3415abadb6edd9be0d6c19acaa789005a"},"cell_type":"code","source":"df['date'] = pd.to_datetime(df['date'])\ndf_test['date'] = pd.to_datetime(df_test['date'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b63eb2347ba17c0cad8b07a1355e998c66e25b8"},"cell_type":"code","source":"df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af0b38064ad6e392da4e5b5b14a96d8154efc4a2"},"cell_type":"code","source":"df['year'] = df['date'].dt.year\ndf['month'] = df['date'].dt.month\ndf['week'] = df['date'].dt.week\ndf['day'] = df['date'].dt.day\ndf['dayofweek'] = df['date'].dt.dayofweek\n#df = df.drop('date', axis=1)\n\ndf_test['year'] = df_test['date'].dt.year\ndf_test['month'] = df_test['date'].dt.month\ndf_test['week'] = df_test['date'].dt.week\ndf_test['day'] = df_test['date'].dt.day\ndf_test['dayofweek'] = df_test['date'].dt.dayofweek\n#df_test = df_test.drop('date', axis=1)\n\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d505866cf62423a4d2c5581661d8180c3f66228c"},"cell_type":"code","source":"corr = df.corr()\nsns.heatmap(corr, cmap='coolwarm_r', annot_kws={'size':20})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"963d0e69183ba14a46c1adf7e7b039c5ce423316"},"cell_type":"code","source":"f, ax = plt.subplots(figsize=(20,5))\ndf.pivot_table('sales', index=['year','month'], columns='store', aggfunc='sum').plot(ax=ax)\nplt.xlabel('Year / Month')\nplt.ylabel('Sales')\nplt.show()\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,5))\ndf.pivot_table('sales', index=['year'], columns='store', aggfunc='sum').plot(ax=ax1)\ndf.pivot_table('sales', index=['month'], columns='store', aggfunc='sum').plot(ax=ax2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"61fc00eba94818390e8fb0248a78bc661a7f1c43"},"cell_type":"code","source":"df_s1_i1 = df.loc[(df['store']==1) & (df['item']==1)]\n\nm = Prophet()\n\n# Drop the columns\nph_df = df_s1_i1.drop(['item', 'store', 'month', 'year', 'day', 'dayofweek', 'week'], axis=1)\nph_df.rename(columns={'sales': 'y', 'date': 'ds'}, inplace=True)\n\nph_df['y_orig'] = ph_df['y']\nph_df['y'] = np.log(ph_df['y'])\n\nph_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f093810f693347205c48bc8d3e96206727cbd44"},"cell_type":"code","source":"m = Prophet()\nm.fit(ph_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"116aecd792f407bbcb0f67ef2d3108de4da26438"},"cell_type":"code","source":"future_data = m.make_future_dataframe(periods=90, freq = 'd')\nforecast_data = m.predict(future_data)\nforecast_data[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33fafc61939c7bfa80cce678b36971275196bfc5"},"cell_type":"code","source":"m.plot_components(forecast_data);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72f29ed5fd7df24d570845355ca6ac1ad6109274"},"cell_type":"code","source":"starting_date = dt.datetime(2018, 4, 1)\nstarting_date1 = mdates.date2num(starting_date)\n\npointing_arrow = dt.datetime(2018, 1, 1)\npointing_arrow1 = mdates.date2num(pointing_arrow)\n\nfig = m.plot(forecast_data)\nax1 = fig.add_subplot(111)\nax1.set_title(\"Item 1/ Store 1 Stock Price Forecast\", fontsize=16)\nax1.set_xlabel(\"Date\", fontsize=12)\nax1.set_ylabel(\"Sales\", fontsize=12)\nax1.annotate('Forecast \\n Initialization', xy=(pointing_arrow1, 2.9), xytext=(starting_date1,3.5),\n            arrowprops=dict(facecolor='#ff7f50', shrink=0.1),\n            )\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"674da12bc7cca2eaaafff85e0e8046ab38ea7d3e"},"cell_type":"code","source":"forecast_data_orig = forecast_data\nforecast_data_orig['yhat'] = np.exp(forecast_data_orig['yhat'])\nforecast_data_orig['yhat_lower'] = np.exp(forecast_data_orig['yhat_lower'])\nforecast_data_orig['yhat_upper'] = np.exp(forecast_data_orig['yhat_upper'])\n\nm.plot(forecast_data_orig);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a6b738abbe1f6adb62f4b746081ed33e48c05a8"},"cell_type":"code","source":"output = forecast_data[['ds','yhat']].sort_values(by=['ds'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"052a56eb20967ded8110ce2d0c95d6b01be4c98f"},"cell_type":"code","source":"output = output.loc[output['ds']>='2018-01-01'].reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea6741d9520fa4a4b3dda52ec9b9e6907e2a6b2b"},"cell_type":"code","source":"output = np.around(output['yhat']).astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e3efa0277d9ee0f7d50b17a5995ecde1181d93b"},"cell_type":"code","source":"df_s_i = df.groupby(['store', 'item'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a50992f2f7a3bab066441af4b2779858d6665fb"},"cell_type":"code","source":"print(max(df.sales))\nprint(min(df.sales))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80d5cdd35c7b1d8cac33dd0a1fc46766fa244de0"},"cell_type":"code","source":"prophet_results = pd.DataFrame()\n\nfor i, d in tqdm(df_s_i):\n    ph_df = d.drop(['item', 'store', 'month', 'year', 'day', 'dayofweek', 'week'], axis=1)\n    ph_df = ph_df.rename(columns={'date': 'ds', 'sales': 'y'})\n#    ph_df['y'] = np.log(ph_df['y'])\n\n    m = Prophet()\n    m.fit(ph_df)\n    \n    future_data = m.make_future_dataframe(periods=90, freq = 'd')\n    forecast_data = m.predict(future_data)\n       \n    prophet_results=prophet_results.append(forecast_data)\n    \n#    prophet_results.append(forecast_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caa1ba63681af504d5878c43480b7d6eac03bd95"},"cell_type":"code","source":"submission = prophet_results[['ds','yhat']]\nsubmission = submission.loc[submission['ds'] >= '2018-01-01'].round().reset_index(drop=True)\nsubmission['sales'] = submission['yhat'].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5cbd6260fa93eeb2b39fe4a1ca234b66e7aafa8"},"cell_type":"code","source":"submission['id'] = list(range(submission.shape[0]))\nsubmission.drop(columns=['ds','yhat'], inplace=True)\nsubmission = submission[['id','sales']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30244b805ba649a8fb7f294fed0c2b6f7e9c267c"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d984ac70cecd49ef15bd3b663c5b9a5d1f511119"},"cell_type":"code","source":"submission.to_csv(\"Submission.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}