{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport statsmodels.api as sm\nfrom pylab import rcParams\nfrom statsmodels.tsa.stattools import adfuller\nfrom statsmodels.tsa.stattools import acf, pacf\nfrom statsmodels.graphics.tsaplots import plot_acf,plot_pacf \nfrom statsmodels.tsa.arima_model import ARIMA\nimport matplotlib.ticker as ticker","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv', parse_dates=['timestamp'])\ndf_weather_train = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv',parse_dates=['timestamp'])\ndf_meta_data = pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Data Analysis."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_meta_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_weather_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Correlation Check"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.corrwith(df_weather_train.air_temperature)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.corrwith(df_weather_train.cloud_coverage)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.corrwith(df_weather_train.wind_direction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.corrwith(df_weather_train.wind_speed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.corrwith(df_weather_train.sea_level_pressure)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Correlation looks low between training data and weather data. So continue with Training data only ."},{"metadata":{},"cell_type":"markdown","source":"Merging of Training data with MetaData to include site_id variable"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_merge_train_md = pd.merge(df_train,df_meta_data, how='left', on='building_id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_merge_train_md.count()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Converting meter reading of site_id \"0\" to kwH"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_merge_train_md.loc[(df_merge_train_md['site_id'] == 0) & (df_merge_train_md['meter'] == 0),'meter_reading'] = \\\n      df_merge_train_md[(df_merge_train_md['site_id'] == 0) & (df_merge_train_md['meter'] == 0)] \\\n      ['meter_reading'] * 0.293","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Filtering data based on meter type usage like 0,1,2 & 3"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_meterType_0_train = df_merge_train_md[(df_merge_train_md.meter == 0) & (df_merge_train_md.meter_reading>0)]\ndf_meterType_1_train = df_merge_train_md[(df_merge_train_md.meter == 1) & (df_merge_train_md.meter_reading>0)]\ndf_meterType_2_train = df_merge_train_md[(df_merge_train_md.meter == 2) & (df_merge_train_md.meter_reading>0)]\ndf_meterType_3_train = df_merge_train_md[(df_merge_train_md.meter == 3) & (df_merge_train_md.meter_reading>0)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_meterType_3_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As there is no relation among data, so either we can use non parametric model or time series. Considering time series for now to forecast the meter reading"},{"metadata":{},"cell_type":"markdown","source":"**TimeSeries Data Exploration**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_0_train_ts = df_meterType_0_train[['timestamp','meter_reading']]\ndf_mt_1_train_ts = df_meterType_1_train[['timestamp','meter_reading']]\ndf_mt_2_train_ts = df_meterType_2_train[['timestamp','meter_reading']]\ndf_mt_3_train_ts = df_meterType_3_train[['timestamp','meter_reading']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_0_train_ts.set_index('timestamp', inplace=True)\ndf_mt_1_train_ts.set_index('timestamp', inplace=True)\ndf_mt_2_train_ts.set_index('timestamp', inplace=True)\ndf_mt_3_train_ts.set_index('timestamp', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_0_train_ts.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_1_train_ts.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_2_train_ts.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_3_train_ts.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Converting daily to weekly meter reading for each meter type**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_0_train_ts_w=df_mt_0_train_ts.meter_reading.resample('w').sum()\ndf_mt_1_train_ts_w=df_mt_1_train_ts.meter_reading.resample('w').sum()\ndf_mt_2_train_ts_w=df_mt_2_train_ts.meter_reading.resample('w').sum()\ndf_mt_3_train_ts_w=df_mt_3_train_ts.meter_reading.resample('w').sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_0_train_ts_w.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_1_train_ts_w.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_2_train_ts_w.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_mt_3_train_ts_w.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots()\nax.plot( df_mt_0_train_ts_w,marker='.', linestyle='-', linewidth=0.5, label='Weekly Meter Type 0',color='green')\nax.plot( df_mt_1_train_ts_w,marker='.', linestyle='-', linewidth=0.5, label='Weekly Meter Type 1',color='red')\nax.plot( df_mt_2_train_ts_w,marker='.', linestyle='-', linewidth=0.5, label='Weekly Meter Type 2',color='blue')\nax.plot( df_mt_3_train_ts_w,marker='.', linestyle='-', linewidth=0.5, label='Weekly Meter Type 3',color='magenta')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax2 = plt.subplots()\nax2.plot(df_mt_0_train_ts_w, color='black', label='meter type 0')\ndf_mt_1_train_ts_w.plot(label='meter type 1',color='blue')\ndf_mt_2_train_ts_w.plot(label='meter type 2',color='red')\ndf_mt_3_train_ts_w.plot(label='meter type 3',color='green')\nax2.legend()\nax2.set_ylabel('Weekly Total (KWh)');","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Time Series Decomposition to review trend , seasonal , Residual etc**"},{"metadata":{},"cell_type":"markdown","source":"Meter Type 0"},{"metadata":{"trusted":true},"cell_type":"code","source":"rcParams['figure.figsize'] = 20, 8\ndecomposition = sm.tsa.seasonal_decompose(df_mt_0_train_ts_w, model='additive')\nfig = decomposition.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Meter Type -1"},{"metadata":{"trusted":true},"cell_type":"code","source":"rcParams['figure.figsize'] = 20, 8\ndecomposition1 = sm.tsa.seasonal_decompose(df_mt_1_train_ts_w, model='multiplicative')\nfig1 = decomposition1.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Meter Type -2"},{"metadata":{"trusted":true},"cell_type":"code","source":"rcParams['figure.figsize'] = 20, 8\ndecomposition2 = sm.tsa.seasonal_decompose(df_mt_2_train_ts_w, model='additive')\nfig2 = decomposition2.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Meter Type -3 "},{"metadata":{"trusted":true},"cell_type":"code","source":"rcParams['figure.figsize'] = 20, 8\ndecomposition3 = sm.tsa.seasonal_decompose(df_mt_3_train_ts_w, model='additive')\nfig3 = decomposition3.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Testing for Stationarity**"},{"metadata":{},"cell_type":"markdown","source":"**Dickey-Fuller Test Function**"},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_stationarity(timeseries):\n    \n    #Determing rolling statistics\n    rolmean = timeseries.rolling(7).mean()\n    rolstd = timeseries.rolling(7).std()\n\n    #Plot rolling statistics:\n    orig = plt.plot(timeseries, color='blue',label='Original')\n    mean = plt.plot(rolmean, color='red', label='Rolling Mean')\n    std = plt.plot(rolstd, color='black', label = 'Rolling Std')\n    plt.legend(loc='best')\n    plt.title('Rolling Mean & Standard Deviation')\n    plt.show(block=False)\n    \n    #Perform Dickey-Fuller test:\n    print('Results of Dickey-Fuller Test:')\n    dftest = adfuller(timeseries, autolag='AIC')\n    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])\n    for key,value in dftest[4].items():\n        dfoutput['Critical Value (%s)'%key] = value\n    print (dfoutput)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 0 Stationary Test**"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_stationarity(df_mt_0_train_ts_w)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 1 Stationary Test**"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_stationarity(df_mt_1_train_ts_w)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 2 Stationary Test**"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_stationarity(df_mt_2_train_ts_w)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 3 Stationary Test**"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_stationarity(df_mt_3_train_ts_w)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Transformation and Differencing**"},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 0 Transformation **"},{"metadata":{"trusted":true},"cell_type":"code","source":"ts_log_0 = np.log(df_mt_0_train_ts_w)\nts_log_diff_0 = ts_log_0 - df_mt_0_train_ts_w.shift()\nplt.plot(ts_log_diff_0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 1 Transformation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"ts_log_1 = np.log(df_mt_1_train_ts_w)\nts_log_diff_1 = ts_log_1 - df_mt_1_train_ts_w.shift()\nplt.plot(ts_log_diff_1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 2 Transformation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"ts_log_2 = np.log(df_mt_2_train_ts_w)\nts_log_diff_2 = ts_log_2 - df_mt_2_train_ts_w.shift()\nplt.plot(ts_log_diff_2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 3 Transformation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"ts_log_3 = np.log(df_mt_3_train_ts_w)\nts_log_diff_3 = ts_log_3 - df_mt_3_train_ts_w.shift()\nplt.plot(ts_log_diff_3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**ACF and PACF plots:**\n"},{"metadata":{},"cell_type":"markdown","source":"**Meter Type - 0**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_acf(df_mt_0_train_ts_w,title='Test',lags=50);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lag_acf_0 = acf(df_mt_0_train_ts_w, nlags=20)\nlag_pacf_0 = pacf(df_mt_0_train_ts_w, nlags=20, method='ols')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**ACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot ACF: \nplt.subplot(121) \nplt.plot(lag_acf_0)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_0_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_0_train_ts_w)),linestyle='--',color='gray')\nplt.title('Autocorrelation Function')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot PACF:\nplt.subplot(122)\nplt.plot(lag_pacf_0)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_0_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_0_train_ts_w)),linestyle='--',color='gray')\nplt.title('Partial Autocorrelation Function')\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_pacf(df_mt_0_train_ts_w,title='PACF',lags=20);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 1: ACF and PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_acf(df_mt_1_train_ts_w,title='Test',lags=50);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lag_acf_1 = acf(df_mt_1_train_ts_w, nlags=20)\nlag_pacf_1 = pacf(df_mt_1_train_ts_w, nlags=20, method='ols')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplot(121) \nplt.plot(lag_acf_1)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_1_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_1_train_ts_w)),linestyle='--',color='gray')\nplt.title('Autocorrelation Function')\nplot_acf(df_mt_1_train_ts_w,title='Test',lags=50);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#PACF\nplt.subplot(122)\nplt.plot(lag_pacf_1)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_1_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_1_train_ts_w)),linestyle='--',color='gray')\nplt.title('Partial Autocorrelation Function')\nplt.tight_layout()\nplot_pacf(df_mt_1_train_ts_w,title='PACF',lags=20);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 2: ACF and PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"lag_acf_2 = acf(df_mt_2_train_ts_w, nlags=20)\nlag_pacf_2 = pacf(df_mt_2_train_ts_w, nlags=20, method='ols')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**ACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplot(121) \nplt.plot(lag_acf_2)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_2_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_2_train_ts_w)),linestyle='--',color='gray')\nplt.title('Autocorrelation Function')\nplot_acf(df_mt_2_train_ts_w,title='Test',lags=50);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#PACF\nplt.subplot(122)\nplt.plot(lag_pacf_2)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_2_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_2_train_ts_w)),linestyle='--',color='gray')\nplt.title('Partial Autocorrelation Function')\nplt.tight_layout()\nplot_pacf(df_mt_2_train_ts_w,title='PACF',lags=20);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 3: ACF and PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"lag_acf_3 = acf(df_mt_3_train_ts_w, nlags=20)\nlag_pacf_3 = pacf(df_mt_3_train_ts_w, nlags=20, method='ols')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**ACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplot(121) \nplt.plot(lag_acf_3)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_3_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_3_train_ts_w)),linestyle='--',color='gray')\nplt.title('Autocorrelation Function')\nplot_acf(df_mt_3_train_ts_w,title='Test',lags=50);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**PACF**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#PACF\nplt.subplot(122)\nplt.plot(lag_pacf_3)\nplt.axhline(y=0,linestyle='--',color='gray')\nplt.axhline(y=-1.96/np.sqrt(len(df_mt_3_train_ts_w)),linestyle='--',color='gray')\nplt.axhline(y=1.96/np.sqrt(len(df_mt_3_train_ts_w)),linestyle='--',color='gray')\nplt.title('Partial Autocorrelation Function')\nplt.tight_layout()\nplot_pacf(df_mt_3_train_ts_w,title='PACF',lags=20);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**AREMA Model**"},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 0: AREMA Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model_0 = ARIMA(df_mt_0_train_ts_w, order=(2, 0, 2))  \nresults_AR_0 = model_0.fit(disp=-1)  \nplt.plot(ts_log_diff_0)\nplt.plot(results_AR_0.fittedvalues, color='red')\nplt.title('RSS: %.4f'% sum((results_AR_0.fittedvalues-ts_log_diff_1)**2))\nresults_AR_0.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcast_0 = results_AR_0.predict(len(df_mt_0_train_ts_w),len(df_mt_0_train_ts_w)+10)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"formatter = ticker.StrMethodFormatter('{x:,.0f}')\ntitle = 'Meter Reading of Type - 0'\nylabel='Weekly Meter Reading'\nxlabel='' # we don't really need a label here\n\nax = df_mt_0_train_ts_w.plot(legend=True,figsize=(12,6),title=title)\nfcast_0.plot(legend=True,label='Forecast')\nax.autoscale(axis='x',tight=True)\nax.set(xlabel=xlabel, ylabel=ylabel)\nax.yaxis.set_major_formatter(formatter);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(fcast_0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 1: AREMA Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model_1 = ARIMA(df_mt_1_train_ts_w, order=(0, 1, 2))  \nresults_AR_1 = model_1.fit(disp=-1)  \nplt.plot(ts_log_diff_1)\nplt.plot(results_AR_1.fittedvalues, color='red')\nplt.title('RSS: %.4f'% sum((results_AR_1.fittedvalues-ts_log_diff_1)**2))\nresults_AR_1.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcast_1 = results_AR_1.predict(len(df_mt_1_train_ts_w),len(df_mt_1_train_ts_w)+10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Forecast "},{"metadata":{"trusted":true},"cell_type":"code","source":"formatter = ticker.StrMethodFormatter('{x:,.0f}')\ntitle = 'Meter Reading of Type - 1'\nylabel='Weekly Meter Reading'\nxlabel='' # we don't really need a label here\n\nax = df_mt_1_train_ts_w.plot(legend=True,figsize=(12,6),title=title)\nfcast_1.plot(legend=True,label='Forecast')\nax.autoscale(axis='x',tight=True)\nax.set(xlabel=xlabel, ylabel=ylabel)\nax.yaxis.set_major_formatter(formatter);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(fcast_1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 2: AREMA Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model_2 = ARIMA(df_mt_2_train_ts_w, order=(2, 1, 2))  \nresults_AR_2 = model_2.fit(disp=-1)  \nplt.plot(ts_log_diff_2)\nplt.plot(results_AR_2.fittedvalues, color='red')\nplt.title('RSS: %.4f'% sum((results_AR_2.fittedvalues-ts_log_diff_2)**2))\nresults_AR_2.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcast_2 = results_AR_2.predict(len(df_mt_2_train_ts_w),len(df_mt_2_train_ts_w)+10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"formatter = ticker.StrMethodFormatter('{x:,.0f}')\ntitle = 'Meter Reading of Type - 1'\nylabel='Weekly Meter Reading'\nxlabel='' # we don't really need a label here\n\nax = df_mt_2_train_ts_w.plot(legend=True,figsize=(12,6),title=title)\nfcast_2.plot(legend=True,label='Forecast')\nax.autoscale(axis='x',tight=True)\nax.set(xlabel=xlabel, ylabel=ylabel)\nax.yaxis.set_major_formatter(formatter);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(fcast_2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Meter Type 3: AREMA Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model_3 = ARIMA(df_mt_3_train_ts_w, order=(2, 0, 2))  \nresults_AR_3 = model_3.fit(disp=-1)  \nplt.plot(ts_log_diff_3)\nplt.plot(results_AR_3.fittedvalues, color='red')\nplt.title('RSS: %.4f'% sum((results_AR_3.fittedvalues-ts_log_diff_3)**2))\nresults_AR_3.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fcast_3 = results_AR_3.predict(len(df_mt_3_train_ts_w),len(df_mt_3_train_ts_w)+10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Forecast**"},{"metadata":{"trusted":true},"cell_type":"code","source":"formatter = ticker.StrMethodFormatter('{x:,.0f}')\ntitle = 'Meter Reading of Type - 3'\nylabel='Weekly Meter Reading'\nxlabel='' # we don't really need a label here\n\nax = df_mt_3_train_ts_w.plot(legend=True,figsize=(12,6),title=title)\nfcast_3.plot(legend=True,label='Forecast')\nax.autoscale(axis='x',tight=True)\nax.set(xlabel=xlabel, ylabel=ylabel)\nax.yaxis.set_major_formatter(formatter);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(fcast_3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}