{"cells":[{"metadata":{},"cell_type":"markdown","source":"This kernel predict 1 building energy consumption by [Prophet](https://facebook.github.io/prophet/docs/quick_start.html).  \nThere is another prophet kernel: Vopani's [great kernel](https://www.kaggle.com/rohanrao/ashrae-prophet-s-prophecy), but I prepared this kernel for a few days so I will release it.\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import gc\nimport os\nimport sys\nimport time\nimport numpy as np\nimport pandas as pd\nimport feather\nimport pickle\n\nfrom sklearn.model_selection import KFold, TimeSeriesSplit\nfrom sklearn.metrics import log_loss, accuracy_score, mean_squared_error\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\n\nfrom fbprophet import Prophet\nfrom fbprophet.diagnostics import cross_validation\nfrom fbprophet.diagnostics import performance_metrics\nfrom fbprophet.plot import plot_cross_validation_metric\nfrom fbprophet.plot import add_changepoints_to_plot\nfrom fbprophet.plot import plot_plotly\nfrom fbprophet.plot import plot_yearly\nimport plotly.offline as py\nfrom datetime import date\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"os.listdir('../input/ashrae-energy-prediction')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\ntest_ = pd.read_csv('../input/ashrae-energy-prediction/test.csv')\nweather_train_ = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')\nweather_test_ = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\nmetadata = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# select 1 building\nbuilding_id = 1085\nsite_id = metadata[metadata['building_id']==building_id]['site_id'].values[0]\ntrain = train_[train_['building_id'] == building_id]\ntest = test_[test_['building_id'] == building_id]\ntrain['timestamp'] = pd.to_datetime(train['timestamp'])\ntest['timestamp'] = pd.to_datetime(test['timestamp'])\ntrain = train[['timestamp', 'meter_reading']].reset_index(drop=True)\ntest = test[['timestamp']].reset_index(drop=True)\ntrain.columns = ['ds', 'y']\ntest.columns = ['ds']\nweather_train = weather_train_[weather_train_['site_id']==site_id].reset_index(drop=True)\nweather_test = weather_test_[weather_test_['site_id']==site_id].reset_index(drop=True)\nweather_train['timestamp'] = pd.to_datetime(weather_train['timestamp'])\nweather_test['timestamp'] = pd.to_datetime(weather_test['timestamp'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['y'].plot();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_test = 2000\nX_train = train[:-n_test]\nX_valid = train[-n_test:]\ny_valid = X_valid['y']\nX_valid = X_valid.drop('y', axis=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Prophet"},{"metadata":{},"cell_type":"markdown","source":"Prophet can set holidays and how long effect it is."},{"metadata":{"trusted":true},"cell_type":"code","source":"# holidays\n# thanks to https://www.kaggle.com/rohanrao/ashrae-half-and-half\nholidays = [\"2016-01-01\", \"2016-01-18\", \"2016-02-15\", \"2016-05-30\", \"2016-07-04\",\n            \"2016-09-05\", \"2016-10-10\", \"2016-11-11\", \"2016-11-24\", \"2016-12-26\",\n            \"2017-01-01\", \"2017-01-16\", \"2017-02-20\", \"2017-05-29\", \"2017-07-04\",\n            \"2017-09-04\", \"2017-10-09\", \"2017-11-10\", \"2017-11-23\", \"2017-12-25\",\n            \"2018-01-01\", \"2018-01-15\", \"2018-02-19\", \"2018-05-28\", \"2018-07-04\",\n            \"2018-09-03\", \"2018-10-08\", \"2018-11-12\", \"2018-11-22\", \"2018-12-25\",\n            \"2019-01-01\"]\nholidays = pd.DataFrame({\n  'holiday': 'holiday',\n  'ds': pd.to_datetime(holidays),\n  'lower_window': 0,\n  'upper_window': 0,\n})\n# Black Friday\nholidays.loc[holidays['ds'] == pd.to_datetime('2016-11-24'), 'upper_window'] = 1\nholidays.loc[holidays['ds'] == pd.to_datetime('2017-11-23'), 'upper_window'] = 1\nholidays.loc[holidays['ds'] == pd.to_datetime('2018-11-22'), 'upper_window'] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"params_prophet = {\n    'growth': 'logistic', # Prophet allows you to make forecasts using a logistic growth trend model\n    'changepoint_prior_scale' :0.03, # trend flexibility default 0.05\n    'holidays': holidays\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train['floor'] = X_train['y'].min()\nX_train['cap'] = X_train['y'].max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Plane Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"m = Prophet(**params_prophet)\nm.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#future = m.make_future_dataframe(periods=365*24, freq='H')\nfuture = pd.concat([X_train[['ds']], X_valid])\nfuture['floor'] = X_train['y'].min()\nfuture['cap'] = X_train['y'].max()\nfcst = m.predict(future)\nfig = m.plot(fcst)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = m.plot(fcst)\na = add_changepoints_to_plot(fig.gca(), m, fcst)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Prophet predict linear trend, because have only one year data. We cannot capture seasoneal trends."},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = m.plot_components(fcst)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Add exogenous features"},{"metadata":{},"cell_type":"markdown","source":"In this competition we can use some features, thus we add some features to model as exogenous features."},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train = weather_train.fillna(0)\nX_train = pd.merge(X_train, weather_train.drop('site_id', axis=1), left_on = 'ds', right_on = 'timestamp').drop('timestamp', axis=1)\nfuture = pd.merge(future, weather_train.drop('site_id', axis=1), left_on = 'ds', right_on = 'timestamp').drop('timestamp', axis=1)\nX_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cols = ['air_temperature', 'cloud_coverage','dew_temperature', 'precip_depth_1_hr', 'sea_level_pressure', 'wind_direction', 'wind_speed']\nm = Prophet(**params_prophet)\nfor col in cols:\n    m.add_regressor(col)\nm.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcst = m.predict(future)\nfig = m.plot(fcst)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predict test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_test = weather_test.fillna(0)\ntest['floor'] = X_train['y'].min()\ntest['cap'] = X_train['y'].max()\nX_test = pd.merge(test, weather_test.drop('site_id', axis=1), left_on = 'ds', right_on = 'timestamp').drop('timestamp', axis=1)\nX_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcst = m.predict(X_test)\npreds = fcst.yhat","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds.plot();","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}