{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport gc\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-09T21:04:07.012457Z","iopub.execute_input":"2022-01-09T21:04:07.012965Z","iopub.status.idle":"2022-01-09T21:04:08.666222Z","shell.execute_reply.started":"2022-01-09T21:04:07.012857Z","shell.execute_reply":"2022-01-09T21:04:08.665363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# to make this notebook's output stable across runs\nnp.random.seed(42)\n\n# To plot pretty figures\n%matplotlib inline\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nmpl.rc('axes', labelsize=14)\nmpl.rc('xtick', labelsize=12)\nmpl.rc('ytick', labelsize=12)\n\n\n# Ignore useless warnings (see SciPy issue #5998)\nimport warnings\nwarnings.filterwarnings(action=\"ignore\", message=\"^internal gelsd\")","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:04:09.902885Z","iopub.execute_input":"2022-01-09T21:04:09.903544Z","iopub.status.idle":"2022-01-09T21:04:09.91262Z","shell.execute_reply.started":"2022-01-09T21:04:09.903505Z","shell.execute_reply":"2022-01-09T21:04:09.911656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2    \n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)    \n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:04:26.614621Z","iopub.execute_input":"2022-01-09T21:04:26.615521Z","iopub.status.idle":"2022-01-09T21:04:26.629586Z","shell.execute_reply.started":"2022-01-09T21:04:26.615482Z","shell.execute_reply":"2022-01-09T21:04:26.628911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building = pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\")\nweather = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\")\nbuilding_meta = pd.read_csv(\"../input/ashrae-energy-prediction/building_metadata.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:05:25.03185Z","iopub.execute_input":"2022-01-09T21:05:25.032199Z","iopub.status.idle":"2022-01-09T21:05:41.874062Z","shell.execute_reply.started":"2022-01-09T21:05:25.032166Z","shell.execute_reply":"2022-01-09T21:05:41.873075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather[\"datetime\"] = pd.to_datetime(weather[\"timestamp\"])\nweather[\"day\"] = weather[\"datetime\"].dt.day\nweather[\"week\"] = weather[\"datetime\"].dt.week\nweather[\"month\"] = weather[\"datetime\"].dt.month\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:05:47.137194Z","iopub.execute_input":"2022-01-09T21:05:47.137555Z","iopub.status.idle":"2022-01-09T21:05:47.302822Z","shell.execute_reply.started":"2022-01-09T21:05:47.137513Z","shell.execute_reply":"2022-01-09T21:05:47.301754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = building.merge(right = building_meta, on = 'building_id', how = 'left')\ntrain_all = temp.merge(right = weather, on = ['site_id','timestamp'], how = 'left')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:06:43.704972Z","iopub.execute_input":"2022-01-09T21:06:43.705532Z","iopub.status.idle":"2022-01-09T21:06:56.47412Z","shell.execute_reply.started":"2022-01-09T21:06:43.705494Z","shell.execute_reply":"2022-01-09T21:06:56.473409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all = reduce_mem_usage(train_all)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:08:37.929812Z","iopub.execute_input":"2022-01-09T21:08:37.930345Z","iopub.status.idle":"2022-01-09T21:08:42.569042Z","shell.execute_reply.started":"2022-01-09T21:08:37.930308Z","shell.execute_reply":"2022-01-09T21:08:42.568093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:09:11.525738Z","iopub.execute_input":"2022-01-09T21:09:11.526095Z","iopub.status.idle":"2022-01-09T21:09:11.666064Z","shell.execute_reply.started":"2022-01-09T21:09:11.526062Z","shell.execute_reply":"2022-01-09T21:09:11.665123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather = weather.set_index(['site_id','day','month'])","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:13.356654Z","iopub.execute_input":"2022-01-09T20:46:13.356868Z","iopub.status.idle":"2022-01-09T20:46:13.376669Z","shell.execute_reply.started":"2022-01-09T20:46:13.356842Z","shell.execute_reply":"2022-01-09T20:46:13.375954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_air_temperature = pd.DataFrame(weather.groupby(['site_id','day','month'])['air_temperature'].mean(),columns=[\"air_temperature\"])\nweather.update(fill_air_temperature,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:13.377809Z","iopub.execute_input":"2022-01-09T20:46:13.378162Z","iopub.status.idle":"2022-01-09T20:46:14.059325Z","shell.execute_reply.started":"2022-01-09T20:46:13.378133Z","shell.execute_reply":"2022-01-09T20:46:14.058394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_cloud_coverage = weather.groupby(['site_id','day','month'])['cloud_coverage'].mean()\nfill_cloud_coverage = pd.DataFrame(fill_cloud_coverage.fillna(method='ffill'),columns=[\"cloud_coverage\"])\n\nweather.update(fill_cloud_coverage,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:14.060759Z","iopub.execute_input":"2022-01-09T20:46:14.061667Z","iopub.status.idle":"2022-01-09T20:46:14.456344Z","shell.execute_reply.started":"2022-01-09T20:46:14.06162Z","shell.execute_reply":"2022-01-09T20:46:14.455393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_dew_temperature = pd.DataFrame(weather.groupby(['site_id','day','month'])['dew_temperature'].mean(),columns=[\"dew_temperature\"])\nweather.update(fill_dew_temperature,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:14.457701Z","iopub.execute_input":"2022-01-09T20:46:14.458004Z","iopub.status.idle":"2022-01-09T20:46:14.960807Z","shell.execute_reply.started":"2022-01-09T20:46:14.457965Z","shell.execute_reply":"2022-01-09T20:46:14.959824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_sea_level_pressure = weather.groupby(['site_id','day','month'])['sea_level_pressure'].mean()\nfill_sea_level_pressure = pd.DataFrame(fill_sea_level_pressure.fillna(method='ffill'),columns=['sea_level_pressure'])\n\nweather.update(fill_sea_level_pressure,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:14.962405Z","iopub.execute_input":"2022-01-09T20:46:14.962706Z","iopub.status.idle":"2022-01-09T20:46:15.380369Z","shell.execute_reply.started":"2022-01-09T20:46:14.962666Z","shell.execute_reply":"2022-01-09T20:46:15.379223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_wind_direction =  pd.DataFrame(weather.groupby(['site_id','day','month'])['wind_direction'].mean(),columns=['wind_direction'])\nweather.update(fill_wind_direction,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:15.382055Z","iopub.execute_input":"2022-01-09T20:46:15.382316Z","iopub.status.idle":"2022-01-09T20:46:15.911516Z","shell.execute_reply.started":"2022-01-09T20:46:15.382284Z","shell.execute_reply":"2022-01-09T20:46:15.910668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_wind_speed =  pd.DataFrame(weather.groupby(['site_id','day','month'])['wind_speed'].mean(),columns=['wind_speed'])\nweather.update(fill_wind_speed,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:15.913077Z","iopub.execute_input":"2022-01-09T20:46:15.913307Z","iopub.status.idle":"2022-01-09T20:46:16.310464Z","shell.execute_reply.started":"2022-01-09T20:46:15.91328Z","shell.execute_reply":"2022-01-09T20:46:16.309474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_precip_depth = weather.groupby(['site_id','day','month'])['precip_depth_1_hr'].mean()\nfill_precip_depth = pd.DataFrame(fill_precip_depth.fillna(method='ffill'),columns=['precip_depth_1_hr'])\n\nweather.update(fill_precip_depth,overwrite=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:16.312324Z","iopub.execute_input":"2022-01-09T20:46:16.312973Z","iopub.status.idle":"2022-01-09T20:46:16.699434Z","shell.execute_reply.started":"2022-01-09T20:46:16.312922Z","shell.execute_reply":"2022-01-09T20:46:16.698308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather.update(fill_precip_depth,overwrite=False)\nweather = weather.reset_index()\nweather= weather.drop(['datetime','day','week','month'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:46:16.700766Z","iopub.execute_input":"2022-01-09T20:46:16.701123Z","iopub.status.idle":"2022-01-09T20:46:17.230933Z","shell.execute_reply.started":"2022-01-09T20:46:16.701082Z","shell.execute_reply":"2022-01-09T20:46:17.230042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all.boxplot(column = ['meter_reading'])","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:10:36.869257Z","iopub.execute_input":"2022-01-09T21:10:36.869604Z","iopub.status.idle":"2022-01-09T21:11:46.4622Z","shell.execute_reply.started":"2022-01-09T21:10:36.869569Z","shell.execute_reply":"2022-01-09T21:11:46.461081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all[train_all['meter_reading'] > 50000].shape\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:11:46.464719Z","iopub.execute_input":"2022-01-09T21:11:46.465054Z","iopub.status.idle":"2022-01-09T21:11:48.127637Z","shell.execute_reply.started":"2022-01-09T21:11:46.465002Z","shell.execute_reply":"2022-01-09T21:11:48.126666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filtering outliers\ntrain_all = train_all[train_all['meter_reading'] < 50000]","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:11:48.12928Z","iopub.execute_input":"2022-01-09T21:11:48.129892Z","iopub.status.idle":"2022-01-09T21:11:52.173278Z","shell.execute_reply.started":"2022-01-09T21:11:48.129843Z","shell.execute_reply":"2022-01-09T21:11:52.172422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking electricity meter reading as 0\ntrain_all[((train_all['meter_reading'] == 0) & (train_all['meter'] == 0))].shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:11:52.175434Z","iopub.execute_input":"2022-01-09T21:11:52.175663Z","iopub.status.idle":"2022-01-09T21:11:52.346984Z","shell.execute_reply.started":"2022-01-09T21:11:52.175636Z","shell.execute_reply":"2022-01-09T21:11:52.346063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filtering out all those rows\ntrain_all = train_all[~((train_all['meter_reading'] == 0) & (train_all['meter'] == 0))]","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:11:52.348356Z","iopub.execute_input":"2022-01-09T21:11:52.348577Z","iopub.status.idle":"2022-01-09T21:11:54.671594Z","shell.execute_reply.started":"2022-01-09T21:11:52.348551Z","shell.execute_reply":"2022-01-09T21:11:54.6706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all[\"timestamp\"] = pd.to_datetime(train_all[\"timestamp\"],format=\"%Y-%m-%d %H:%M:%S\")\ntrain_all['hour'] = train_all['timestamp'].dt.hour\ntrain_all['dayofweek'] = train_all['timestamp'].dt.dayofweek\ntrain_all['month'] = train_all['timestamp'].dt.month\ntrain_all['dayofmonth'] = train_all['timestamp'].dt.day","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:12:03.357616Z","iopub.execute_input":"2022-01-09T21:12:03.357969Z","iopub.status.idle":"2022-01-09T21:12:14.815272Z","shell.execute_reply.started":"2022-01-09T21:12:03.357932Z","shell.execute_reply":"2022-01-09T21:12:14.814057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\ntemp_df = (building_meta['primary_use'].value_counts(normalize = True)*100).sort_values()\ntemp_df.plot(kind = 'barh')\nplt.title('Percentage share of each building type', pad = 20, fontsize = 15)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:00.694157Z","iopub.execute_input":"2022-01-09T20:48:00.694442Z","iopub.status.idle":"2022-01-09T20:48:01.02815Z","shell.execute_reply.started":"2022-01-09T20:48:00.694407Z","shell.execute_reply":"2022-01-09T20:48:01.027265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nbuilding_meta.groupby('primary_use')['square_feet'].median().sort_values().plot(kind = 'barh')\nplt.title('Median area of each building type', pad = 20, fontsize = 15)\nplt.ylabel(' ')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:01.029673Z","iopub.execute_input":"2022-01-09T20:48:01.029893Z","iopub.status.idle":"2022-01-09T20:48:01.357274Z","shell.execute_reply.started":"2022-01-09T20:48:01.029866Z","shell.execute_reply":"2022-01-09T20:48:01.356304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\nfor i in range(0,16):\n    build_met=building_meta[building_meta['site_id']==i]\n    sns.histplot(build_met['square_feet'],label=i)\n    i=i+1\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:01.358569Z","iopub.execute_input":"2022-01-09T20:48:01.358815Z","iopub.status.idle":"2022-01-09T20:48:02.642896Z","shell.execute_reply.started":"2022-01-09T20:48:01.358784Z","shell.execute_reply":"2022-01-09T20:48:02.640994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.crosstab(index=building_meta['site_id'],columns=building_meta['primary_use'], dropna=True).plot(kind='bar', stacked=True, figsize=(15,10))\nplt.ylabel('Number of buildings')\nplt.title('Buildings in each site')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:02.643902Z","iopub.execute_input":"2022-01-09T20:48:02.644166Z","iopub.status.idle":"2022-01-09T20:48:03.864327Z","shell.execute_reply.started":"2022-01-09T20:48:02.644138Z","shell.execute_reply":"2022-01-09T20:48:03.863355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nbuilding_meta.groupby('primary_use')['floor_count'].mean().sort_values(ascending = False)[:12].plot(kind = 'bar')\nplt.title('Mean floor count of each building type', pad = 20, fontsize = 15)\nplt.xlabel(' ')\nplt.ylabel('Floor Count')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:03.866099Z","iopub.execute_input":"2022-01-09T20:48:03.866377Z","iopub.status.idle":"2022-01-09T20:48:04.179713Z","shell.execute_reply.started":"2022-01-09T20:48:03.866318Z","shell.execute_reply":"2022-01-09T20:48:04.178579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nbuilding_meta.groupby('site_id')['square_feet'].mean().sort_values(ascending = False).plot(kind = 'bar')\nplt.title('Mean area distribution for buildings in each site', pad = 20, fontsize = 15)\nplt.xlabel('site id')\nplt.ylabel('Area ')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:04.180935Z","iopub.execute_input":"2022-01-09T20:48:04.181191Z","iopub.status.idle":"2022-01-09T20:48:04.630764Z","shell.execute_reply.started":"2022-01-09T20:48:04.181161Z","shell.execute_reply":"2022-01-09T20:48:04.630146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\ntrain_all.groupby('meter')['building_id'].nunique().plot(kind = 'bar')\nplt.title('Meter type distribution', fontsize = 15, pad = 20)\n# plt.xlabel('meter type')\nplt.ylabel('#of buildings')\nplt.xticks(np.arange(4), ('Electricity', 'chilledwater', 'steam', 'hotwater'));","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:04.632142Z","iopub.execute_input":"2022-01-09T20:48:04.632599Z","iopub.status.idle":"2022-01-09T20:48:09.144726Z","shell.execute_reply.started":"2022-01-09T20:48:04.632556Z","shell.execute_reply":"2022-01-09T20:48:09.143922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\ntrain_all.groupby('meter')['meter_reading'].sum().sort_values().plot(kind = 'barh')\nplt.title('Meters Energy Consumption Distribution', fontsize = 15, pad = 20)\nplt.ylabel('')\nplt.xlabel('energy consumption')\nplt.yticks(np.arange(4), ('hotwater', 'Electricity', 'chilledwater', 'steam'));","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:09.14619Z","iopub.execute_input":"2022-01-09T20:48:09.146608Z","iopub.status.idle":"2022-01-09T20:48:09.822829Z","shell.execute_reply.started":"2022-01-09T20:48:09.146571Z","shell.execute_reply":"2022-01-09T20:48:09.821755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all.groupby(['primary_use', 'meter'])['meter_reading'].median().unstack().plot(kind = 'bar', stacked = True, figsize = (12,6))\nplt.title('Median energy consumption by building type and meter type', fontsize = 15, pad = 20)\nplt.legend(labels=['Electricity', 'chilledwater', 'steam', 'hotwater'])","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:12:28.903453Z","iopub.execute_input":"2022-01-09T21:12:28.904295Z","iopub.status.idle":"2022-01-09T21:12:33.123957Z","shell.execute_reply.started":"2022-01-09T21:12:28.904229Z","shell.execute_reply":"2022-01-09T21:12:33.123094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all.groupby(['site_id', 'meter'])['meter_reading'].mean().unstack().plot(kind = 'bar', stacked = True, figsize = (10,6))\nplt.title('Mean energy consumption at a site level by meter types', fontsize = 15, pad = 20)\nplt.legend(labels=['Electricity', 'chilledwater', 'steam', 'hotwater'])","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:12:33.125844Z","iopub.execute_input":"2022-01-09T21:12:33.126095Z","iopub.status.idle":"2022-01-09T21:12:34.785355Z","shell.execute_reply.started":"2022-01-09T21:12:33.126059Z","shell.execute_reply":"2022-01-09T21:12:34.784427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# higher consumption during day\ntrain_all.groupby('hour')['meter_reading'].mean().plot(linestyle='-.').axes.set_ylim(325,500)\nplt.ylabel('meter reading')\nplt.title('Hourly distribution of energy consumption')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:15.425913Z","iopub.execute_input":"2022-01-09T20:48:15.426143Z","iopub.status.idle":"2022-01-09T20:48:16.098895Z","shell.execute_reply.started":"2022-01-09T20:48:15.426116Z","shell.execute_reply":"2022-01-09T20:48:16.097968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# higher consumption during weekdays\ntrain_all.groupby('dayofweek')['meter_reading'].mean().plot(linestyle='-.').axes.set_ylim(325,500)\nplt.ylabel('meter reading')\nplt.title('Weekly distribution of energy consumption')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:16.100547Z","iopub.execute_input":"2022-01-09T20:48:16.100797Z","iopub.status.idle":"2022-01-09T20:48:16.810055Z","shell.execute_reply.started":"2022-01-09T20:48:16.100769Z","shell.execute_reply":"2022-01-09T20:48:16.809128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pleasant months see low consumption as expected\ntrain_all.groupby('month')['meter_reading'].mean().plot(linestyle='-.').axes.set_ylim(325,500)\nplt.ylabel('meter reading')\nplt.title('Month wise distribution of energy consumption')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:16.811255Z","iopub.execute_input":"2022-01-09T20:48:16.811995Z","iopub.status.idle":"2022-01-09T20:48:17.486656Z","shell.execute_reply.started":"2022-01-09T20:48:16.811958Z","shell.execute_reply":"2022-01-09T20:48:17.485685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\ntrain_all.groupby('air_temperature')['meter_reading'].mean().plot(linestyle='-.')\nplt.ylabel('meter reading')\nplt.title('Impact of Air Temperature on Power Consumption')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T20:48:17.488059Z","iopub.execute_input":"2022-01-09T20:48:17.488284Z","iopub.status.idle":"2022-01-09T20:48:18.393684Z","shell.execute_reply.started":"2022-01-09T20:48:17.488256Z","shell.execute_reply":"2022-01-09T20:48:18.393058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\n\nle = preprocessing.LabelEncoder()\nle.fit(train_all['primary_use'])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:02.37689Z","iopub.execute_input":"2022-01-09T21:13:02.377185Z","iopub.status.idle":"2022-01-09T21:13:03.246561Z","shell.execute_reply.started":"2022-01-09T21:13:02.377154Z","shell.execute_reply":"2022-01-09T21:13:03.245625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all['primary_use'] = pd.Series(le.transform(train_all['primary_use']))\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:11.516437Z","iopub.execute_input":"2022-01-09T21:13:11.517154Z","iopub.status.idle":"2022-01-09T21:13:17.976658Z","shell.execute_reply.started":"2022-01-09T21:13:11.517109Z","shell.execute_reply":"2022-01-09T21:13:17.975957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all['square_feet'] = np.log(train_all['square_feet'] + 1)\ntrain_all['meter_reading'] = np.log(train_all['meter_reading'] + 1)\n\ntrain_all.drop(columns=[\"timestamp\",\"year_built\",\"floor_count\"], inplace=True)\ntrain_all.reset_index(drop = True, inplace = True)\n\ntrain_all.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:17.991855Z","iopub.execute_input":"2022-01-09T21:13:17.992698Z","iopub.status.idle":"2022-01-09T21:13:21.590648Z","shell.execute_reply.started":"2022-01-09T21:13:17.992657Z","shell.execute_reply":"2022-01-09T21:13:21.589758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_feature = ['building_id','meter','site_id','primary_use','hour', 'dayofweek', 'month','dayofmonth']","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:22.293142Z","iopub.execute_input":"2022-01-09T21:13:22.293808Z","iopub.status.idle":"2022-01-09T21:13:22.298809Z","shell.execute_reply.started":"2022-01-09T21:13:22.293751Z","shell.execute_reply":"2022-01-09T21:13:22.297934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:33.359924Z","iopub.execute_input":"2022-01-09T21:13:33.360661Z","iopub.status.idle":"2022-01-09T21:13:33.510287Z","shell.execute_reply.started":"2022-01-09T21:13:33.360622Z","shell.execute_reply":"2022-01-09T21:13:33.50957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:35.291173Z","iopub.execute_input":"2022-01-09T21:13:35.291529Z","iopub.status.idle":"2022-01-09T21:13:35.430574Z","shell.execute_reply.started":"2022-01-09T21:13:35.29149Z","shell.execute_reply":"2022-01-09T21:13:35.429656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_test = pd.read_csv(\"../input/ashrae-energy-prediction/weather_test.csv\")\nweather_test.drop(columns=[\"dew_temperature\",\"sea_level_pressure\", \"wind_direction\", \"wind_speed\"], inplace=True)\ntest= pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\")\n\n# merge all\ntemp = test.merge(right = building_meta, on = 'building_id', how = 'left')\ntest = temp.merge(right = weather_test, on = ['site_id','timestamp'], how = 'left')","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:13:41.335096Z","iopub.execute_input":"2022-01-09T21:13:41.335425Z","iopub.status.idle":"2022-01-09T21:14:34.465821Z","shell.execute_reply.started":"2022-01-09T21:13:41.335393Z","shell.execute_reply":"2022-01-09T21:14:34.464885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = reduce_mem_usage(test)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:16:06.056436Z","iopub.execute_input":"2022-01-09T21:16:06.057273Z","iopub.status.idle":"2022-01-09T21:16:09.790123Z","shell.execute_reply.started":"2022-01-09T21:16:06.057233Z","shell.execute_reply":"2022-01-09T21:16:09.78901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:17:12.699623Z","iopub.execute_input":"2022-01-09T21:17:12.700083Z","iopub.status.idle":"2022-01-09T21:17:12.847313Z","shell.execute_reply.started":"2022-01-09T21:17:12.70003Z","shell.execute_reply":"2022-01-09T21:17:12.846562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = np.log1p(train_all[\"meter_reading\"])\ntrain = train_all.drop([\"meter_reading\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:30:55.045117Z","iopub.execute_input":"2022-01-09T21:30:55.045509Z","iopub.status.idle":"2022-01-09T21:30:57.645812Z","shell.execute_reply.started":"2022-01-09T21:30:55.045474Z","shell.execute_reply":"2022-01-09T21:30:57.644611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:38:05.125887Z","iopub.execute_input":"2022-01-09T21:38:05.126226Z","iopub.status.idle":"2022-01-09T21:38:05.278799Z","shell.execute_reply.started":"2022-01-09T21:38:05.126192Z","shell.execute_reply":"2022-01-09T21:38:05.277882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.iloc[:, :-1].values\ny = target.iloc[:, 1].values","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:50:49.371282Z","iopub.execute_input":"2022-01-09T21:50:49.371614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0, random_state=45)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.model_selection import KFold\nfrom sklearn import metrics\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model  import SGDRegressor\nfrom sklearn.metrics import  mean_absolute_error","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:35:24.317723Z","iopub.execute_input":"2022-01-09T21:35:24.318081Z","iopub.status.idle":"2022-01-09T21:35:24.41377Z","shell.execute_reply.started":"2022-01-09T21:35:24.318041Z","shell.execute_reply":"2022-01-09T21:35:24.412822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DecisionTreeRegressor(max_depth=30)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:41:18.419592Z","iopub.execute_input":"2022-01-09T21:41:18.420001Z","iopub.status.idle":"2022-01-09T21:41:18.460549Z","shell.execute_reply.started":"2022-01-09T21:41:18.419957Z","shell.execute_reply":"2022-01-09T21:41:18.459268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:41:57.412609Z","iopub.execute_input":"2022-01-09T21:41:57.412994Z","iopub.status.idle":"2022-01-09T21:41:57.437792Z","shell.execute_reply.started":"2022-01-09T21:41:57.412955Z","shell.execute_reply":"2022-01-09T21:41:57.436708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SGDreg   =  SGDRegressor(random_state = 42)\nSGDreg.fit(X_train, y_train.ravel())\ny_predicted=SGDreg.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T21:42:47.023497Z","iopub.execute_input":"2022-01-09T21:42:47.024577Z","iopub.status.idle":"2022-01-09T21:42:47.072375Z","shell.execute_reply.started":"2022-01-09T21:42:47.024499Z","shell.execute_reply":"2022-01-09T21:42:47.071111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}