{"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":"markdown","source":"# ***CLIMATE CHANGE - PREDICTING  ENERGY CONSUMPTION IN BUILDINGS***","metadata":{"id":"CAjxJRxr1xQb"}},{"cell_type":"markdown","source":"### ***IMPORTING THE NECESSARY MODULES AND PACKAGES***","metadata":{"id":"zCpTcsDoBOTm"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import r2_score\nfrom sklearn.metrics import mean_squared_error\nfrom math import sqrt\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.model_selection import GridSearchCV\n\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor\nimport xgboost as xgb\nfrom sklearn.svm import SVR\nimport lightgbm as lgb\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\npd.set_option('display.max_rows', 500)\npd.set_option(\"display.max_columns\", 500)","metadata":{"id":"jEkUFrBSwa9o","execution":{"iopub.status.busy":"2022-07-18T09:31:31.096357Z","iopub.execute_input":"2022-07-18T09:31:31.096914Z","iopub.status.idle":"2022-07-18T09:31:31.107088Z","shell.execute_reply.started":"2022-07-18T09:31:31.096874Z","shell.execute_reply":"2022-07-18T09:31:31.106419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***LOADING THE DATA***","metadata":{"id":"5Yhhzg0oAbc0"}},{"cell_type":"code","source":"df_train=pd.read_csv(\"../input/widsdatathon2022/train.csv\")\ndf_test=pd.read_csv(\"../input/widsdatathon2022/test.csv\")","metadata":{"id":"BKV6OVz2ys1M","execution":{"iopub.status.busy":"2022-07-18T09:31:31.424265Z","iopub.execute_input":"2022-07-18T09:31:31.424889Z","iopub.status.idle":"2022-07-18T09:31:31.891316Z","shell.execute_reply.started":"2022-07-18T09:31:31.424849Z","shell.execute_reply":"2022-07-18T09:31:31.890585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **DATA PREPROCESSING**","metadata":{"id":"6MluM0ASCwIj"}},{"cell_type":"markdown","source":"***DUPLICATE VALUES***\n\nDropping duplicate values considering the groupby columns identify the same building","metadata":{"id":"NJ044ry-Vxn4"}},{"cell_type":"code","source":"df_train=df_train.drop_duplicates(['Year_Factor', 'State_Factor', 'floor_area', 'year_built', 'facility_type'], keep= 'last') # dropping the duplicate rows","metadata":{"id":"R9bWb-DEEWVa","outputId":"f506ae84-6cb8-4ab3-eee4-a65b9e00c98d","execution":{"iopub.status.busy":"2022-07-18T09:31:31.893107Z","iopub.execute_input":"2022-07-18T09:31:31.893430Z","iopub.status.idle":"2022-07-18T09:31:31.931077Z","shell.execute_reply.started":"2022-07-18T09:31:31.893393Z","shell.execute_reply":"2022-07-18T09:31:31.930258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=df_train.copy(deep=True)","metadata":{"id":"i31B0GxtJqEH","execution":{"iopub.status.busy":"2022-07-18T09:31:32.003811Z","iopub.execute_input":"2022-07-18T09:31:32.004023Z","iopub.status.idle":"2022-07-18T09:31:32.015736Z","shell.execute_reply.started":"2022-07-18T09:31:32.003992Z","shell.execute_reply":"2022-07-18T09:31:32.015077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TRAIN DATA","metadata":{}},{"cell_type":"code","source":"drop_col = ['january_min_temp', 'january_avg_temp', 'january_max_temp',\n       'february_min_temp', 'february_avg_temp', 'february_max_temp',\n       'march_min_temp', 'march_avg_temp', 'march_max_temp', 'april_min_temp',\n       'april_avg_temp', 'april_max_temp', 'may_min_temp', 'may_avg_temp',\n       'may_max_temp', 'june_min_temp', 'june_avg_temp', 'june_max_temp',\n       'july_min_temp', 'july_avg_temp', 'july_max_temp', 'august_min_temp',\n       'august_avg_temp', 'august_max_temp', 'september_min_temp',\n       'september_avg_temp', 'september_max_temp', 'october_min_temp',\n       'october_avg_temp', 'october_max_temp', 'november_min_temp',\n       'november_avg_temp', 'november_max_temp', 'december_min_temp',\n       'december_avg_temp', 'december_max_temp','building_class','precipitation_inches', 'snowfall_inches',\n       'snowdepth_inches','days_below_30F', 'days_below_20F',\n       'days_below_10F', 'days_below_0F', 'days_above_80F', 'days_above_90F',\n       'days_above_100F', 'days_above_110F','direction_max_wind_speed',\n       'direction_peak_wind_speed', 'max_wind_speed', 'days_with_fog','ELEVATION']\n\ntrain = train.drop(drop_col,axis =1)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:32.326990Z","iopub.execute_input":"2022-07-18T09:31:32.327641Z","iopub.status.idle":"2022-07-18T09:31:32.349423Z","shell.execute_reply.started":"2022-07-18T09:31:32.327606Z","shell.execute_reply":"2022-07-18T09:31:32.348665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['energy_star_rating'] = train['energy_star_rating'].replace(0, np.nan)\ntrain['year_built'] = train['year_built'].replace(0, np.nan)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:32.604249Z","iopub.execute_input":"2022-07-18T09:31:32.604864Z","iopub.status.idle":"2022-07-18T09:31:32.611656Z","shell.execute_reply.started":"2022-07-18T09:31:32.604825Z","shell.execute_reply":"2022-07-18T09:31:32.610963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://datahack.analyticsvidhya.com/discussions/janatahack-machine-learning-in-agriculture/21/","metadata":{}},{"cell_type":"code","source":"train['energy_star_rating'] = train['energy_star_rating'].replace(np.nan,-999)\ntrain['year_built'] = train['year_built'].replace(np.nan,-999)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:32.879929Z","iopub.execute_input":"2022-07-18T09:31:32.880438Z","iopub.status.idle":"2022-07-18T09:31:32.888805Z","shell.execute_reply.started":"2022-07-18T09:31:32.880402Z","shell.execute_reply":"2022-07-18T09:31:32.888064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***TEST DATA - PRE PROCESSING***","metadata":{"id":"ZlFVZw_-TvmM"}},{"cell_type":"code","source":"#test=df_test.drop(\"id\", axis=1)\ntest=df_test.copy(deep=True)","metadata":{"id":"Qq7rTHpVUOwN","execution":{"iopub.status.busy":"2022-07-18T09:31:33.159281Z","iopub.execute_input":"2022-07-18T09:31:33.160057Z","iopub.status.idle":"2022-07-18T09:31:33.165058Z","shell.execute_reply.started":"2022-07-18T09:31:33.160006Z","shell.execute_reply":"2022-07-18T09:31:33.164061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_col = ['january_min_temp', 'january_avg_temp', 'january_max_temp',\n       'february_min_temp', 'february_avg_temp', 'february_max_temp',\n       'march_min_temp', 'march_avg_temp', 'march_max_temp', 'april_min_temp',\n       'april_avg_temp', 'april_max_temp', 'may_min_temp', 'may_avg_temp',\n       'may_max_temp', 'june_min_temp', 'june_avg_temp', 'june_max_temp',\n       'july_min_temp', 'july_avg_temp', 'july_max_temp', 'august_min_temp',\n       'august_avg_temp', 'august_max_temp', 'september_min_temp',\n       'september_avg_temp', 'september_max_temp', 'october_min_temp',\n       'october_avg_temp', 'october_max_temp', 'november_min_temp',\n       'november_avg_temp', 'november_max_temp', 'december_min_temp',\n       'december_avg_temp', 'december_max_temp','building_class','precipitation_inches', 'snowfall_inches',\n       'snowdepth_inches','days_below_30F', 'days_below_20F',\n       'days_below_10F', 'days_below_0F', 'days_above_80F', 'days_above_90F',\n       'days_above_100F', 'days_above_110F','direction_max_wind_speed',\n       'direction_peak_wind_speed', 'max_wind_speed', 'days_with_fog','ELEVATION']\n\ntest = test.drop(drop_col,axis ='columns')\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:33.503403Z","iopub.execute_input":"2022-07-18T09:31:33.504208Z","iopub.status.idle":"2022-07-18T09:31:33.524882Z","shell.execute_reply.started":"2022-07-18T09:31:33.504157Z","shell.execute_reply":"2022-07-18T09:31:33.524132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['energy_star_rating'] = test['energy_star_rating'].replace(0, np.nan)\ntest['year_built'] = test['year_built'].replace(0, np.nan)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:33.782359Z","iopub.execute_input":"2022-07-18T09:31:33.782820Z","iopub.status.idle":"2022-07-18T09:31:33.789476Z","shell.execute_reply.started":"2022-07-18T09:31:33.782780Z","shell.execute_reply":"2022-07-18T09:31:33.788730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['energy_star_rating'] = test['energy_star_rating'].replace(np.nan,-999)\ntest['year_built'] = test['year_built'].replace(np.nan,-999)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:34.060617Z","iopub.execute_input":"2022-07-18T09:31:34.061218Z","iopub.status.idle":"2022-07-18T09:31:34.069437Z","shell.execute_reply.started":"2022-07-18T09:31:34.061179Z","shell.execute_reply":"2022-07-18T09:31:34.068619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Combining train and test data to create new lag features\n\ncols1=['State_Factor','facility_type','floor_area','year_built']\ntrain['source'] = 'train'\ntest['source']  = 'test'\ndf = pd.concat([train,test],0,ignore_index=True)\ndf=df.sort_values(by=cols1+['Year_Factor'])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:34.339097Z","iopub.execute_input":"2022-07-18T09:31:34.339330Z","iopub.status.idle":"2022-07-18T09:31:34.396114Z","shell.execute_reply.started":"2022-07-18T09:31:34.339302Z","shell.execute_reply":"2022-07-18T09:31:34.395383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:34.513224Z","iopub.execute_input":"2022-07-18T09:31:34.513926Z","iopub.status.idle":"2022-07-18T09:31:34.528436Z","shell.execute_reply.started":"2022-07-18T09:31:34.513873Z","shell.execute_reply":"2022-07-18T09:31:34.527786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a lag feature for site EUI (1 year)\nfor i in [1] :\n    df[f'site_eui_lag{i}']          =df.groupby(cols1)['site_eui'].shift(i)\n   # df[f'energy_star_rating_lag{i}']=df.groupby(cols1)['energy_star_rating'].shift(i)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:34.691150Z","iopub.execute_input":"2022-07-18T09:31:34.691674Z","iopub.status.idle":"2022-07-18T09:31:34.723533Z","shell.execute_reply.started":"2022-07-18T09:31:34.691638Z","shell.execute_reply":"2022-07-18T09:31:34.722891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One Hot Encoding\none_hot_enc_f = pd.get_dummies(df['facility_type'])\ndf = df.drop('facility_type',axis = 1)\n# Join the encoded df\ndf = df.join(one_hot_enc_f)\nprint(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:34.955696Z","iopub.execute_input":"2022-07-18T09:31:34.956125Z","iopub.status.idle":"2022-07-18T09:31:35.093390Z","shell.execute_reply.started":"2022-07-18T09:31:34.956088Z","shell.execute_reply":"2022-07-18T09:31:35.092716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Deleting the rows of year_factor =1 as site_eui_lag_1 will be null for those rows\ndf=df[~((df['Year_Factor']==1) & (df['source']=='train'))]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:35.163858Z","iopub.execute_input":"2022-07-18T09:31:35.164226Z","iopub.status.idle":"2022-07-18T09:31:35.239350Z","shell.execute_reply.started":"2022-07-18T09:31:35.164171Z","shell.execute_reply":"2022-07-18T09:31:35.238577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping year_factor and state_factor as they dont influence site EUI anymore\ndf = df.drop(['Year_Factor','State_Factor'],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:35.368349Z","iopub.execute_input":"2022-07-18T09:31:35.368604Z","iopub.status.idle":"2022-07-18T09:31:35.380433Z","shell.execute_reply.started":"2022-07-18T09:31:35.368574Z","shell.execute_reply":"2022-07-18T09:31:35.379736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:35.620123Z","iopub.execute_input":"2022-07-18T09:31:35.620640Z","iopub.status.idle":"2022-07-18T09:31:35.657443Z","shell.execute_reply.started":"2022-07-18T09:31:35.620603Z","shell.execute_reply":"2022-07-18T09:31:35.656815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df.loc[df['source']=='train']\ntrain=train[train.site_eui_lag1.notnull()]","metadata":{"id":"JzimuC_Cke2g","execution":{"iopub.status.busy":"2022-07-18T09:31:36.064349Z","iopub.execute_input":"2022-07-18T09:31:36.064955Z","iopub.status.idle":"2022-07-18T09:31:36.123168Z","shell.execute_reply.started":"2022-07-18T09:31:36.064918Z","shell.execute_reply":"2022-07-18T09:31:36.122451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = df.loc[df['source']=='test']","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:36.527492Z","iopub.execute_input":"2022-07-18T09:31:36.527745Z","iopub.status.idle":"2022-07-18T09:31:36.548566Z","shell.execute_reply.started":"2022-07-18T09:31:36.527716Z","shell.execute_reply":"2022-07-18T09:31:36.547833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop('source',axis = 1)\ntest = test.drop('source',axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:36.763145Z","iopub.execute_input":"2022-07-18T09:31:36.763411Z","iopub.status.idle":"2022-07-18T09:31:36.773601Z","shell.execute_reply.started":"2022-07-18T09:31:36.763382Z","shell.execute_reply":"2022-07-18T09:31:36.772884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop(['site_eui','id'], axis=1) # assigning features to x, y\ny = train['site_eui']\n\nprint(x.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:37.022483Z","iopub.execute_input":"2022-07-18T09:31:37.022731Z","iopub.status.idle":"2022-07-18T09:31:37.032981Z","shell.execute_reply.started":"2022-07-18T09:31:37.022702Z","shell.execute_reply":"2022-07-18T09:31:37.032220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42) # train-val split\nprint(x_train.shape, y_train.shape)\nprint(x_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:37.250240Z","iopub.execute_input":"2022-07-18T09:31:37.250884Z","iopub.status.idle":"2022-07-18T09:31:37.282249Z","shell.execute_reply.started":"2022-07-18T09:31:37.250848Z","shell.execute_reply":"2022-07-18T09:31:37.281435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#xg = xgb.XGBRegressor(objective = 'reg:squarederror', n_estimators=1000, learning_rate=0.025, max_depth=12, min_child_weight=7,gamma=2,subsample=1,colsample_bytree=0.8, seed=42)\nxg = xgb.XGBRegressor(objective = 'reg:squarederror', n_estimators=1000, learning_rate=0.025, max_depth=12, min_child_weight=7,gamma=2,subsample=1,colsample_bytree=0.8, seed=42)\nxg.fit(x_train, y_train)\n\nprint('xgboost Regressor Train Score :', xg.score(x_train, y_train))\nprint('xgboost Regressor Validation Score :', xg.score(x_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:31:37.623244Z","iopub.execute_input":"2022-07-18T09:31:37.623499Z","iopub.status.idle":"2022-07-18T09:34:12.026471Z","shell.execute_reply.started":"2022-07-18T09:31:37.623470Z","shell.execute_reply":"2022-07-18T09:34:12.025906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred3 = xg.predict(x_val)\nRMSE3 = np.sqrt(mean_squared_error(y_val, y_pred3)) \nprint('RMSE value :', RMSE3)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:34:12.029837Z","iopub.execute_input":"2022-07-18T09:34:12.031339Z","iopub.status.idle":"2022-07-18T09:34:12.434248Z","shell.execute_reply.started":"2022-07-18T09:34:12.031306Z","shell.execute_reply":"2022-07-18T09:34:12.433698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop('site_eui',axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:34:12.437368Z","iopub.execute_input":"2022-07-18T09:34:12.438844Z","iopub.status.idle":"2022-07-18T09:34:12.444905Z","shell.execute_reply.started":"2022-07-18T09:34:12.438811Z","shell.execute_reply":"2022-07-18T09:34:12.444224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.DataFrame()\n#sub['id']=[75757 + i for i in range(test.shape[0])]\nsub['id']=test.id\nsub[\"site_eui\"]=xg.predict(test.drop('id',axis = 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:34:12.448280Z","iopub.execute_input":"2022-07-18T09:34:12.448480Z","iopub.status.idle":"2022-07-18T09:34:12.848354Z","shell.execute_reply.started":"2022-07-18T09:34:12.448456Z","shell.execute_reply":"2022-07-18T09:34:12.847750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:34:12.849616Z","iopub.execute_input":"2022-07-18T09:34:12.850004Z","iopub.status.idle":"2022-07-18T09:34:12.859480Z","shell.execute_reply.started":"2022-07-18T09:34:12.849968Z","shell.execute_reply":"2022-07-18T09:34:12.858921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:34:12.860911Z","iopub.execute_input":"2022-07-18T09:34:12.861298Z","iopub.status.idle":"2022-07-18T09:34:12.893222Z","shell.execute_reply.started":"2022-07-18T09:34:12.861262Z","shell.execute_reply":"2022-07-18T09:34:12.892563Z"},"trusted":true},"execution_count":null,"outputs":[]}]}