{"cells":[{"metadata":{"colab_type":"text","id":"Zl4knKePm598"},"cell_type":"markdown","source":"# Great Energy predictor using LSTM model"},{"metadata":{"colab_type":"text","id":"k86S1IHSm59_"},"cell_type":"markdown","source":"### Abstract:\n\nWe are using a dataset related to ASHRAE – Great Energy Predictor III (How much energy will a building consume?). The goal is to develop models from ASHRAE’s 2016 data in order to better understand metered building energy usage in the following areas: chilled water, electric, hot water, and steam meters. The data comes from over 1,000 buildings over a one-year timeframe."},{"metadata":{"colab":{},"colab_type":"code","id":"mNcK7agPm5-E","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport json\nimport os\nimport pandas as pd\nimport seaborn as sns\nimport statsmodels.formula.api as smf\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn import metrics\nfrom sklearn.model_selection  import train_test_split\nimport numpy as np\nimport gc\nfrom scipy.stats import norm # for scientific Computing\nfrom scipy import stats, integrate\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"twS0olxUm5-M"},"cell_type":"markdown","source":"## 1.Data understanding"},{"metadata":{"colab":{},"colab_type":"code","id":"Ho0Hi-rWm5-O","trusted":true},"cell_type":"code","source":"#load the dataset\nASHRAE_train = pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')\nASHRAE_test = pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')\nweather_train = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv')\nweather_test = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv')\nbuilding_meta = pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":178},"colab_type":"code","id":"aLspJ4CMm5-U","outputId":"06138c07-2397-4697-e2f8-72e791f91e9e","trusted":true},"cell_type":"code","source":"ASHRAE_train.info()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":267},"colab_type":"code","id":"Qg8vc1iSm5-b","outputId":"47493803-a9f1-40de-8c83-f8b5d1f72cd7","trusted":true},"cell_type":"code","source":"weather_train.info()","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"fPWFaLjfm5-g"},"cell_type":"markdown","source":"Reduce some data types to save some memory usage."},{"metadata":{"colab":{},"colab_type":"code","id":"7n0MT1qcm5-i","trusted":true},"cell_type":"code","source":"## Function to reduce the DF size\ndef reduce_memory_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","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":513},"colab_type":"code","id":"0ihmK9yPm5-m","outputId":"1b0e414b-4bbd-4d24-af71-1250a41fb363","scrolled":true,"trusted":true},"cell_type":"code","source":"reduce_memory_usage(building_meta)\nreduce_memory_usage(weather_train)\nreduce_memory_usage(ASHRAE_train)\n\nreduce_memory_usage(weather_test)\nreduce_memory_usage(ASHRAE_test)","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":300},"colab_type":"code","id":"Zh5VQB41m5-r","outputId":"24d49758-ada5-440b-feb9-92e598e7f0e7","trusted":true},"cell_type":"code","source":"ASHRAE_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":300},"colab_type":"code","id":"JOK4x_tGm5-y","outputId":"417c838b-d26f-42c0-c366-90545a78c4a1","trusted":true},"cell_type":"code","source":"building_meta.describe()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":142},"colab_type":"code","id":"Vgu0uauPm5-2","outputId":"60e711b1-dba1-4afa-f68c-3f0b5403e371","trusted":true},"cell_type":"code","source":"print('number of categories used on \"primary_use\": ',building_meta['primary_use'].nunique())\nprint('list of categories used on \"primary_use\" : ')\nprint(building_meta['primary_use'].unique())","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":160},"colab_type":"code","id":"fon-XTuJm5-6","outputId":"e3c32d00-54bc-4418-9e08-227935395050","scrolled":true,"trusted":true},"cell_type":"code","source":"#print train data columns\nprint('Columns of the building dataset is', building_meta.columns)\nprint('Columns of the weather_train dataset is', weather_train.columns)\nprint('Columns of the train dataset is', ASHRAE_train.columns)","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"A75uXDo0m5_A"},"cell_type":"markdown","source":"### Messing data in 'building_meta"},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":142},"colab_type":"code","id":"wUnneYiCm5_B","outputId":"a0277b48-9d8b-4b3e-a2c3-a5206597b46c","trusted":true},"cell_type":"code","source":"building_meta.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":451},"colab_type":"code","id":"q2kjIh-Em5_G","outputId":"78a89c33-d7b5-4480-878a-a9d9b86e0f94","trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(15,7))\nsns.heatmap(building_meta.isnull(), yticklabels=False, cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":160},"colab_type":"code","id":"YkA-OYSSm5_K","outputId":"f55a35a3-09b1-44a9-dfaf-9ddf00f11bfb","trusted":true},"cell_type":"code","source":"print(\"Percentage of missing values in the building_meta dataset\")\nbuilding_meta.isna().sum()/len(building_meta)*100","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":124},"colab_type":"code","id":"o60qR9UHm5_N","outputId":"88ea19b9-d029-477f-c484-ceb7ff7ec21c","trusted":true},"cell_type":"code","source":"print(\"Percentage of missing values in the train dataset\")\nASHRAE_train.isna().sum()/len(ASHRAE_train)*100","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":214},"colab_type":"code","id":"VM7r8sfJm5_R","outputId":"c880cc30-d9f4-476e-cee5-3a339b0a8a7c","trusted":true},"cell_type":"code","source":"print(\"Percentage of missing values in the weather_train dataset\")\nweather_train.isna().sum()/len(weather_train)*100","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"j_3Kg1L3m5_U"},"cell_type":"markdown","source":"## 2.Data preparation"},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"colab_type":"code","id":"UL8Nv235m5_X","outputId":"dfe592b4-9c37-410c-ae16-de0c7139b534","trusted":true},"cell_type":"code","source":"#merging tables\nBuildingTrain = building_meta.merge(ASHRAE_train, left_on='building_id', right_on='building_id' , how='left')\nBuildingTest = building_meta.merge(ASHRAE_test, left_on='building_id', right_on='building_id' , how='left')\nBuildingTrain.shape, BuildingTest.shape","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"colab_type":"code","id":"FJvbi43Pm5_b","outputId":"a55ebcba-be3a-4146-db0e-a7f1a09c43c2","trusted":true},"cell_type":"code","source":"BuildingTrain.head()","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"hNI2C7A2fk-1","trusted":true},"cell_type":"code","source":"del ASHRAE_test\ndel ASHRAE_train\ndel building_meta\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"colab_type":"code","id":"ljtPIIFmm5_f","outputId":"e06ef00c-e1c5-453b-d7ab-3b1c07b08267","trusted":true},"cell_type":"code","source":"BTW_train=BuildingTrain.merge(weather_train,left_on=['site_id','timestamp'],right_on=['site_id','timestamp'],how='left')\nBTW_test = BuildingTest.merge(weather_test,left_on=['site_id','timestamp'],right_on=['site_id','timestamp'],how='left')\nBTW_train.shape","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"NYMMNDtOTvxr","trusted":true},"cell_type":"code","source":"del BuildingTest\ndel BuildingTrain\ndel weather_test\ndel weather_train\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":106},"colab_type":"code","id":"q7q_9y0Em5_i","outputId":"945ad00d-ddc9-467c-d59b-dc864e2a9357","trusted":true},"cell_type":"code","source":"BTW_train.columns","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":313},"colab_type":"code","id":"ieX8vOxPm5_l","outputId":"5806a2a5-1582-420b-c3f7-464274102470","trusted":true},"cell_type":"code","source":"BTW_train.head()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":339},"colab_type":"code","id":"mjE3Zwnkm5_q","outputId":"afc9c966-81c3-4a7d-87e6-e03d903e2aaa","trusted":true},"cell_type":"code","source":"print(\"Percentage of missing values in the BTW_train dataset\")\nBTW_train.isna().sum()/len(BTW_train)*100","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":564},"colab_type":"code","id":"SJBsSrnhm5_t","outputId":"24cbe215-9a61-4bd8-c31f-7f3888874525","trusted":true},"cell_type":"code","source":"BTW_train.hist('sea_level_pressure')\nBTW_train[['sea_level_pressure']].describe()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":564},"colab_type":"code","id":"DRgPmVxCm5_y","outputId":"f9590428-cb65-4145-fe36-d4437981cab1","trusted":true},"cell_type":"code","source":"BTW_train.hist('cloud_coverage')\nBTW_train[['cloud_coverage']].describe()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":564},"colab_type":"code","id":"l27Sx6nUm5_2","outputId":"0824284d-f603-4662-f2f4-5983514d9443","trusted":true},"cell_type":"code","source":"BTW_train.hist('precip_depth_1_hr')\nBTW_train[['precip_depth_1_hr']].describe()","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"JatBKzhFm5_5","trusted":true},"cell_type":"code","source":"def plot_dist(df, column):\n    plt.figure(figsize=(18,12))\n    ax = sns.distplot(df[column].dropna())\n    ax.set_title(column+\" Distribution\", fontsize=16)\n    plt.xlabel(column, fontsize=12)\n    #plt.ylabel(\"distribution\", fontsize=12)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":735},"colab_type":"code","id":"EdSGwl-pm5_-","outputId":"25ee71e0-a807-4180-a349-7ce5b1985c3a","trusted":true},"cell_type":"code","source":"BTW_train[['wind_speed']].describe()\nplot_dist(BTW_train,'wind_speed')","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":735},"colab_type":"code","id":"vrls5u7Xm6AA","outputId":"70b10590-3fda-4169-fee5-b320b996ecc9","trusted":true},"cell_type":"code","source":"BTW_train[['air_temperature']].describe()\nplot_dist(BTW_train,'air_temperature')","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":735},"colab_type":"code","id":"X0WrQ-jXm6AF","outputId":"f757f73b-379a-4839-f570-2bd33232817b","trusted":true},"cell_type":"code","source":"plot_dist(BTW_train, \"sea_level_pressure\")","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"1G2f8yTvm6AI"},"cell_type":"markdown","source":"meter - The meter id code Read as {0: electricity, 1: chilledwater, 2: steam, 3: hotwater}. Not every building has all meter types.\n\nmeter_reading - The target variable. Energy consumption in kWh (or equivalent). Note that this is real data with measurement error, which we expect will impose a baseline level of modeling error. UPDATE: as discussed here, the site 0 electric meter readings are in kBTU."},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":308},"colab_type":"code","id":"LIcKf6Pgm6AJ","outputId":"fb7bf566-7fd0-4808-d669-eb41b2ac7f62","trusted":true},"cell_type":"code","source":"sns.boxplot(x = 'meter', y = 'meter_reading', data = BTW_train)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"jlIkXn2um6AL","trusted":true},"cell_type":"code","source":"def outlier_function(df, col_name):\n    ''' this function detects first and third quartile and interquartile range for a given column of a dataframe\n    then calculates upper and lower limits to determine outliers conservatively\n    returns the number of lower and uper limit and number of outliers respectively\n    '''\n    first_quartile = np.percentile(\n        np.array(df[col_name].tolist()), 25)\n    third_quartile = np.percentile(\n        np.array(df[col_name].tolist()), 75)\n    IQR = third_quartile - first_quartile\n                      \n    upper_limit = third_quartile+(3*IQR)\n    lower_limit = first_quartile-(3*IQR)\n    outlier_count = 0\n                      \n    for value in df[col_name].tolist():\n        if (value < lower_limit) | (value > upper_limit):\n            outlier_count +=1\n    return lower_limit, upper_limit, outlier_count","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"colab_type":"code","id":"xEZhB36am6AO","outputId":"2987ee6e-54de-4b3d-8d1e-6d44c551a2e3","trusted":true},"cell_type":"code","source":"print(\"{} percent of {} are outliers.\"\n      .format((\n              (100 * outlier_function(BTW_train, 'meter_reading')[2])\n               / len(BTW_train['meter_reading'])),\n              'meter_reading'))","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":641},"colab_type":"code","id":"94EXqmWhm6AR","outputId":"4a0c9137-28d2-45c4-c5fd-da3e95d82ed7","trusted":true},"cell_type":"code","source":"# Distribution of the meter reading in meters without zeros\nplt.figure(figsize=(12,10))\n\n#list of different meters\nmeters = sorted(BTW_train['meter'].unique().tolist()) # [0, 1, 2, 3]\n\n# plot meter_reading distribution for each meter\nfor meter_type in meters:\n    subset = BTW_train[BTW_train['meter'] == meter_type]\n    sns.kdeplot(np.log1p(subset[\"meter_reading\"]), \n                label=meter_type, linewidth=2)\n\n# set title, legends and labels\nplt.ylabel(\"Density\")\nplt.xlabel(\"Meter_reading\")\nplt.legend(['electricity', 'chilled water', 'steam', 'hot water'])\nplt.title(\"Density of Logartihm(Meter Reading + 1) Among Different Meters\", size=14)","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":313},"colab_type":"code","id":"TEvBBZctm6AU","outputId":"af94bec9-3065-4f00-f2bb-91b665159445","trusted":true},"cell_type":"code","source":"BTW_train.head()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":106},"colab_type":"code","id":"s8AcNT46m6AW","outputId":"11344890-0bab-42ce-eb71-aa7d36731b49","trusted":true},"cell_type":"code","source":"BTW_train.columns","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":699},"colab_type":"code","id":"eI5ZfkVDm6AY","outputId":"888e2994-c8ba-4c7b-88c1-9cd4e8987e40","trusted":true},"cell_type":"code","source":"corrmat=BTW_train.corr()\nfig,ax=plt.subplots(figsize=(12,10))\nsns.heatmap(corrmat,annot=True,annot_kws={'size': 12})","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"x2YNMEdom6Ab"},"cell_type":"markdown","source":"### Dropping columns and filling null value"},{"metadata":{"colab":{},"colab_type":"code","id":"hTeJExAKm6Ab","trusted":true},"cell_type":"code","source":"BTW_train = BTW_train.drop(columns=['year_built', 'floor_count', 'wind_direction', 'dew_temperature'])\nBTW_test = BTW_test.drop(columns=['year_built', 'floor_count','wind_direction', 'dew_temperature'])","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"AxmLlVnhfQhU","trusted":true},"cell_type":"code","source":"BTW_train ['timestamp'] =  pd.to_datetime(BTW_train['timestamp'])\nBTW_test ['timestamp'] =  pd.to_datetime(BTW_test['timestamp'])\nBTW_train['Month']=pd.DatetimeIndex(BTW_train['timestamp']).month\nBTW_test['Month']=pd.DatetimeIndex(BTW_test['timestamp']).month\nBTW_train['Day']=pd.DatetimeIndex(BTW_train['timestamp']).day\nBTW_test['Day']=pd.DatetimeIndex(BTW_test['timestamp']).day","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"LHeo-OjBm6Ae","trusted":true},"cell_type":"code","source":"BTW_train= BTW_train.groupby(['meter',BTW_train['building_id'],'primary_use',BTW_train['Month'], BTW_train['Day']]).agg({'meter_reading':'sum', 'air_temperature': 'mean', 'wind_speed': 'mean', 'precip_depth_1_hr': 'mean', 'cloud_coverage': 'mean', 'square_feet': 'mean'})\nBTW_test_1= BTW_test.groupby(['row_id','meter',BTW_test['building_id'],'primary_use',BTW_test['Month'], BTW_test['Day']]).agg({ 'air_temperature': 'mean', 'wind_speed': 'mean', 'precip_depth_1_hr': 'mean', 'cloud_coverage': 'mean', 'square_feet': 'mean'})","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":142},"colab_type":"code","id":"Zti9t9WWm6Ag","outputId":"89a2b7b2-b5a5-40b0-86e4-84a87eab3748","trusted":true},"cell_type":"code","source":"BTW_train.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"2klhpNO-m6Ai","trusted":true},"cell_type":"code","source":"BTW_train = BTW_train.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":300},"colab_type":"code","id":"6hG1YvWsm6Al","outputId":"94f4b43d-8c5a-47c7-b8bb-e09acf5ad204","trusted":true},"cell_type":"code","source":"BTW_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"HlSzRNhum6Aq"},"cell_type":"markdown","source":"Change data type to float 32 for filling NA value before transforming them into int for smooth modeling processing"},{"metadata":{"colab":{},"colab_type":"code","id":"py-aNbKqm6Aq","trusted":true},"cell_type":"code","source":"BTW_train['wind_speed'] = BTW_train['wind_speed'].astype('float32')\nBTW_train['air_temperature'] = BTW_train['air_temperature'].astype('float32')\nBTW_train['precip_depth_1_hr'] = BTW_train['precip_depth_1_hr'].astype('float32')\nBTW_train['cloud_coverage'] = BTW_train['cloud_coverage'].astype('float32')\nBTW_test['wind_speed'] = BTW_test['wind_speed'].astype('float32')\nBTW_test['air_temperature'] = BTW_test['air_temperature'].astype('float32')\nBTW_test['precip_depth_1_hr'] = BTW_test['precip_depth_1_hr'].astype('float32')\nBTW_test['cloud_coverage'] = BTW_test['cloud_coverage'].astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":231},"colab_type":"code","id":"BFjCmFysm6Au","outputId":"4806eb6d-7d21-4a0f-c940-bcae4f7ff1dd","trusted":true},"cell_type":"code","source":"BTW_train['precip_depth_1_hr'].fillna(method='ffill', inplace = True)\nBTW_train['cloud_coverage'].fillna(method='bfill', inplace = True)\n\nBTW_train['wind_speed'].fillna(BTW_train['wind_speed'].mean(), inplace=True)\nBTW_train['air_temperature'].fillna(BTW_train['air_temperature'].mean(), inplace=True)\n\nBTW_test['precip_depth_1_hr'].fillna(method='ffill', inplace = True)\nBTW_test['cloud_coverage'].fillna(method='bfill', inplace = True)\nBTW_test['precip_depth_1_hr'].fillna(BTW_test['precip_depth_1_hr'].mean(), inplace=True)\nBTW_test['cloud_coverage'].fillna(BTW_test['cloud_coverage'].mean(), inplace=True)\n\nBTW_test['wind_speed'].fillna(BTW_test['wind_speed'].mean(), inplace=True)\nBTW_test['air_temperature'].fillna(BTW_test['air_temperature'].mean(), inplace=True)\nBTW_train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":231},"colab_type":"code","id":"lbPKUqKom6Aw","outputId":"de76fd62-ee10-4999-c673-0fef29300e15","trusted":true},"cell_type":"code","source":"BTW_train.shape,BTW_train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":513},"colab_type":"code","id":"B8FkDrpZm6A0","outputId":"f93daf5f-5c98-4c1c-d295-3bc257f53f7d","scrolled":false,"trusted":true},"cell_type":"code","source":"#visualization of meter reading for a specific building_id\n# by Day\nbuilding_id = 213\nplt.figure(figsize=(14, 8))\nax = sns.lineplot(x=\"Day\", y=\"meter_reading\", hue=\"meter\", data=BTW_train[BTW_train['building_id'] == building_id])\nplt.title('Meter readings from building_id {}'.format(building_id))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":513},"colab_type":"code","id":"0pfKxMaMm6A2","outputId":"ea946d9f-7642-46cf-bd8f-d023650c80e2","trusted":true},"cell_type":"code","source":"#visualization of meter reading for a specific building_id\n# by Month\nbuilding_id = 213\nplt.figure(figsize=(14, 8))\nax = sns.lineplot(x=\"Month\", y=\"meter_reading\", hue=\"meter\", data=BTW_train[BTW_train['building_id'] == building_id])\nplt.title('Meter readings from building_id {}'.format(building_id))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"rpqmP-mKm6A5"},"cell_type":"markdown","source":"## 3.Data modeling "},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":106},"colab_type":"code","id":"LsQXjX_cm6A5","outputId":"dc65757c-21a4-462e-de0e-d54a9c91b227","trusted":true},"cell_type":"code","source":"BTW_train.primary_use.unique()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"colab_type":"code","id":"xauo8DYXm6A8","outputId":"5fe072ea-b065-44b6-a1b0-89127d99b838","trusted":true},"cell_type":"code","source":"BTW_train.head()","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"ONh2snbUm6BC","trusted":true},"cell_type":"code","source":"BTW_encoded = BTW_train[:]\nBTW_test_encoded = BTW_test[:]","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"N1VEiHqcm6BE","trusted":true},"cell_type":"code","source":"# label encoding \nfrom sklearn.preprocessing import LabelEncoder\n\nle = LabelEncoder()\nBTW_encoded[\"primary_use\"] = le.fit_transform(BTW_encoded[\"primary_use\"])\nBTW_test_encoded[\"primary_use\"] = le.fit_transform(BTW_test_encoded[\"primary_use\"])","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":86},"colab_type":"code","id":"u2UvtI8AAh6n","outputId":"9033b2cb-c45b-45a5-86e0-ad3c0b98f485","trusted":true},"cell_type":"code","source":"BTW_encoded.columns","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"X6CB1rH9AjN8","trusted":true},"cell_type":"code","source":"X = BTW_encoded[['meter', 'building_id', 'primary_use', 'Month', 'Day','air_temperature', 'wind_speed', 'precip_depth_1_hr', 'cloud_coverage',\n       'square_feet']]\ny = BTW_encoded['meter_reading']","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"C8qS_A0em6BG","trusted":true},"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(X,y, test_size = 0.2, random_state= 45)","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"xZ5ySGJam6Bb"},"cell_type":"markdown","source":"# Deep learning model"},{"metadata":{"colab":{},"colab_type":"code","id":"tZi2ONSxm6Bc","trusted":true},"cell_type":"code","source":"from sklearn import preprocessing\nfrom keras import backend as K\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.layers import Dense, LSTM, GRU, Dropout, BatchNormalization\nfrom keras.models import Sequential\nfrom keras.optimizers import RMSprop,Adam\nfrom keras import regularizers","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"8iehtyglLUTf","trusted":true},"cell_type":"code","source":"def root_mean_squared_error(y_true, y_pred):\n  return K.sqrt(K.mean(K.square(y_pred - y_true)))","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"S9zQAEtZt7Dl","trusted":true},"cell_type":"code","source":"def make_model(input_dim=10,metrics=root_mean_squared_error,loss='mse', optimizer=\"rmsprop\",drop_rate=0.5):\n\n  model = Sequential()\n  model.add(LSTM(128,return_sequences=True, input_shape=(None,input_dim)))\n  model.add(Dropout(drop_rate))\n  model.add(BatchNormalization())\n  model.add(LSTM(128,return_sequences=False))\n  model.add(BatchNormalization())\n  model.add(Dropout(drop_rate))\n  model.add(Dense(1))\n  model.compile(optimizer=optimizer, loss=loss, metrics=[metrics])\n  \n  return model","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"kJIckM1Mwb7e","trusted":true},"cell_type":"code","source":"def run_model(model,x_train,y_train,epochs=50,batch_size=500,verbose=1,validation_data=(x_val,y_val),callbacks =None):\n  x_train = x_train.values[:]\n  x_train= x_train.reshape((x_train.shape[0],1,x_train.shape[-1]))\n  y_train = np.log1p(y_train)\n  if validation_data != None:\n    x_val = validation_data[0].values[:]\n    x_val = x_val.reshape((x_val.shape[0],1,x_val.shape[-1]))\n    y_val = np.log1p(validation_data[-1])\n      \n  return model.fit(x_train,y_train,epochs=epochs,batch_size=batch_size,verbose=verbose,validation_data=(x_val,y_val),callbacks=callbacks)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"67tQb--jEEaJ","trusted":true},"cell_type":"code","source":"#best_model_file = \"my_model.h5\"\n#mc = ModelCheckpoint(best_model_file, monitor='val_loss', mode='auto',verbose=True, save_best_only=True)\nes = EarlyStopping(monitor='val_root_mean_squared_error', min_delta=0.0001, patience=5, verbose=True, mode='auto')","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"Zk7yLDOCxmrc","trusted":true},"cell_type":"code","source":"model = make_model(input_dim=x_train.shape[-1],drop_rate=0.2)","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":399},"colab_type":"code","id":"IIakASH2m6Bh","outputId":"0d3f86c5-e837-4a89-f013-7e8bb2379210","scrolled":true,"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":905},"colab_type":"code","id":"QztQsaz3m6Bm","outputId":"4fe1f2d8-bd8f-44cf-d1ca-47eb7a0b8a2f","trusted":true},"cell_type":"code","source":"history = run_model(model,x_train,y_train,epochs=30,batch_size=500,verbose=1,validation_data=(x_val,y_val), callbacks =[es]) # callbacks =[mc, es]","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":54},"colab_type":"code","id":"N_DBQl6vHcHV","outputId":"b87752f1-da3f-4bb1-bfd0-9375dbc97a9b","trusted":true},"cell_type":"code","source":"loss = history.history\nloss.keys()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":281},"colab_type":"code","id":"lKsGiY5WYFzw","outputId":"e5e14249-eef7-48f9-f532-186e7bd9e6a4","trusted":true},"cell_type":"code","source":"#rmse loss\nrmse_loss_train = loss['root_mean_squared_error']\nrmse_loss_val = loss['val_root_mean_squared_error']\nepochs_stops = es.stopped_epoch +1 # epochs number from early stopping\nepochs = range(1,epochs_stops + 1)  #len(loss_train)\nplt.figure(figsize=(12,6))\nplt.plot(epochs,rmse_loss_train,'r', label='RMSE train loss')\nplt.plot(epochs,rmse_loss_val,'b',label='RMSE val loss')\nplt.title(' root mean square error loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"o59AD_aHmSd7"},"cell_type":"markdown","source":"## Submitting"},{"metadata":{"colab":{},"colab_type":"code","id":"p9k6KTU_2SYW","trusted":true},"cell_type":"code","source":"# submit = pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"iCi_cCQ8Kt2T","trusted":true},"cell_type":"code","source":"# x_test = BTW_test[['meter', 'building_id', 'primary_use', 'Month', 'Day','air_temperature', 'wind_speed', 'precip_depth_1_hr', 'cloud_coverage',#\n#        'square_feet']]\n# x_test = x_test.values[:]\n# x_test = x_test.reshape((x_test.shape[0],1,x_test.shape[-1]))","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"1XzQ0ix1mWq2","trusted":true},"cell_type":"code","source":"# prediction = history.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"6wfxGfYAmpl6","trusted":true},"cell_type":"code","source":"# prediction = np.expm1(prediction)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"8DmNquQvsKAk","trusted":true},"cell_type":"code","source":"# submit['meter_reading'] = prediction","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"EzYVUr79sNGQ","trusted":true},"cell_type":"code","source":"# submit.to_csv('submission.csv', index=False,float_format='%.4f')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"},"colab":{"name":"Great Energy Predictor.ipynb","provenance":[],"collapsed_sections":[]},"accelerator":"TPU"},"nbformat":4,"nbformat_minor":1}