{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9994,"databundleVersionId":752467,"sourceType":"competition"}],"dockerImageVersionId":29840,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# for data manipulation\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# pandas options\npd.set_option('display.max_columns', 50)\npd.set_option('display.float_format', lambda x: '%.3f' % x)\npd.set_option('mode.use_inf_as_na', True)\npd.options.mode.chained_assignment = None\n\n# for date manipulation\nfrom datetime import datetime\n\n# for visualization: matplotlib\nfrom matplotlib import pyplot as plt\nfrom IPython.core.pylabtools import figsize\n%matplotlib inline\n# to display visuals in the notebook\n\n# for visualization: seaborn\nimport seaborn as sns\nsns.set_context(font_scale=2)\n\n# for feature engineering: itertools\nfrom itertools import combinations\n\n# for data preprocessing\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import MinMaxScaler\n\n# for building the model and calculate RMSE\nimport lightgbm as lgb\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom math import sqrt\n\n# for hyperparamter optimization and the cross-validation search\nfrom hyperopt import hp, tpe\nfrom hyperopt.fmin import fmin\nfrom sklearn.model_selection import KFold\n# from sklearn.model_selection import cross_val_score\n# from sklearn.model_selection import TimeSeriesSplit\n\n# for model explainability\nimport shap\n\n# to cleanup memory usage\nimport gc\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.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-19T00:44:24.285903Z","iopub.execute_input":"2024-01-19T00:44:24.286288Z","iopub.status.idle":"2024-01-19T00:44:24.314337Z","shell.execute_reply.started":"2024-01-19T00:44:24.286213Z","shell.execute_reply":"2024-01-19T00:44:24.312859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load train  data\nbuilding = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/building_metadata.csv\")\nweather_train = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_train.csv\")\ntrain = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/train.csv\")\n\n# drop floor_count\nbuilding.drop(columns=[\"floor_count\"], inplace=True)\n\n# convert timestamp column of weather_train and train\ntrain[\"timestamp\"] = pd.to_datetime(train[\"timestamp\"],\n                                   format='%Y-%m-%d %H:%M:%S')\nweather_train[\"timestamp\"] = pd.to_datetime(weather_train[\"timestamp\"],\n                                            format='%Y-%m-%d %H:%M:%S')","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-01-19T00:44:24.316388Z","iopub.execute_input":"2024-01-19T00:44:24.316686Z","iopub.status.idle":"2024-01-19T00:44:36.688924Z","shell.execute_reply.started":"2024-01-19T00:44:24.316636Z","shell.execute_reply":"2024-01-19T00:44:36.687400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Function to reduce the DF size\n# Original code from https://www.kaggle.com/gemartin/load-data-reduce-memory-usage by @gemartin\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))","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:44:36.690180Z","iopub.execute_input":"2024-01-19T00:44:36.690422Z","iopub.status.idle":"2024-01-19T00:44:36.705730Z","shell.execute_reply.started":"2024-01-19T00:44:36.690389Z","shell.execute_reply":"2024-01-19T00:44:36.704856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_memory_usage(building)\nreduce_memory_usage(weather_train)\nreduce_memory_usage(train)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:44:36.706973Z","iopub.execute_input":"2024-01-19T00:44:36.707191Z","iopub.status.idle":"2024-01-19T00:44:37.068377Z","shell.execute_reply.started":"2024-01-19T00:44:36.707151Z","shell.execute_reply":"2024-01-19T00:44:37.067441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add building age column\ncurrent_year = datetime.now().year\nbuilding['building_age'] = current_year - building['year_built']\nbuilding.drop(columns=['year_built'], inplace=True)\n\n# since NA values only present in building age fillna can be used\nbuilding.fillna(round(building.building_age.mean(),0),\n                inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.072943Z","iopub.execute_input":"2024-01-19T00:44:37.074414Z","iopub.status.idle":"2024-01-19T00:44:37.088564Z","shell.execute_reply.started":"2024-01-19T00:44:37.074339Z","shell.execute_reply":"2024-01-19T00:44:37.086880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if any NA values left\nbuilding.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.092768Z","iopub.execute_input":"2024-01-19T00:44:37.093130Z","iopub.status.idle":"2024-01-19T00:44:37.108606Z","shell.execute_reply.started":"2024-01-19T00:44:37.093055Z","shell.execute_reply":"2024-01-19T00:44:37.107458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create label encoder object and transform the column\nle = LabelEncoder()\nle_primary_use = le.fit_transform(building.primary_use)\n\n# add label encoded column to dataframe\nbuilding['primary_use'] = le_primary_use\n\ndel le, le_primary_use\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.110012Z","iopub.execute_input":"2024-01-19T00:44:37.110281Z","iopub.status.idle":"2024-01-19T00:44:37.190119Z","shell.execute_reply.started":"2024-01-19T00:44:37.110239Z","shell.execute_reply":"2024-01-19T00:44:37.189464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add month, day of week, day of month and hour\nweather_train['month'] = weather_train['timestamp'].dt.month.astype(np.int8)\nweather_train['day_of_week'] = weather_train['timestamp'].dt.dayofweek.astype(np.int8)\nweather_train['day_of_month']= weather_train['timestamp'].dt.day.astype(np.int8)\nweather_train['hour'] = weather_train['timestamp'].dt.hour\n\n# add is weekend column\nweather_train['is_weekend'] = weather_train.day_of_week.apply(lambda x: 1 if x>=5 else 0)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.191428Z","iopub.execute_input":"2024-01-19T00:44:37.191851Z","iopub.status.idle":"2024-01-19T00:44:37.265763Z","shell.execute_reply.started":"2024-01-19T00:44:37.191811Z","shell.execute_reply":"2024-01-19T00:44:37.265034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_season(month):\n    if (month <= 2) | (month == 12):\n        return 0\n    # as winter\n    elif month <= 5:\n        return 1\n    # as spring\n    elif month <= 8:\n        return 2\n    # as summer\n    elif month <= 11:\n        return 3\n    # as fall","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.266781Z","iopub.execute_input":"2024-01-19T00:44:37.267098Z","iopub.status.idle":"2024-01-19T00:44:37.271149Z","shell.execute_reply.started":"2024-01-19T00:44:37.267064Z","shell.execute_reply":"2024-01-19T00:44:37.270649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_train['season'] = weather_train.month.apply(convert_season)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.272066Z","iopub.execute_input":"2024-01-19T00:44:37.272339Z","iopub.status.idle":"2024-01-19T00:44:37.341285Z","shell.execute_reply.started":"2024-01-19T00:44:37.272265Z","shell.execute_reply":"2024-01-19T00:44:37.340329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reset Index for Update**","metadata":{}},{"cell_type":"code","source":"weather_train = weather_train.set_index(\n    ['site_id','day_of_month','month'])","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.342607Z","iopub.execute_input":"2024-01-19T00:44:37.342896Z","iopub.status.idle":"2024-01-19T00:44:37.361171Z","shell.execute_reply.started":"2024-01-19T00:44:37.342843Z","shell.execute_reply":"2024-01-19T00:44:37.360156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Air temperature**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\nair_temperature_filler = pd.DataFrame(weather_train\n                                      .groupby(['site_id','day_of_month','month'])\n                                      ['air_temperature'].mean(),\n                                      columns=[\"air_temperature\"])\nair_temperature_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.362395Z","iopub.execute_input":"2024-01-19T00:44:37.362813Z","iopub.status.idle":"2024-01-19T00:44:37.385771Z","shell.execute_reply.started":"2024-01-19T00:44:37.362768Z","shell.execute_reply":"2024-01-19T00:44:37.384963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataframe of air_temperatures to fill\ntemporary_df = pd.DataFrame({'air_temperature' : weather_train.air_temperature})\n\n# update NA air_temperature values\ntemporary_df.update(air_temperature_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"air_temperature\"] = temporary_df[\"air_temperature\"]\n\ndel temporary_df, air_temperature_filler\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.386923Z","iopub.execute_input":"2024-01-19T00:44:37.387223Z","iopub.status.idle":"2024-01-19T00:44:37.785217Z","shell.execute_reply.started":"2024-01-19T00:44:37.387168Z","shell.execute_reply":"2024-01-19T00:44:37.784466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cloud Coverage**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\ncloud_coverage_filler = pd.DataFrame(weather_train\n                                     .groupby(['site_id','day_of_month','month'])\n                                     ['cloud_coverage'].mean(),\n                                     columns = ['cloud_coverage'])\ncloud_coverage_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.786601Z","iopub.execute_input":"2024-01-19T00:44:37.787056Z","iopub.status.idle":"2024-01-19T00:44:37.808765Z","shell.execute_reply.started":"2024-01-19T00:44:37.787011Z","shell.execute_reply":"2024-01-19T00:44:37.807927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"round(cloud_coverage_filler.cloud_coverage.mean(),0)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.809865Z","iopub.execute_input":"2024-01-19T00:44:37.810117Z","iopub.status.idle":"2024-01-19T00:44:37.816433Z","shell.execute_reply.started":"2024-01-19T00:44:37.810056Z","shell.execute_reply":"2024-01-19T00:44:37.815637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cloud_coverage_filler.fillna(round(cloud_coverage_filler.cloud_coverage.mean(),0), \n                             inplace=True)\n\n# create dataframe of cloud_coverages to fill\ntemporary_df = pd.DataFrame({'cloud_coverage' : weather_train.cloud_coverage})\n\n# update NA cloud_coverage values\ntemporary_df.update(cloud_coverage_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"cloud_coverage\"] = temporary_df[\"cloud_coverage\"]\n\ndel temporary_df, cloud_coverage_filler\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:37.817540Z","iopub.execute_input":"2024-01-19T00:44:37.817797Z","iopub.status.idle":"2024-01-19T00:44:38.050441Z","shell.execute_reply.started":"2024-01-19T00:44:37.817731Z","shell.execute_reply":"2024-01-19T00:44:38.049593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dew Temperature**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\ndew_temperature_filler = pd.DataFrame(weather_train\n                                      .groupby(['site_id','day_of_month','month'])\n                                      ['dew_temperature'].mean(),\n                                      columns=[\"dew_temperature\"])\ndew_temperature_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.051732Z","iopub.execute_input":"2024-01-19T00:44:38.052017Z","iopub.status.idle":"2024-01-19T00:44:38.073445Z","shell.execute_reply.started":"2024-01-19T00:44:38.051962Z","shell.execute_reply":"2024-01-19T00:44:38.072407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataframe of dew_temperatures to fill\ntemporary_df = pd.DataFrame({'dew_temperature' : weather_train.dew_temperature})\n\n# update NA dew_temperature values\ntemporary_df.update(dew_temperature_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"dew_temperature\"] = temporary_df[\"dew_temperature\"]\n\ndel temporary_df, dew_temperature_filler\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.074549Z","iopub.execute_input":"2024-01-19T00:44:38.074881Z","iopub.status.idle":"2024-01-19T00:44:38.305547Z","shell.execute_reply.started":"2024-01-19T00:44:38.074846Z","shell.execute_reply":"2024-01-19T00:44:38.304609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Precip Depth 1 Hour**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\nprecip_depth_filler = pd.DataFrame(weather_train\n                                   .groupby(['site_id','day_of_month','month'])\n                                   ['precip_depth_1_hr'].mean(),\n                                   columns=['precip_depth_1_hr'])\nprecip_depth_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.307026Z","iopub.execute_input":"2024-01-19T00:44:38.307336Z","iopub.status.idle":"2024-01-19T00:44:38.328540Z","shell.execute_reply.started":"2024-01-19T00:44:38.307264Z","shell.execute_reply":"2024-01-19T00:44:38.327519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"round(precip_depth_filler['precip_depth_1_hr'].mean(),0)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.329739Z","iopub.execute_input":"2024-01-19T00:44:38.330001Z","iopub.status.idle":"2024-01-19T00:44:38.338086Z","shell.execute_reply.started":"2024-01-19T00:44:38.329947Z","shell.execute_reply":"2024-01-19T00:44:38.337036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"precip_depth_filler.fillna(round(precip_depth_filler['precip_depth_1_hr'].mean(),0)\n                           , inplace=True)\n\n# create dataframe of precip_depth_1_hr to fill\ntemporary_df = pd.DataFrame({'precip_depth_1_hr' : weather_train.precip_depth_1_hr})\n\n# update NA precip_depth_1_hr values\ntemporary_df.update(precip_depth_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"precip_depth_1_hr\"] = temporary_df[\"precip_depth_1_hr\"]\n\ndel precip_depth_filler, temporary_df\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.339714Z","iopub.execute_input":"2024-01-19T00:44:38.340060Z","iopub.status.idle":"2024-01-19T00:44:38.579560Z","shell.execute_reply.started":"2024-01-19T00:44:38.339998Z","shell.execute_reply":"2024-01-19T00:44:38.578829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Sea Level Pressure**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\nsea_level_filler = pd.DataFrame(weather_train\n                                .groupby(['site_id','day_of_month','month'])\n                                ['sea_level_pressure'].mean(),\n                                columns=['sea_level_pressure'])\nsea_level_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.580540Z","iopub.execute_input":"2024-01-19T00:44:38.580756Z","iopub.status.idle":"2024-01-19T00:44:38.604104Z","shell.execute_reply.started":"2024-01-19T00:44:38.580721Z","shell.execute_reply":"2024-01-19T00:44:38.603277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_sea_level_pressure = round(\n    sea_level_filler\n    ['sea_level_pressure']\n    .astype(float)\n    .mean(),2)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.605402Z","iopub.execute_input":"2024-01-19T00:44:38.605649Z","iopub.status.idle":"2024-01-19T00:44:38.611283Z","shell.execute_reply.started":"2024-01-19T00:44:38.605603Z","shell.execute_reply":"2024-01-19T00:44:38.610209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sea_level_filler.fillna(mean_sea_level_pressure, inplace=True)\n\n# create dataframe of sea_level_pressure to fill\ntemporary_df = pd.DataFrame({'sea_level_pressure' : weather_train.sea_level_pressure})\n\n# update NA sea_level_pressure values\ntemporary_df.update(sea_level_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"sea_level_pressure\"] = temporary_df[\"sea_level_pressure\"]\n\ndel sea_level_filler, temporary_df\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.612787Z","iopub.execute_input":"2024-01-19T00:44:38.613074Z","iopub.status.idle":"2024-01-19T00:44:38.850952Z","shell.execute_reply.started":"2024-01-19T00:44:38.613016Z","shell.execute_reply":"2024-01-19T00:44:38.850234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Wind Direction**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\nwind_direction_filler = pd.DataFrame(weather_train\n                                     .groupby(['site_id','day_of_month','month'])\n                                     ['wind_direction'].mean(),\n                                     columns=['wind_direction'])\nwind_direction_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.852020Z","iopub.execute_input":"2024-01-19T00:44:38.852376Z","iopub.status.idle":"2024-01-19T00:44:38.874593Z","shell.execute_reply.started":"2024-01-19T00:44:38.852340Z","shell.execute_reply":"2024-01-19T00:44:38.873534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataframe of wind_direction to fill\ntemporary_df = pd.DataFrame({'wind_direction' : weather_train.wind_direction})\n\n# update NA wind_direction values\ntemporary_df.update(wind_direction_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"wind_direction\"] = temporary_df[\"wind_direction\"]\n\ndel temporary_df, wind_direction_filler\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:38.875571Z","iopub.execute_input":"2024-01-19T00:44:38.875788Z","iopub.status.idle":"2024-01-19T00:44:39.101304Z","shell.execute_reply.started":"2024-01-19T00:44:38.875753Z","shell.execute_reply":"2024-01-19T00:44:39.100587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Wind Speed**","metadata":{}},{"cell_type":"code","source":"# create dataframe of daily means per site id\nwind_speed_filler = pd.DataFrame(weather_train\n                                 .groupby(['site_id','day_of_month','month'])\n                                 ['wind_speed'].mean(),\n                                 columns=['wind_speed'])\nwind_speed_filler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.102440Z","iopub.execute_input":"2024-01-19T00:44:39.102658Z","iopub.status.idle":"2024-01-19T00:44:39.123904Z","shell.execute_reply.started":"2024-01-19T00:44:39.102622Z","shell.execute_reply":"2024-01-19T00:44:39.123122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataframe of wind_speed to fill\ntemporary_df = pd.DataFrame({'wind_speed' : weather_train.wind_speed})\n\n# update NA wind_speed values\ntemporary_df.update(wind_speed_filler, overwrite=False)\n\n# update in the weather train dataset\nweather_train[\"wind_speed\"] = temporary_df[\"wind_speed\"]\n\ndel temporary_df, wind_speed_filler\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.124927Z","iopub.execute_input":"2024-01-19T00:44:39.125145Z","iopub.status.idle":"2024-01-19T00:44:39.356134Z","shell.execute_reply.started":"2024-01-19T00:44:39.125104Z","shell.execute_reply":"2024-01-19T00:44:39.354872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if NA values left\nweather_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.358400Z","iopub.execute_input":"2024-01-19T00:44:39.358804Z","iopub.status.idle":"2024-01-19T00:44:39.376653Z","shell.execute_reply.started":"2024-01-19T00:44:39.358732Z","shell.execute_reply":"2024-01-19T00:44:39.375744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reset indexes to transfrom weather dataframe to original form**","metadata":{}},{"cell_type":"code","source":"weather_train = weather_train.reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.378015Z","iopub.execute_input":"2024-01-19T00:44:39.378340Z","iopub.status.idle":"2024-01-19T00:44:39.392588Z","shell.execute_reply.started":"2024-01-19T00:44:39.378269Z","shell.execute_reply":"2024-01-19T00:44:39.391740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.393918Z","iopub.execute_input":"2024-01-19T00:44:39.394268Z","iopub.status.idle":"2024-01-19T00:44:39.417696Z","shell.execute_reply.started":"2024-01-19T00:44:39.394190Z","shell.execute_reply":"2024-01-19T00:44:39.416809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_direction(degrees):\n    if degrees <= 90:\n        return 0\n    # as norteast direction\n    elif degrees <= 180:\n        return 1\n    # as southeast direction\n    elif degrees <= 270:\n        return 2\n    # as southwest direction\n    elif degrees <= 360:\n        return 3\n    # as northwest direction","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.424788Z","iopub.execute_input":"2024-01-19T00:44:39.425346Z","iopub.status.idle":"2024-01-19T00:44:39.431486Z","shell.execute_reply.started":"2024-01-19T00:44:39.425268Z","shell.execute_reply":"2024-01-19T00:44:39.430467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_train['wind_compass_direction'] = weather_train.wind_direction.apply(convert_direction)\nweather_train.drop(columns=['wind_direction'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.433103Z","iopub.execute_input":"2024-01-19T00:44:39.433437Z","iopub.status.idle":"2024-01-19T00:44:39.521591Z","shell.execute_reply.started":"2024-01-19T00:44:39.433375Z","shell.execute_reply":"2024-01-19T00:44:39.520775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create list of weather variables\nweather_variables = [\"air_temperature\", \"cloud_coverage\", \"dew_temperature\",\n                    \"precip_depth_1_hr\", \"sea_level_pressure\", \"wind_speed\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.522627Z","iopub.execute_input":"2024-01-19T00:44:39.522865Z","iopub.status.idle":"2024-01-19T00:44:39.526401Z","shell.execute_reply.started":"2024-01-19T00:44:39.522828Z","shell.execute_reply":"2024-01-19T00:44:39.525363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, j in combinations(weather_variables, 2):\n    train[\"mean\" + i + \"_\" + j] = (weather_train[i] + weather_train[j]) / 2","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:39.527893Z","iopub.execute_input":"2024-01-19T00:44:39.528132Z","iopub.status.idle":"2024-01-19T00:44:44.857051Z","shell.execute_reply.started":"2024-01-19T00:44:39.528089Z","shell.execute_reply":"2024-01-19T00:44:44.855679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# merge dataframes on train dataframe\ntrain = train.merge(building, on = \"building_id\", how = \"left\")\ntrain = train.merge(weather_train, on = [\"site_id\", \"timestamp\"], how='left')","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:44.858588Z","iopub.execute_input":"2024-01-19T00:44:44.858901Z","iopub.status.idle":"2024-01-19T00:44:59.531787Z","shell.execute_reply.started":"2024-01-19T00:44:44.858847Z","shell.execute_reply":"2024-01-19T00:44:59.530258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del building\ndel weather_train\ndel weather_variables\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:44:59.533162Z","iopub.execute_input":"2024-01-19T00:44:59.533458Z","iopub.status.idle":"2024-01-19T00:44:59.611905Z","shell.execute_reply.started":"2024-01-19T00:44:59.533410Z","shell.execute_reply":"2024-01-19T00:44:59.610739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of unique columns in the train dataset:\", train.shape[1])","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:59.614312Z","iopub.execute_input":"2024-01-19T00:44:59.614748Z","iopub.status.idle":"2024-01-19T00:44:59.622796Z","shell.execute_reply.started":"2024-01-19T00:44:59.614688Z","shell.execute_reply":"2024-01-19T00:44:59.621713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Lets check again for the null values one more time**","metadata":{}},{"cell_type":"code","source":"train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:44:59.624041Z","iopub.execute_input":"2024-01-19T00:44:59.624808Z","iopub.status.idle":"2024-01-19T00:45:03.842576Z","shell.execute_reply.started":"2024-01-19T00:44:59.624702Z","shell.execute_reply":"2024-01-19T00:45:03.841699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlations_transformed = pd.DataFrame(train.corr())\ncorrelations_transformed = pd.DataFrame(correlations_transformed[\"meter_reading\"]).reset_index()\n\n# format, and display sorted correlations_transformed\ncorrelations_transformed.columns = [\"Feature\", \"Correlation with meter_reading\"]\ncorr_df = (correlations_transformed[correlations_transformed[\"Feature\"] != \"meter_reading\"]\n                .sort_values(by=\"Correlation with meter_reading\", ascending=True))\ncorr_df","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:03.843706Z","iopub.execute_input":"2024-01-19T00:45:03.843966Z","iopub.status.idle":"2024-01-19T00:45:29.143839Z","shell.execute_reply.started":"2024-01-19T00:45:03.843918Z","shell.execute_reply":"2024-01-19T00:45:29.142920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del corr_df, correlations_transformed\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:45:29.145371Z","iopub.execute_input":"2024-01-19T00:45:29.145656Z","iopub.status.idle":"2024-01-19T00:45:29.225547Z","shell.execute_reply.started":"2024-01-19T00:45:29.145611Z","shell.execute_reply":"2024-01-19T00:45:29.224405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add log_meter_reading column to the dataframe\ntrain['log_meter_reading'] = np.log1p(train.meter_reading)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:29.227044Z","iopub.execute_input":"2024-01-19T00:45:29.227353Z","iopub.status.idle":"2024-01-19T00:45:29.828020Z","shell.execute_reply.started":"2024-01-19T00:45:29.227281Z","shell.execute_reply":"2024-01-19T00:45:29.826688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlations_transformed = pd.DataFrame(train.corr())\ncorrelations_transformed = pd.DataFrame(correlations_transformed[\"log_meter_reading\"]).reset_index()\n\n# format, and display sorted correlations_transformed\ncorrelations_transformed.columns = [\"Feature\", \"Correlation with log_meter_reading\"]\ncorr_df = (correlations_transformed[correlations_transformed[\"Feature\"] != \"log_meter_reading\"]\n                .sort_values(by=\"Correlation with log_meter_reading\", ascending=True))\ncorr_df","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:29.829502Z","iopub.execute_input":"2024-01-19T00:45:29.829755Z","iopub.status.idle":"2024-01-19T00:45:56.121167Z","shell.execute_reply.started":"2024-01-19T00:45:29.829711Z","shell.execute_reply":"2024-01-19T00:45:56.119770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_feature_list = (corr_df[\n    (corr_df[\"Correlation with log_meter_reading\"] >= 0.004) | \n    (corr_df[\"Correlation with log_meter_reading\"] <= -0.004)][\"Feature\"].\n                        to_list())","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:56.122786Z","iopub.execute_input":"2024-01-19T00:45:56.123125Z","iopub.status.idle":"2024-01-19T00:45:56.132578Z","shell.execute_reply.started":"2024-01-19T00:45:56.123052Z","shell.execute_reply":"2024-01-19T00:45:56.131458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#original_feature_set = ['building_age', 'le_primary_use', 'cloud_coverage', \n#                        'is_weekend','wind_speed', 'day_of_week',\n#                        'wind_compass_direction', 'sea_level_pressure', 'air_temperature',\n#                        'day_of_month', 'dew_temperature', 'hour', \n#                        'month', 'meter', 'building_id', \n#                        'site_id', 'square_feet']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-19T00:45:56.133693Z","iopub.execute_input":"2024-01-19T00:45:56.133938Z","iopub.status.idle":"2024-01-19T00:45:56.145104Z","shell.execute_reply.started":"2024-01-19T00:45:56.133888Z","shell.execute_reply":"2024-01-19T00:45:56.143679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we included meter_reading in the initial feature set\n# which is not a feature\n# replace meter_reading with precip_depth_1_hr\nfor n, i in enumerate(initial_feature_list):\n    if i == \"meter_reading\":\n        initial_feature_list[n] = \"precip_depth_1_hr\"\ninitial_feature_list","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:45:56.146451Z","iopub.execute_input":"2024-01-19T00:45:56.146713Z","iopub.status.idle":"2024-01-19T00:45:56.161191Z","shell.execute_reply.started":"2024-01-19T00:45:56.146672Z","shell.execute_reply":"2024-01-19T00:45:56.159862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del corr_df, correlations_transformed\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:45:56.162761Z","iopub.execute_input":"2024-01-19T00:45:56.163121Z","iopub.status.idle":"2024-01-19T00:45:56.249638Z","shell.execute_reply.started":"2024-01-19T00:45:56.163046Z","shell.execute_reply":"2024-01-19T00:45:56.248694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[initial_feature_list]\ny = train['log_meter_reading']","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:56.250889Z","iopub.execute_input":"2024-01-19T00:45:56.251200Z","iopub.status.idle":"2024-01-19T00:45:57.734392Z","shell.execute_reply.started":"2024-01-19T00:45:56.251149Z","shell.execute_reply":"2024-01-19T00:45:57.733006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Pearson coefficient based feature selection leaves us with {} features.\".\n      format(len(X.columns)))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:57.735708Z","iopub.execute_input":"2024-01-19T00:45:57.735944Z","iopub.status.idle":"2024-01-19T00:45:57.742175Z","shell.execute_reply.started":"2024-01-19T00:45:57.735901Z","shell.execute_reply":"2024-01-19T00:45:57.741599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-19T00:45:57.743110Z","iopub.execute_input":"2024-01-19T00:45:57.743496Z","iopub.status.idle":"2024-01-19T00:45:57.947995Z","shell.execute_reply.started":"2024-01-19T00:45:57.743452Z","shell.execute_reply":"2024-01-19T00:45:57.946966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <a id='3-2-2'> 3.2.2. Imputation of Train Dataset </a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"code","source":"X = X.fillna(method='ffill', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:45:57.949221Z","iopub.execute_input":"2024-01-19T00:45:57.949549Z","iopub.status.idle":"2024-01-19T00:46:03.319276Z","shell.execute_reply.started":"2024-01-19T00:45:57.949488Z","shell.execute_reply":"2024-01-19T00:46:03.318432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:46:03.320461Z","iopub.execute_input":"2024-01-19T00:46:03.320790Z","iopub.status.idle":"2024-01-19T00:46:04.624967Z","shell.execute_reply.started":"2024-01-19T00:46:03.320724Z","shell.execute_reply":"2024-01-19T00:46:04.623666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <a id='3-3'> 3.3. Split train set into training and validation set </a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"code","source":"# split train and validation set into 75 and 25 percent sequentially\nX_train = X[:int(3 * X.shape[0] / 4)]\nX_valid = X[int(3 * X.shape[0] / 4):]\n\ny_train = y[:int(3 * y.shape[0] / 4)]\ny_valid = y[int(3 * y.shape[0] / 4):]","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:46:04.626918Z","iopub.execute_input":"2024-01-19T00:46:04.627372Z","iopub.status.idle":"2024-01-19T00:46:04.651310Z","shell.execute_reply.started":"2024-01-19T00:46:04.627290Z","shell.execute_reply":"2024-01-19T00:46:04.649771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make sure train and validation sets shape align\nprint(\"Shape of the training set is: \", X_train.shape)\nprint(\"Shape of the validation set is: \", X_valid.shape)\nprint(\"Shape of the training labels are: \", y_train.shape)\nprint(\"Shape of the validation labels are: \", y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T00:46:04.652790Z","iopub.execute_input":"2024-01-19T00:46:04.653098Z","iopub.status.idle":"2024-01-19T00:46:04.664032Z","shell.execute_reply.started":"2024-01-19T00:46:04.653050Z","shell.execute_reply":"2024-01-19T00:46:04.662445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <a id='4'> 4. Several Machine Learning Models </a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"markdown","source":"**Function to evaluate model's performance on train and validation set with RMSLE**","metadata":{}},{"cell_type":"code","source":"def fit_evaluate_model(model, X_train, y_train, X_valid, y_valid):\n    model.fit(X_train, y_train)\n    y_predicted = model.predict(X_valid)\n    return sqrt(mean_squared_error(y_valid, y_predicted))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <a id='4-1'> 4.1. Linear Regression </a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"code","source":"%%time\n# create model apply fit_evaluate_model\nlinear_regression = LinearRegression()\nlr_rmse = fit_evaluate_model(linear_regression, X_train, y_train, X_valid, y_valid)\nprint(\"RMSE of the linear regression model is:\", lr_rmse)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del linear_regression\ndel lr_rmse\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <a id='4-2'> 4.2. Random Forest Regressor</a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"code","source":"%%time\n# create model apply fit_evaluate_model\nrandom_forest= RandomForestRegressor()\nrf_rmse = fit_evaluate_model(random_forest, X_train, y_train, X_valid, y_valid)\nprint(\"RMSE of the random forest model is:\", rf_rmse)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del random_forest\ndel rf_rmse\ngc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <a id='4-3'> 4.3. Light GBM </a>\n<a href='#top'> Back to Top </a>","metadata":{}},{"cell_type":"code","source":"%%time\n# create model apply fit_evaluate_model\nlgbm_regressor = lgb.LGBMRegressor(random_state=42)\nlgbm_rmse = fit_evaluate_model(lgbm_regressor, X_train, y_train, X_valid, y_valid)\nprint(\"RMSE of the light gbm regressor is:\", lgbm_rmse)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del lgbm_regressor\ndel lgbm_rmse\ngc.collect()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Saving the files to use in future**","metadata":{}},{"cell_type":"code","source":"X.to_csv('X.csv', index=False)\ny.to_csv('y.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}