{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bangkok_temp = pd.read_csv('../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Train/3H_temperature_Bangkok.csv')\nbangkok_temp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bangkok_wind = pd.read_csv('../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Train/3H_wind_Bangkok.csv')\nbangkok_wind.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bangkok = bangkok_temp.merge(bangkok_wind)\nbangkok.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bangkok['datetime'] = pd.to_datetime(bangkok['datetime'])\nbangkok.set_index(bangkok['datetime'],inplace=True)\nbangkok.drop(columns={'datetime',\"lat\",\"long\"},inplace=True)\nbangkok = bangkok.resample('h').ffill()\nbangkok.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bkk_pm25 = pd.read_csv('../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Train/Bangkok.txt', sep='\\t', header=None, skiprows=10)\nbkk_pm25.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bkk_pm25['Time'] = pd.to_datetime({'year': bkk_pm25[0], 'month': bkk_pm25[1], 'day':bkk_pm25[2],'hour': bkk_pm25[3]})\nbkk_pm25['Time'] = bkk_pm25['Time'].dt.tz_localize('UTC').dt.tz_convert('Asia/Bangkok')\nbkk_pm25['Time'] = bkk_pm25['Time'].dt.tz_localize(None)\nbkk_pm25=bkk_pm25.drop([0,1,2,3,5,6],axis=1)\nbkk_pm25.columns = ['PM2.5(µg/m3)', 'Time']\nbkk_pm25 = bkk_pm25.set_index('Time')\nbkk_pm25.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bkk_pm25 = bkk_pm25[~bkk_pm25.index.duplicated(keep='first')]\nbkk_pm25.index = pd.DatetimeIndex(bkk_pm25.index)\nbkk_pm25 = bkk_pm25.reindex(pd.date_range(\"2016-03-03 15:00:00\", \"2019-03-18 06:00:00\",freq='h'), fill_value=np.nan)\nbkk_pm25 = bkk_pm25.interpolate()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(12,6))\nplt.plot(bkk_pm25,'r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bkk_train = bkk_pm25.merge(bangkok,left_index=True,right_index=True)\nbkk_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nfrom statsmodels.tsa.statespace.sarimax import SARIMAX","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"best_order = (0, 1, 1)\nbest_seasonal_order = (1, 1, 0, 12)\nexog_columns = ['Temp(C)','WindDir','Wind Speed(km/h)']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod = SARIMAX(bkk_train['PM2.5(µg/m3)'],\n              exog=bkk_train[exog_columns],\n              order=best_order,\n              seasonal_order=best_seasonal_order,\n              enforce_stationarity=False,\n              enforce_invertibility=False)\n\nresults = mod.fit()\n\nprint(results.summary().tables[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mergeData(temp, wind, pm):\n    df_temp = pd.read_csv(temp)\n    df_wind = pd.read_csv(wind)\n    df_pm = pd.read_csv(pm, sep='\\t', header=None, skiprows=10)\n    df_tw = df_temp.merge(df_wind)\n    df_tw['datetime'] = pd.to_datetime(df_tw['datetime'])\n    df_tw.set_index(df_tw['datetime'],inplace=True)\n    df_tw.drop(columns={'datetime',\"lat\",\"long\"},inplace=True)\n    df_tw = df_tw.resample('h').ffill()\n    df_pm['Time'] = pd.to_datetime({'year': df_pm[0], 'month': df_pm[1], 'day':df_pm[2],'hour': df_pm[3]})\n    df_pm['Time'] = df_pm['Time'].dt.tz_localize('UTC').dt.tz_convert('Asia/Bangkok')\n    df_pm['Time'] = df_pm['Time'].dt.tz_localize(None)\n    df_pm=df_pm.drop([0,1,2,3,5,6],axis=1)\n    df_pm.columns = ['PM2.5(µg/m3)', 'Time']\n    df_pm = df_pm.set_index('Time')\n    df =  df_pm.merge(df_tw,left_index=True,right_index=True)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bkk_test = mergeData(\"../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Test/3H_temperature_Bangkok.csv\",\\\n                    \"../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Test/3H_wind_Bangkok.csv\",\\\n                    \"../input/2110446-data-science-and-data-engineering-2021/PM2.5_kaggle 2021/Bangkok/Test/Bangkok.txt\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = results.predict(bkk_test, exog=bkk_test[exog_columns], dynamic=False)\npred_ci = pred.conf_int()","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":4}