{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set()\n\nfrom statsmodels.graphics.tsaplots import plot_acf\n\nimport plotly.offline as py\nimport plotly.graph_objs as go\nfrom plotly import tools\npy.init_notebook_mode(connected=True)\n\n#############\n\nimport statsmodels.api as sm\nfrom fbprophet import Prophet\n\n#############\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"dtype_dict={\"id\":np.uint32,\n            \"store_nbr\":np.uint8,\n            \"item_nbr\":np.uint32,\n            \"unit_sales\":np.float32,\n            \"perishable\":np.uint8,\n            \"class\":np.uint32,\n            \"cluster\":np.uint8,\n            \"transactions\":np.uint32\n           }\n\niter_csv = pd.read_csv(\"../input/train.csv\", iterator=True, chunksize=40000000, usecols=[\"date\", \"store_nbr\", \"item_nbr\", \"unit_sales\"], dtype=dtype_dict, parse_dates=['date'])\ndf = pd.concat([chunk[chunk['store_nbr'] == 44] for chunk in iter_csv])\n#df = pd.read_csv(\"output.csv\", usecols=[\"date\", \"store_nbr\", \"item_nbr\", \"unit_sales\"], dtype=dtype_dict, parse_dates=['date'])\n\ndf[\"year\"] = df[\"date\"].dt.year\ndf[\"month\"] = df[\"date\"].dt.month\ndf[\"week\"] = df[\"date\"].dt.week\ndf[\"day_week\"] = df[\"date\"].dt.weekday_name\ndf[\"day_month\"] = df[\"date\"].dt.day\n\ndf_2013 = df[df[\"year\"] == 2013]\n\nitems = pd.read_csv(\"../input/items.csv\", dtype=dtype_dict)\n#items[\"family\"] = items[\"family\"].map(lambda x: x.lower().replace(\"i\", \"I\"))\n\nstores = pd.read_csv(\"../input/stores.csv\", dtype=dtype_dict) \nholidays = pd.read_csv(\"../input/holidays_events.csv\", dtype=dtype_dict, parse_dates=['date'])\ntransactions = pd.read_csv(\"../input/transactions.csv\", dtype=dtype_dict, parse_dates=['date'])\noil = pd.read_csv(\"../input/oil.csv\", dtype=dtype_dict, parse_dates=['date'])\n\n'ok'","execution_count":5,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2500c66f3ccadaee5db14a427381c9deded9b8c0","collapsed":true},"cell_type":"code","source":"df_sample = df.sample(1000 * 1000)\ndf_sample = pd.merge(left=df_sample, right=items, how=\"left\", on=\"item_nbr\")\n\nsales = df_sample.groupby('date').size()","execution_count":4,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2c364ffd08fcff6cb0e70be29acf218463d8058"},"cell_type":"code","source":"x_sliding_window = pd.DataFrame(data={'x1':sales.values[:-1], 'x_date': sales.keys()[1:]})\nx_sliding_window[\"x_year\"] = x_sliding_window[\"x_date\"].dt.year\nx_sliding_window[\"x_month\"] = x_sliding_window[\"x_date\"].dt.month\nx_sliding_window[\"x_week\"] = x_sliding_window[\"x_date\"].dt.week\nx_sliding_window[\"x_day_week\"] = x_sliding_window[\"x_date\"].dt.weekday #Monday=0, Sunday=6\nx_sliding_window[\"x_day_month\"] = x_sliding_window[\"x_date\"].dt.day\ny_sliding_window = pd.DataFrame(data={'y':sales.values[1:]})\n\n'ok'","execution_count":7,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65d08873342c7d5bac552b0982d795279f1f4cd3","collapsed":true},"cell_type":"code","source":"# y_sliding_window\n\ny_sliding_window['y'].plot('line')","execution_count":8,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b4104a630a69492fa4439897bfde8643500ae3d","collapsed":true},"cell_type":"code","source":"# armax = sm.tsa.ARMA(y_sliding_window.values, order=(1, 1), exog=x_sliding_window.values).fit()\nx_sliding_window","execution_count":9,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7df45ba139d23be89756c0543e3bba5bc4236c1a"},"cell_type":"markdown","source":"## Tentativas\n- Holt-Winters\n    - https://grisha.org/blog/2016/01/29/triple-exponential-smoothing-forecasting/\n- Prophet\n    - https://facebook.github.io/prophet/docs/quick_start.html\n    - https://arnesund.com/2017/02/26/using-facebook-prophet-forecasting-library-to-predict-the-weather/"},{"metadata":{"_uuid":"c8aafe8712400002646ba2235da49626ee82843c"},"cell_type":"markdown","source":"## Tentando usar Prophet do Facebook"},{"metadata":{"trusted":true,"_uuid":"a40a0ed45c9e64fc2e1ccea6edf64e9a138ce073"},"cell_type":"code","source":"df = pd.DataFrame(data={'ds':sales.keys(), 'y':np.log(sales.values) })\n\nholidays_values = pd.read_csv(\"../input/holidays_events.csv\")['date'].values\n\nholidays = pd.DataFrame({\n  'holiday': 'holidays',\n  'ds': pd.to_datetime(holidays_values),\n  'lower_window': 0,\n  'upper_window': 5,\n})\n\nm1 = Prophet(holidays=holidays).fit(df)\n# m1 = Prophet().fit(df)","execution_count":17,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a79950f3ced020f38f2e762d5bbcdcd6881860e2"},"cell_type":"code","source":"future1 = m1.make_future_dataframe(periods=365)\nforecast1 = m1.predict(future1)","execution_count":20,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62a5a4c0d2bebe55a0156e8a43be43093e41cc42"},"cell_type":"code","source":"m1.plot(forecast1);","execution_count":21,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50bd454341b8eaa899ebb5542db1daf5bc6b9212","scrolled":true},"cell_type":"code","source":"# pd.read_csv(\"../input/holidays_events.csv\")['date']\n\n'ok'","execution_count":22,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"0a23f34125b512395f58a5fb111a983bbeaa3dea"},"cell_type":"code","source":"model2 = Prophet(mcmc_samples=500).fit(df)\nmodel2.plot_components(forecast1)","execution_count":29,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3984891e8eaddf5c41bc74df7865cdd33eeda21c"},"cell_type":"code","source":"from fbprophet.diagnostics import cross_validation\n# from fbprophet.diagnostics.plot_cross_validation_metric import plot_cross_validation_metric\n# fig = plot_cross_validation_metric(df_cv, metric='mape')\n\ndf_cv = cross_validation(m1, initial='730 days', period='180 days', horizon = '365 days')\ndf_cv\n","execution_count":41,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d5bafeb6e1805492dee2aa87b1bd8d9af06b00e"},"cell_type":"code","source":"import fbprophet as fbprophet\n# from fbprophet.diagnostics import performance_metrics\n# df_p = performance_metrics(df_cv)\n# df_p\n\nfbprophet.__version__","execution_count":48,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6cea0d73e908c66369e4679de0a410928c56d5d3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b393264fab36670ab14d8f6a7cf21adc74be2f1d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c243ac2fa755f73ae9a3a9e455b4b7447de8c4cb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a43fde221f1f881cec700ca687a62b0aa968a7c2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"12f9bf194d4fab8bb66b6b9a8f46c9a0e50b7b07"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d42012cec7048b086dae0fd8342ed07119ebe258"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"37d1806a13872506163ee0f28b5edf1c71bfa9ff"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5b70852b261df3d197b43fec4637acc4da85a6b1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"100e5b9db7f064be6e7767d310a866b033cf19be"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"83177b8f42dae4857c0ee072b4d4cdba28f86039"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"921f6dee925ff4481be1e8bdb3e5cbf5f4a76b23"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8d1f235ea75dda693cd393ea70d9ba1a870e70ad"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"11668941c23aff368fc35806d0da1c328f5006c7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f795725333e41adcfb7cabb3b416d4a8b424f9ca"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ee791b83015de7aeae5e95af16fd80baee92fef9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6150c191ae192495284f17e4c7ab69e9384b261d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"89e91db41ce0365ea75dd1786cb448ff32b610b3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"89d3e829b7e20fab9f062aafa6daae54d1605db3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"aabc436a1d090314bd34c6e76de090d97454dea5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"cfa40fd63d6f22e9ba1afa62391c599b43898bc6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9de9ead4e1cba028621a4b829a76999e8ce61af0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2f016f334fb6b930fcc90ca97fb776e70de5826b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c2114979be148383b4f24858404995ffc1fb8dab"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a189d9c7e6308e64f4ef11ac965294e2334f78c0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ad9e3c9e909b9560243d41d26ff1c3c03a153efe"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"fac37fdbb4f3b7908ebfb4d41d73f6bc5551c153"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"81f0f1bcc77b60d4f0ccb9d346fbc89a3feac283"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7b91a1584aed1ceb77ed5676974fd856a1a7d78e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1d1518bd46081abed524fd8a226b94292d355df3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"597ba842907d066052c78d217fb062ea69140dcd"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d21f3d7aeb3d3a498f8cf52d59d425c06371f1ff"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5fc88970381d3ae557c56901d5296e0b5cd46aab"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"011d082cf9ee222d895760f5414307403412c398"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"779108a844e35a583f39a1dc7db694eef2b53c64"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3d71f9337de4e9d68c4f148559dd2140fe2131b8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b53707e3346a4c9b2939557ec2cafa1f585f63b9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2c2c3e64f228fe9004f59da157ff86bbc92ba833"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f3e258c336166bd823411a6b64cfea9130f200e3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e2f2b200598cba6fc4a44fb42852644a4443b5e2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b13e7dec854e1b2642b8b61b741a05c85ce2e1b4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"679748c7c1931ba14b5d94dfae1125a33b19f78a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6e90ad6bd5a1b8450780add343bc26e38e35e251"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5c5fe316ca2ee194dca695424e0ab3b5ee6396d6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"fdfb15b5dc2e0a549dab7d4908d071b3538ff369"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"17122b71082c9faab49c4949dbe94552e62c65fb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7ee720d9284b565f8b9741233a9c690fd6ee3b81"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ad9d0675f8a39da1396eb62d83f392befe65c842"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"01dcf42049a9136be654829901c7eb2411cd10ef"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b6a77686822fa00c1ab5c0c0504c4c5f969aecfb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"532eb1ca14840b7f0ab4493c27919f8f19e05534"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c55394c920ab4cbe411c1469cf20b72053fffea0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d4525c462cbbbc6b8e71a3ee2b3750ef8fb6fb89"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"973d5ce4f3b48f53a825459ceba2278b80e1a328"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}