{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport gc\nfrom  pathlib import Path\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/ashrae-energy-prediction/')\nbuilding_file = path / 'building_metadata.csv'\nweather_file = path / 'weather_train.csv'\ntrain_file = path / 'train.csv'\ntest_file = path / 'test.csv'\nweathertest_file = path / 'weather_test.csv'\n#sample_file = path / 'sample_submission.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nmydateparser = lambda x: pd.datetime.strptime(x, \"%Y-%m-%d %H:%M:%S\")\nwt = pd.read_csv(weather_file, parse_dates=['timestamp'], date_parser=mydateparser )\ntestwt = pd.read_csv(weathertest_file, parse_dates=['timestamp'], date_parser=mydateparser)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"allwt = pd.concat([wt, testwt], axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"allwt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.tabular.transform import add_datepart\nadd_datepart(allwt, field_name='timestamp', prefix=\"ts\", drop=False, time=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"allwt.tsYear","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"allwt[\"nYear\"] = allwt.tsYear  - 2016\nallwt[\"nMonth\"] = allwt.nYear * 12 + allwt.tsMonth","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Group hourly data to monthly and getting Min, Max and Mean."},{"metadata":{"trusted":true},"cell_type":"code","source":"wts = allwt.groupby(['site_id', 'nMonth'])['air_temperature'].agg(['min', 'max', 'mean']).reset_index()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Peek on weather data in year 2016"},{"metadata":{"trusted":true},"cell_type":"code","source":"f, axes = plt.subplots(4, 4,figsize=(25,30))\nplt.subplots_adjust(hspace=0.5)\nfor i in range(0,16):\n    wts[ (wts.site_id == i) & (wts.nMonth.isin(range(1,13)))].plot('nMonth', ['min', 'mean', 'max'], \n                                       title=f'site:{i}', \n                                       figure = f, ax=axes[ (i// 4), (i % 4) ], \n                                       grid=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Plot on the same y-axis scale"},{"metadata":{"trusted":true},"cell_type":"code","source":"f, axes = plt.subplots(4, 4,figsize=(25,30) , sharey = True)\nplt.subplots_adjust(hspace=0.5)\nfor i in range(0,16):\n    wts[ (wts.site_id == i) & (wts.nMonth.isin(range(1,13)))].plot('nMonth', ['min', 'mean', 'max'], title=f'site:{i}', figure = f, ax=axes[ (i//4), (i % 4) ])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 36 months"},{"metadata":{},"cell_type":"markdown","source":"Lets see the weather data we have in train and test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"f, axes = plt.subplots(4, 4,figsize=(25,30))\nplt.subplots_adjust(hspace=0.5)\nfor i in range(0,16):\n    wts[ (wts.site_id == i) & (wts.nMonth.isin(range(1,37)))].plot('nMonth', ['min', 'mean', 'max'], \n                                       title=f'site:{i}', \n                                       figure = f, ax=axes[ (i // 4), (i % 4) ], \n                                       grid=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, axes = plt.subplots(4, 4,figsize=(25,30), sharey=True)\nplt.subplots_adjust(hspace=0.5)\nfor i in range(0,16):\n    wts[ (wts.site_id == i) & (wts.nMonth.isin(range(1,37)))].plot('nMonth', ['min', 'mean', 'max'], \n                                                           title=f'site:{i}', \n                                                           figure = f, ax=axes[ (i//4), (i % 4) ], )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Heatmap"},{"metadata":{"trusted":true},"cell_type":"code","source":"heatdf = wts [ (wts.site_id == 0) & (wts.nMonth.isin(range(1,13))) ][['mean']].reset_index()\nheatdf.index = heatdf.index + 1\nheatdf = heatdf.T\nheatdf.drop('index', axis=0, inplace=True)\nfor i in range(1,16):\n    temp = wts [ (wts.site_id == i) & (wts.nMonth.isin(range(1,13))) ][['mean']].reset_index()\n    temp.index = temp.index + 1\n    temp = temp.T\n    heatdf = pd.concat( [heatdf, temp.drop('index', axis=0)], axis=0 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"heatdf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure( figsize=(25,15))\nax = fig.add_subplot(111)\nax.set_xlabel('Month in 2016')\nax.set_title(label='HeatMap for Average Temperature in 2016 for 16 station ')\n\nax.set_xticks(np.arange(13))\nax.set_yticks(np.arange(16))\nax.set_xticklabels( [ i for i in range(1,13)] )\nax.set_yticklabels( [ f'site_{i}' for i in range(0,16) ])\n\n# Loop over data dimensions and create text annotations.\nfor i in range(12):\n    for j in range(16):\n        text = ax.text(i, j, \n                       heatdf.iloc[j, i].round(2),\n                       ha=\"center\", va=\"center\", color=\"black\")\n\nim = ax.imshow( heatdf , interpolation='nearest' , cmap='coolwarm')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **Any interesting insight you found? **\n## Can we figure out the station is from which city ? Please comment"},{"metadata":{},"cell_type":"markdown","source":"# Please **upvote** if you found this kernel is helpful."}],"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}