{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction\n\nThe aim of this notebook is to analyse the relation between energy and temperature."},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport plotly.express as px\nfrom IPython.core.display import display, HTML\nimport pickle","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Import data"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = '../input/ashrae-data-wrangling-csv-to-pickle/'\nwith open(data_path + 'X_train.pickle', 'rb') as f:\n    X_train = pickle.load(f)\nwith open(data_path + 'nan.pickle', 'rb') as f:\n    nan = pickle.load(f)    \nwith open(data_path + 'building_metadata.pickle', 'rb') as f:\n    building_metadata = pickle.load(f)\nwith open(data_path + 'weather_train.pickle', 'rb') as f:\n    weather_train = pickle.load(f)\n    \nworld_cities = pd.read_csv('../input/world-cities/worldcities.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train['timestamp'] = pd.to_datetime(weather_train['timestamp'], format='%Y-%m-%d %H:%M:%S')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train['hour'] = weather_train['timestamp'].dt.hour\nweather_train['day'] = weather_train['timestamp'].dt.day\nweather_train['week'] = weather_train['timestamp'].dt.week\nweather_train['month'] = weather_train['timestamp'].dt.month","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":"weather_hour = weather_train.groupby(by=['site_id', 'hour']).agg([np.mean, np.std])\nweather_hour.reset_index(inplace=True)\nweather_hour","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_hour['air_temperature']['mean']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"level_0 = weather_hour.columns.droplevel(0)\nlevel_1 = weather_hour.columns.droplevel(1)\nlevel_0 = ['' if x == '' else '-' + x for x in level_0]\nweather_hour.columns = level_1 + level_0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\npalette = sns.color_palette(\"Oranges\", 16)\npalette_hex = [f'#{int(256*x[0]):02x}{int(256*x[1]):02x}{int(256*x[2]):02x}'for x in palette]\nfig = px.line(weather_hour,\n             x='hour',\n             y='air_temperature-mean',\n             color='site_id',\n             color_discrete_sequence=palette_hex)\nfig.update_layout()\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_hour","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"time_offset = weather_hour[['site_id', 'air_temperature-mean']].groupby(by=['site_id']).agg(['idxmin'])\ntime_offset[('air_temperature-mean', 'idxmin')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_time_offset = weather_hour.iloc[time_offset[('air_temperature-mean', 'idxmin')]][['site_id', 'hour']]\nweather_time_offset.reset_index(drop=True, inplace=True)\nweather_time_offset['offset'] = weather_time_offset['hour'] - weather_time_offset['hour'].min()\nweather_time_offset.drop(['hour'], axis='columns', inplace=True)\nweather_time_offset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_time_offset['city'] = ['Jacksonville',\n                               'London',\n                               'Phoenix',\n                               'Philadelphia',\n                               'San Francisco',\n                               'Leicester',\n                               'Philadelphia',\n                               'Montréal',\n                               'Jacksonville',\n                               'San Antonio',\n                               'Las Vegas',\n                               'Montréal',\n                               'Dublin',\n                               'Minneapolis',\n                               'Philadelphia',\n                               'Pittsburgh']\nweather_time_offset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_city_info(city):\n    results = world_cities[world_cities['city'] == city].sort_values(by='population', ascending=False)[['lat', 'lng', 'country', 'admin_name', 'population']]\n    if results.empty:\n        return None\n    else:\n        return results.iloc[0]\n\ncity_info = weather_time_offset['city'].apply(get_city_info)\ncity_info","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_time_offset = pd.concat([weather_time_offset, city_info], axis='columns')\nweather_time_offset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import plotly.express as px\nfig = px.scatter_geo(weather_time_offset,\n                     lat='lat',\n                     lon='lng',\n                     color='offset',\n                     size='population',\n                     hover_name='city',\n                     hover_data=['admin_name', 'country'],\n                     projection='kavrayskiy7',\n                     color_continuous_scale=px.colors.sequential.Plasma)\nfig.show()","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}