{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Introduction\n\nThis notebook focuses only on weather & Building data.\nI am trying to make dashboard style eda by using plotly.\n\nThis is my results.\n\n![dashboard](https://i.imgur.com/8BdGI8I.png)\n\n- Building Primary Use Pie chart\n- wind direction & speed bar polar chart\n- other weather line charts\n\n**If you find it helpful or if you like it, please upvote it.**"},{"metadata":{},"cell_type":"markdown","source":"## Simple Preprocessing"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns \nimport plotly.graph_objects as go\nimport plotly.express as px\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport os \nprint(os.listdir('../input/ashrae-energy-prediction/'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"weather_train = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')\nweather_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\n\nbuilding_metadata = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Weather"},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import missingno as msno\nmsno.matrix(weather_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"msno.matrix(weather_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Fill Null Data as 0\n\n- cloud_coverage\n- previp_depth_1_hr\n- wind_direction\n- wind_speed\n\nFill Null Data as Mean Value\n\n- sea_level_pressure"},{"metadata":{"trusted":true},"cell_type":"code","source":"features = ['cloud_coverage', 'precip_depth_1_hr', 'wind_direction', 'wind_speed']\nweather_train[features] = weather_train[features].fillna(0)\nweather_test[features] = weather_test[features].fillna(0)\nweather_train.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mean_value = weather_train['sea_level_pressure'].dropna().sum()/weather_train['sea_level_pressure'].dropna().shape[0]\nprint(mean_value)\nweather_train['sea_level_pressure'] = weather_train['sea_level_pressure'].fillna(mean_value)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Building"},{"metadata":{"trusted":true},"cell_type":"code","source":"msno.matrix(building_metadata)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata['site_id'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,1,figsize=(12, 5))\nsns.countplot(building_metadata['site_id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,1,figsize=(12, 5))\nsns.countplot(y = building_metadata['primary_use'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Site Id & Primary Use"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\ndata = list()\nfor i in building_metadata['primary_use'].unique():\n    data.append(go.Bar(name=i, x=building_metadata['site_id'].unique(), y= building_metadata[building_metadata['primary_use']==i]['site_id'].value_counts().sort_index()))\n\nfig = go.Figure(data=data)\nfig.update_layout(barmode='stack')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def building_pie_chart(df, siteid):\n    data = df['primary_use'].value_counts()\n    fig = go.Figure(data=[go.Pie(values=data, labels=data.index)])\n    fig.update_layout(template='plotly_dark', title_text=\"Building in Site: {}\".format(siteid))\n    fig.show()\n\nbuilding_pie_chart(building_metadata[building_metadata['site_id']==0],0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Wind Direction and Wind Speed "},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(17, 5))\nsns.distplot(weather_train['wind_direction'], ax=ax[0])\nsns.distplot(weather_train['wind_speed'], ax=ax[1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ndef angle_range(st, ed, x):\n    if st < ed: ed += 360\n    return st <= x.values < ed\n\ndef wind_news(df):\n    df = df[df['wind_direction'] > 0]\n    df['wind_direction'][ df['wind_direction'] < 11.25 ] = df['wind_direction'][ df['wind_direction'] < 11.25 ].apply(lambda x : x+360)\n    \n    wind_df = pd.DataFrame(columns=['direction', 'speed', 'frequency'])\n    angle = [ (i * 22.5 - 11.25 + 360) % 360.0 for i in range(16) ]\n    direction = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WMW', 'NW', 'NNW']\n    speed = [i*2.5 for i in range(1, 9)]\n    tot = 0\n    for ang, d  in zip(angle, direction):\n        for s in speed:\n            cnt = df[(ang <= df['wind_direction'] ) & ( df['wind_direction'] < ang+22.5 )&  (s-2.5 <= df['wind_speed']) & (df['wind_speed'] < s) ].shape[0]\n            tot += cnt\n            wind_df = wind_df.append({'direction': d, 'speed':s, 'frequency': cnt }, ignore_index=True)\n    return wind_df ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def wind_direction_plot(df, title):\n    wind = wind_news(df)\n    fig = px.bar_polar(wind, r=\"frequency\", theta=\"direction\",\n                       color=\"speed\", template=\"plotly_dark\",\n                       color_discrete_sequence= px.colors.sequential.Plasma[-2::-1])\n    fig.update_layout(title_text='Site ID {}'.format(title))\n    fig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wind_direction_plot(weather_train[weather_train['site_id'] == 0], str(0))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Temperature"},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef temperature_graph(df):\n    df = df.sort_values('timestamp')\n    fig = go.Figure()\n    fig.add_trace(go.Scatter(x=df['timestamp'], y=df['air_temperature'], name=\"Air Temperature\", line_color='deepskyblue', opacity=0.7))\n    fig.add_trace(go.Scatter(x=df['timestamp'], y=df['dew_temperature'], name=\"Dew Temperature\", line_color='mediumseagreen', opacity=0.7))\n    fig.update_layout(template='plotly_dark', title_text='Time Series with Temperature', xaxis_rangeslider_visible=True)\n    fig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temperature_graph(weather_train[weather_train['site_id'] == 0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cloud and Pressure"},{"metadata":{"trusted":true},"cell_type":"code","source":"def cloud_graph(df):\n    df = df.sort_values('timestamp')\n    fig = go.Figure()\n    fig.add_trace(go.Scatter(x=df['timestamp'], y=df['cloud_coverage'], name=\"Cloud Coverage\", line_color='lightskyblue', opacity=0.7))\n    fig.update_layout(template='plotly_dark',title_text='Cloud', xaxis_rangeslider_visible=True)\n    fig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cloud_graph(weather_train[weather_train['site_id'] == 0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pressure_graph(df):\n    df = df.sort_values('timestamp')\n    fig = go.Figure()\n    fig.add_trace(go.Scatter(x=df['timestamp'], y=df['sea_level_pressure'], name=\"Sea Level Pressure\", line_color='royalblue', opacity=0.7))\n    fig.update_layout(template='plotly_dark',title_text='Sea Level Pressure', xaxis_rangeslider_visible=True)\n    fig.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pressure_graph(weather_train[weather_train['site_id'] == 0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def total_plot(siteid):\n    building_pie_chart(building_metadata[building_metadata['site_id']==siteid],str(siteid))\n    wind_direction_plot(weather_train[weather_train['site_id'] == siteid], str(siteid))\n    temperature_graph(weather_train[weather_train['site_id'] == siteid])\n    cloud_graph(weather_train[weather_train['site_id'] == siteid])\n    pressure_graph(weather_train[weather_train['site_id'] == siteid])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"total_plot(3)","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}