{"cells":[{"metadata":{"_uuid":"58e2ee80-4fc2-44dc-a26e-dc8566ea3eb2","_cell_guid":"5622a21d-828f-4e9e-b877-e89f17c3cefb","trusted":true},"cell_type":"markdown","source":"# Import Libraries"},{"metadata":{"_uuid":"615c2ca7-0e57-4ef6-a06d-5b7d77793517","_cell_guid":"b34e210c-c831-4fa6-85c4-00225b05d363","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb\nfrom tqdm import tqdm\nimport gc","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ad851485-da40-45cd-b6bd-6fc78619e05d","_cell_guid":"611d4f93-128e-476f-ac89-08c7c642ca9a","trusted":true},"cell_type":"code","source":"# Code from https://www.kaggle.com/caesarlupum/ashrae-start-here-a-gentle-introduction \n\n# Function to reduce the DF size\ndef reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2    \n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)    \n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","execution_count":0,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read Input\nRead only training data to avoid RAM overhead"},{"metadata":{"_uuid":"30a5a67d-f6d9-4e5f-9bed-42a309d3ef16","_cell_guid":"57b54a88-3015-4a59-8dc8-12a0348dad84","trusted":true},"cell_type":"code","source":"InputPath = \"../input/ashrae-energy-prediction\"\ntrain_df = pd.read_csv(InputPath+'/train.csv')\nbuilding_df = pd.read_csv(InputPath+'/building_metadata.csv')\nweather_train_df = pd.read_csv(InputPath+'/weather_train.csv')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"f25baa1f-99b2-440c-a873-38324d10d0ff","_cell_guid":"bee93741-bf32-4e9b-b7a3-8ffa6bd4ef27","trusted":true},"cell_type":"markdown","source":"# **EDA**"},{"metadata":{"_uuid":"50ed34bc-0a3f-44c7-bca9-32f9b381a9bf","_cell_guid":"d179e05e-230e-4d12-8067-48b51893fd82","trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"84267e95-9fcf-4d42-88d3-c6d327b903e3","_cell_guid":"c15d7d16-9715-49e9-be15-8f1864a2ffed","trusted":true},"cell_type":"code","source":"train_df.describe()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"0beec647-8de2-40ec-b1b3-197563fe2984","_cell_guid":"28590bb1-7b96-4c29-b94a-b43d03ff7a7e","trusted":true},"cell_type":"code","source":"building_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"9e5058ec-5f44-43c6-b512-d8e879e00324","_cell_guid":"82135349-0e8e-4ea3-987d-540059577cf7","trusted":true},"cell_type":"code","source":"building_df.describe()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"9f2dc73d-f870-4bdc-bc08-37168fc13a28","_cell_guid":"6bba9c25-183f-4389-baf6-d64e1903b250","trusted":true},"cell_type":"code","source":"weather_train_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"2053cc0f-5810-48a1-a2b7-bde8ddcc30ce","_cell_guid":"0b89f6ea-a742-48ce-998e-97527aaa246d","trusted":true},"cell_type":"code","source":"weather_train_df.describe()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"846ebe52-18e2-4a69-a2e9-f59538e3b7f9","_cell_guid":"a0a0ba06-a80c-47e0-b252-b09e5f3ade28","trusted":true},"cell_type":"code","source":"#Reduce memory usage\ntrain_df = reduce_mem_usage(df=train_df)\nweather_train_df = reduce_mem_usage(df=weather_train_df)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"2ce8ebf1-0c12-4728-a2ed-d90369f7b8cf","_cell_guid":"658cc2b2-42c7-4704-8d78-689e4cd04189","trusted":true},"cell_type":"markdown","source":"# Merge Datasets"},{"metadata":{"_uuid":"593855b1-e88d-473d-91db-a570d1d051f3","_cell_guid":"d7c66a39-7f70-4f3e-ac52-080eb007cfa6","trusted":true},"cell_type":"code","source":"\ntrain = pd.merge(train_df,building_df,how = 'left')     \ntrain = pd.merge(train,weather_train_df, on = ['site_id','timestamp'], how = 'left')\nprint(train.shape)\n\ndel train_df\ndel weather_train_df","execution_count":0,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Change timestamp to type timestamp**"},{"metadata":{"trusted":true},"cell_type":"code","source":"train['timestamp'] = pd.to_datetime(train.timestamp)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Extract information from timestamp**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extracting date features from timestamp\ntrain['year'] = train['timestamp'].dt.year\ntrain['month'] = train['timestamp'].dt.month\ntrain['day'] = train['timestamp'].dt.day\ntrain['hour'] = train['timestamp'].dt.hour\ntrain['dayofweek'] = train['timestamp'].dt.dayofweek","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Reduce memory usage\ntrain = reduce_mem_usage(df=train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Now we can drop timestamp**"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = train.drop('timestamp',axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Encode primary_use using LabelEncoder **"},{"metadata":{"trusted":true},"cell_type":"code","source":"le = LabelEncoder()\ntrain[\"primary_use\"] = le.fit_transform(train[\"primary_use\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Convert to categorical datatype\ncat_cols = ['meter', 'primary_use', 'site_id', 'building_id', 'year', 'month', 'day', 'hour', 'dayofweek']\nfor col in cat_cols:\n    train[col] = train[col].astype('category')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Split train to target and features**"},{"metadata":{"trusted":true},"cell_type":"code","source":"target = np.log1p(train[\"meter_reading\"])\nfeatures = train.drop('meter_reading', axis = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Now we can delete train dataframe**"},{"metadata":{"trusted":true},"cell_type":"code","source":"del train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features = reduce_mem_usage(df=features)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **KFOLD LIGHTGBM **"},{"metadata":{"trusted":true},"cell_type":"code","source":"no_splits = 3\nkf = KFold(no_splits)\nLGBM = []\nparams = {\n        \"objective\": \"regression\",\n    \"boosting\": \"gbdt\",\n    \"num_leaves\": 1280,\n    \"learning_rate\": 0.05,\n    \"feature_fraction\": 0.85,\n    \"reg_lambda\": 2,\n    \"metric\": \"rmse\",\n\n}\nfor train,test1 in kf.split(features):\n    train_features = features.loc[train]\n    train_target = target.loc[train]\n    \n    test_features = features.loc[test1]\n    test_target = target.loc[test1]\n    \n    training = lgb.Dataset(train_features, label=train_target,categorical_feature=cat_cols, free_raw_data=False)\n    testing = lgb.Dataset(test_features, label=test_target,categorical_feature=cat_cols, free_raw_data=False)\n    \n    del train_features, train_target, test_features, test_target\n    gc.collect()\n    \n    model = lgb.train(params, train_set=training, num_boost_round=1000, valid_sets=[training,testing], verbose_eval=25, early_stopping_rounds=50)\n    LGBM.append(model)\n    \n    del training, testing\n    gc.collect()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#delete intermediate dataframes\ndel target\ndel features\ndel train\ndel test1\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(InputPath+'/test.csv')\nbuilding_df = pd.read_csv(InputPath+'/building_metadata.csv')\nweather_test_df = pd.read_csv(InputPath+'/weather_test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#drop row_id in test_df\n\ntest_df = test_df.drop(columns=['row_id'])\nimport gc\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reduce memory usage\ntest_df = reduce_mem_usage(df=test_df)\nweather_test_df = reduce_mem_usage(df=weather_test_df)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Merge datasets**"},{"metadata":{"_uuid":"c6b66492-8532-4ee1-824a-a798581c4229","_cell_guid":"a30fa8f9-ec7b-4d44-8d05-d9739681da22","trusted":true},"cell_type":"code","source":"test = pd.merge(test_df,building_df,how = 'left')           \ntest = pd.merge(test,weather_test_df, on = ['site_id','timestamp'], how = 'left')\nprint(test.shape)","execution_count":0,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**We dont need these dataframes anymore**"},{"metadata":{"_uuid":"5896e1f8-d827-4386-a0b2-34429a1dec47","_cell_guid":"a114a9bf-e7fa-40bd-a113-ea96d5d4eec4","trusted":true},"cell_type":"code","source":"del test_df\ndel weather_test_df\ndel building_df","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"24f9c0e3-7a1b-4a33-ae50-b2892b20e980","_cell_guid":"a72ffbb7-5aee-4e26-81df-26e28f05a098","trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":0,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**timestamp update on test data**"},{"metadata":{"_uuid":"06b65fdf-5a4a-4676-a492-b20cb8e93ff6","_cell_guid":"ae71a919-54ab-4baa-a1bd-9f21743bcc4f","trusted":true},"cell_type":"code","source":"test['timestamp'] = pd.to_datetime(test.timestamp)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"50b9a662-a594-4c87-8c2c-d038143fde53","_cell_guid":"229c0595-3886-47c2-a1fd-40ffa93597b5","trusted":true},"cell_type":"code","source":"test['year'] = test['timestamp'].dt.year\ntest['month'] = test['timestamp'].dt.month\ntest['day'] = test['timestamp'].dt.day\ntest['hour'] = test['timestamp'].dt.hour\ntest['dayofweek'] = test['timestamp'].dt.dayofweek","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"0e63cbe4-371d-4123-81ec-03e3f388e667","_cell_guid":"6701026f-4118-4c40-bc2e-112903440368","trusted":true},"cell_type":"code","source":"#Reduce memory usage\ntest = reduce_mem_usage(df=test)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"93b4ba42-778b-4418-bed1-cfe07f286032","_cell_guid":"eb4ffdb1-ef8b-4f96-9992-04a49b3e592b","trusted":true},"cell_type":"code","source":"#Drop timestamp from test\ntest = test.drop('timestamp',axis=1)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"676f43e5-553e-4a1a-83fc-fffb8e5cc4b4","_cell_guid":"9ebf8776-d70f-4d4b-a4f4-d913d9ac93bd","trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":0,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Label Encoder and categorical variables in test dataframe**"},{"metadata":{"_uuid":"0bca0e06-9999-4739-a16c-b3f7eda8375f","_cell_guid":"1523861b-ce51-4c28-8cdc-a0f8cb7192c8","trusted":true},"cell_type":"code","source":"le = LabelEncoder()\ntest[\"primary_use\"] = le.fit_transform(test[\"primary_use\"])","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4f9d1ff4-bf73-49b3-b30a-52e43c85a523","_cell_guid":"e04dddb2-5fc6-4006-901c-7da8420e0707","trusted":true},"cell_type":"code","source":"# Convert to categorical datatype\ncat_cols = ['meter', 'primary_use', 'site_id', 'building_id', 'year', 'month', 'day', 'hour', 'dayofweek']\nfor col in cat_cols:\n    test[col] = test[col].astype('category')","execution_count":0,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prediction on test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"i=0\nresult=[]\nstep_size = 50000\nfor j in tqdm(range(int(np.ceil(test.shape[0]/50000)))):\n    result.append(np.expm1(sum([model.predict(test.iloc[i:i+step_size]) for model in LGBM])/no_splits))\n    i+=step_size\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = np.concatenate(result)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Submit to csv**"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv(InputPath+'/sample_submission.csv')\nsubmission['meter_reading'] = result\nsubmission.loc[submission['meter_reading']<0, 'meter_reading'] = 0\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}