{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\n%matplotlib inline\nimport matplotlib.pyplot as plt\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.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data_bm = pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv',encoding='utf-8')\ndata_wtrain = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv',encoding='utf-8')\ndata_wtest = pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv',encoding='utf-8')\ndata_train = pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv',encoding='utf-8')\ndata_test = pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv',encoding='utf-8')\nsample_submission = pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv',encoding='utf-8')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_train=data_train.head(10108050)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"building_metadata:\",data_bm.shape,'\\n'\"weather_train:\",data_wtrain.shape,'\\n'\"weather_test:\",data_wtest.shape,'\\n'\n      \"train:\",data_train.shape,'\\n'\"data_test:\",data_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"data_train['meter']=data_train.meter.map({'Education':1,'Lodging/residential':2,'Entertainment/public assembly':3,\n                                           'Public services':4,'Office':5,'Technology/science':6,'Utility':7,\n                                           'Parking':8,'Other':9,'Healthcare':10,'Manufacturing/industrial':11})"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_bm['primary_use']=data_bm.primary_use.map({'Education':1,'Lodging/residential':2,'Entertainment/public assembly':3,\n                                           'Public services':4,'Office':5,'Technology/science':6,'Utility':7,\n                                           'Parking':8,'Other':9,'Healthcare':10,'Manufacturing/industrial':11})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_bm[\"building_age\"]=data_bm[\"year_built\"]-data_bm[\"year_built\"].min()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for column in data_bm.iloc[:,1:7]:\n    plt.figure()\n    data_bm.boxplot([column])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Check the memory usage of the data frames in MB"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"building_metadata:\",data_bm.memory_usage().sum() / 1024**2,'\\n'\"weather_train:\",data_wtrain.memory_usage().sum()/ 1024**2,\n      '\\n'\"weather_test:\",data_wtest.memory_usage().sum() / 1024**2,'\\n'\"train:\",data_train.memory_usage().sum() / 1024**2,'\\n'\"data_test:\",\n      data_test.memory_usage().sum() / 1024**2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**information on memory consumption**\n\nint8 ot uint8: consumes 1 byte of memory range:0 to 255.\n\nbool :consumes 1 byte of memory range: True or False.\n\nfloat16,int16,uint16 : consumes 2 bytes of memory range:0 to 65535 \n\nfloat32,int32,unit32 : consumes 4 bytes of memory range:0 to 2147483647\n\nfloat64,int64,uint64 : consumes 8 bytes of memory range \n\ne.g for the data_bm data frame contain 3 float32,1 object,1 uint16,1 uint8\n\ni.e (3*4)+(1*2)+(1*1)"},{"metadata":{},"cell_type":"markdown","source":"**Function to reduce the memory size.**\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def reduce_mem_usage(dataframe):\n    start_mem_usg = dataframe.memory_usage().sum() / 1024**2 \n    print(\"Memory usage of dataframe is :\",start_mem_usg,\" MB\")\n    NAlist = [] \n    for col in dataframe.columns:\n        if dataframe[col].dtype != object:\n            print(\"******************************\")\n            print(\"Column: \",col)\n            print(\"dtype before: \",dataframe[col].dtype)\n            IsInt = False\n            mx = dataframe[col].max()\n            mn = dataframe[col].min()\n            print(\"max value of a column:\",mx)\n            print(\"min value of a column:\",mn)\n            if not np.isfinite(dataframe[col]).all(): \n                NAlist.append(col)\n                #dataframe[col].fillna(mn-1,inplace=True)\n                dataframe[col].fillna(0,inplace=True)\n                asint = dataframe[col].fillna(0).astype(np.int64)\n                result = (dataframe[col] - asint)\n                result = result.sum()\n                print(\"result:\",result)\n                if result > -0.01 and result < 0.01:\n                    IsInt = True\n            if IsInt:\n                if mn >= 0:\n                    if mx < 255:\n                        dataframe[col] = dataframe[col].astype(np.uint8)\n                        print(\"in < 255 loop:\",col)\n                    elif mx < 65535:\n                        dataframe[col] = dataframe[col].astype(np.uint16)\n                        print(\"in < 65535 loop:\",col)\n                    elif mx < 4294967295:\n                        dataframe[col] = dataframe[col].astype(np.uint32)\n                        print(\"in < 4294967295 loop:\",col)\n                    else:\n                        dataframe[col] = dataframe[col].astype(np.uint64)\n                        print(\"in uint64 loop:\",col)\n                else:\n                    if mn > np.iinfo(np.int8).min and mx < np.iinfo(np.int8).max:\n                        dataframe[col] = dataframe[col].astype(np.int8)\n                    elif mn > np.iinfo(np.int16).min and mx < np.iinfo(np.int16).max:\n                        dataframe[col] = dataframe[col].astype(np.int16)\n                    elif mn > np.iinfo(np.int32).min and mx < np.iinfo(np.int32).max:\n                        dataframe[col] = dataframe[col].astype(np.int32)\n                    elif mn > np.iinfo(np.int64).min and mx < np.iinfo(np.int64).max:\n                        dataframe[col] = dataframe[col].astype(np.int64)  \n            else:\n                dataframe[col] = dataframe[col].astype(np.float32)\n            \n            # Print new column type\n        print(\"dtype after: \",dataframe[col].dtype)\n        print(\"******************************\")\n            \n    print(\"___MEMORY USAGE AFTER COMPLETION:___\")\n    mem_usg = dataframe.memory_usage().sum() / 1024**2 \n    print(\"Memory usage is: \",mem_usg,\" MB\")\n    print(\"This is \",100*mem_usg/start_mem_usg,\"% of the initial size\")                                                \n    return data_bm, NAlist","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(data_bm)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with '0': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(data_wtrain)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with '0': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(data_wtest)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with '0': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(data_train)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with ''0'': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(data_test)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with ''0'': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datamerg=pd.merge(data_train,data_bm,on='building_id',how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.merge(datamerg,data_wtrain,how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"timestamp\"] = pd.to_datetime(train[\"timestamp\"])\ntrain[\"hour\"] = train[\"timestamp\"].dt.hour\ntrain[\"day\"]=train[\"timestamp\"].dt.day\n#train[\"weekday_name\"]=train[\"timestamp\"].dt.weekday_name\ntrain[\"weekday\"]=train[\"timestamp\"].dt.weekday\ntrain[\"month\"]=train[\"timestamp\"].dt.month","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=train.drop([\"timestamp\"],axis=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"catagorical columns"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"train['primary_use']=train.primary_use.map({'Education':1,'Lodging/residential':2,'Entertainment/public assembly':3,\n                                           'Public services':4,'Office':5,'Technology/science':6,'Utility':7,\n                                           'Parking':8,'Other':9,'Healthcare':10,'Manufacturing/industrial':11})"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(train)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with ''0'': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=train[(train.square_feet>0) & (train.year_built>0) & (train.floor_count >0) & (train.day >0) & (train.month >0)\n           & (train.primary_use >0)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**feature Engineering**"},{"metadata":{},"cell_type":"markdown","source":"Encoding of the catagorical columns"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"train=train.drop([\"primary_use\"],axis=1)"},{"metadata":{"trusted":true},"cell_type":"code","source":"def correlation_heatmap(data):\n    correlation=train.corr()\n    \n    fig,ax=plt.subplots(figsize=(10,10))\n    sns.heatmap(correlation, vmax=1.0, center=0, fmt='.2f',\n                square=True, linewidths=.5, annot=True, cbar_kws={\"shrink\": .70})\n    plt.show();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"correlation_heatmap(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndel data_wtrain\ndel data_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### regression"},{"metadata":{"trusted":true},"cell_type":"code","source":"#features=train.drop(['meter_reading','precip_depth_1_hr','weekday','wind_direction','wind_speed','day','hour'],axis=1)\n#features=train.drop(['meter_reading','precip_depth_1_hr','weekday','wind_direction','wind_speed'],axis=1)\n#features=train.drop(['meter_reading','weekday','wind_direction','wind_speed','building_id','site_id'],axis=1)\nfeatures=train.drop(['meter_reading','weekday','wind_direction','wind_speed','building_id','site_id','year_built'],axis=1)\ntarget=train[\"meter_reading\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in features.columns:\n    if features[col].isnull().any():\n       print(col)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn import ensemble","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train,X_test,y_train,y_test =train_test_split(features,target,test_size=0.2,random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\nfrom sklearn.ensemble import RandomForestRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rf = RandomForestRegressor(n_estimators = 10, random_state = 42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rf.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions1 = rf.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"errors1 = abs(predictions1 - y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#y_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Root Mean square Error:', np.sqrt(round(np.mean(errors1), 2)), 'degrees.')\n#from sklearn.metrics import mean_squared_error \n\n#print(np.sqrt(np.mean((y_test-predictions1)**2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#from bayes_opt import BayesianOptimization","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Try with bayesian optimaization**"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"def bayesian_optimization(dataset, function, parameters):\n   X_train, y_train, X_test, y_test = dataset\n   n_iterations = 5\n   gp_params = {\"alpha\": 1e-4}\n\n   BO = BayesianOptimization(function, parameters)\n   BO.maximize(n_iter=n_iterations, **gp_params)\n\n   return BO.max"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"def rfc_optimization(cv_splits):\n    def function(n_estimators, max_depth, min_samples_split):\n        return cross_val_score(\n               RandomForestRegressor(\n                   n_estimators=int(max(n_estimators,0)),                                                               \n                   max_depth=int(max(max_depth,1)),\n                   min_samples_split=int(max(min_samples_split,2)), \n                   n_jobs=-1, \n                   random_state=42   \n                   #class_weight=\"balanced\"\n               ),  \n               X=X_train, \n               y=y_train, \n               cv=cv_splits,\n               #scoring=\"roc_auc\",\n               n_jobs=-1).mean()\n\n    parameters = {\"n_estimators\": (10, 50),\n                  \"max_depth\": (1, 150),\n                  \"min_samples_split\": (2, 10)}\n    \n    return function, parameters"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"def train(X_train, y_train, X_test, y_test, function, parameters):\n    dataset = (X_train, y_train, X_test, y_test)\n    cv_splits = 4\n    \n    best_solution = bayesian_optimization(dataset, function, parameters)      \n    params = best_solution[\"params\"]\n\n    model = RandomForestRegressor(\n             n_estimators=int(max(params[\"n_estimators\"], 0)),\n             max_depth=int(max(params[\"max_depth\"], 1)),\n             min_samples_split=int(max(params[\"min_samples_split\"], 2)), \n             n_jobs=-1, \n             random_state=42   \n             #class_weight=\"balanced\"\n             )\n\n    model.fit(X_train, y_train)\n    \n    return model\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"#function,parameters = rfc_optimization(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#regressor = train(X_train, y_train, X_test, y_test, function, parameters)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"pred = regressor.predict(X_test)\nerrors = abs(pred - y_test)\nprint('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')"},{"metadata":{},"cell_type":"markdown","source":"**Applying for test data**"},{"metadata":{"trusted":true},"cell_type":"code","source":"datamerg_test=pd.merge(data_test,data_bm,on='building_id',how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=pd.merge(datamerg_test,data_wtest,how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#del data_bm\ndel data_wtest\ndel datamerg_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test[\"timestamp\"] = pd.to_datetime(test[\"timestamp\"])\ntest[\"hour\"] = test[\"timestamp\"].dt.hour\ntest[\"day\"]=test[\"timestamp\"].dt.day\n#train[\"weekday_name\"]=train[\"timestamp\"].dt.weekday_name\ntest[\"weekday\"]=test[\"timestamp\"].dt.weekday\ntest[\"month\"]=test[\"timestamp\"].dt.month","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=test.drop([\"timestamp\"],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['primary_use']=test.primary_use.map({'Education':1,'Lodging/residential':2,'Entertainment/public assembly':3,\n                                           'Public services':4,'Office':5,'Technology/science':6,'Utility':7,\n                                           'Parking':8,'Other':9,'Healthcare':10,'Manufacturing/industrial':11})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe, NAlist = reduce_mem_usage(test)\nprint(\"_________________\")\nprint(\"\")\nprint(\"Warning: the following columns have missing values filled with ''0'': \")\nprint(\"_________________\")\nprint(\"\")\nprint(NAlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#test=test[(test.square_feet>0) & (test.year_built>0) & (test.floor_count >0) & (test.day >0) & (test.month >0)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"test['primary_use']=test.primary_use.map({'Education':0,'Lodging/residential':1,'Entertainment/public assembly':2,\n                                           'Public services':3,'Office':4,'Technology/science':5,'Utility':6,\n                                           'Parking':7,'Other':8,'Healthcare':9,'Manufacturing/industrial':10})"},{"metadata":{"trusted":true},"cell_type":"code","source":"#test=test.drop([\"weekday\",\"precip_depth_1_hr\",'wind_direction','wind_speed'],axis=1)\n#test=test.drop(['weekday','wind_direction','wind_speed','building_id','site_id'],axis=1)\ntest=test.drop(['weekday','wind_direction','wind_speed','building_id','site_id','year_built'],axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=test.drop([\"row_id\"],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"test=test.drop([\"weekday\",\"precip_depth_1_hr\",'wind_direction','wind_speed','day','hour'],axis=1)"},{"metadata":{"trusted":true},"cell_type":"code","source":"features.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#test_pred=regressor.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_pred = rf.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sample_submission[\"row_id\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output = pd.DataFrame({'row_id':sample_submission[\"row_id\"], \n                       'meter_reading': test_pred})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.to_csv('submission5.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}