{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../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# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Reduce memory size func \n\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\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prep_data(df):\n    #  expanded on over time\n    df[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"])\n    df.rename(columns = {\"timestamp\":\"date\"},inplace= True)\n    df[\"month\"] = np.uint8(df[\"date\"].dt.month)\n    df[\"week\"] =df[\"date\"].dt.weekofyear\n    df[\"weekday\"] = np.uint8(df[\"date\"].dt.weekday)\n    df[\"day\"] = np.uint8(df[\"date\"].dt.day)\n    df[\"hour\"] = np.uint8(df[\"date\"].dt.hour)\n    df[\"weekend\"] = [1 if x in [5,6] else 0 for x in df.day]\n    \n    df['part_of_day'] = (df['date'].dt.hour % 24 + 4) // 4\n    df['part_of_day'].replace({1: 'Late Night',\n                      2: 'Early Morning',\n                      3: 'Morning',\n                      4: 'Noon',\n                      5: 'Evening',\n                      6: 'Night'}, inplace=True)\n   \n    # Encode meter types\n    df['meter'] = pd.Categorical(df['meter']).rename_categories({0: 'electricity', 1: 'chilledwater', 2: 'steam', 3: 'hotwater'})\n    \n    df['log_square_feet'] = np.log(df['square_feet'])\n    \n    df['year_built'] = df['year_built']-1900\n \n    df.floor_count.fillna(0,inplace = True)  \n    columns = ['air_temperature', 'cloud_coverage', 'dew_temperature','precip_depth_1_hr',\n'sea_level_pressure', 'wind_direction','wind_speed'] \n    for col in columns:\n        imputation = df.groupby(['site_id','month','part_of_day'])[col].transform('mean')\n        df[col].fillna(imputation,inplace = True)\n    df = reduce_mem_usage(df)\n    gc.collect()\n    #remove outliers train data\n   \n    \n    # remove redundant columns\n    for col in df.columns:\n        if col in ['date', 'row_id']:\n            del df[col]\n    if 'meter_reading' in df.columns:\n        df['meter_reading'] = np.log1p(df['meter_reading']) # comp metric uses log errors\n\n    return df\n    \n    print('Data is ready')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\nweather_train = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')\nbuild = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\ntrain = train.merge(build, on='building_id', how='left')\nalltrain = train.merge(weather_train, on=['site_id', 'timestamp'], how='left')\ndel train, weather_train,build\ngc.collect()\nprint('Data is imported')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltrain =prep_data(alltrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" #remove outliers train data\nalltrain.drop(alltrain[(alltrain.site_id==0) & ( alltrain.date < \"2016-05-20 00:00:00\") & \n                       (alltrain.meter ==\"electricity\")].index,inplace= True )\n    \nalltrain.drop(alltrain[alltrain.building_id == 1099].index,inplace = True)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"categorical_columns = [\"building_id\",\"site_id\",\"meter\",\"month\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le = LabelEncoder()\nalltrain.part_of_day =le.fit_transform(alltrain[\"part_of_day\"])\nalltrain.primary_use =le.fit_transform(alltrain[\"primary_use\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltrain[\"building_id\"] = alltrain[\"building_id\"].astype('category')\nalltrain[\"site_id\"] = alltrain[\"site_id\"].astype('category')\nalltrain[\"meter\"] = alltrain[\"meter\"].astype('category')\nalltrain[\"month\"] = alltrain[\"month\"].astype('category')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = alltrain.drop([\"square_feet\",\"year_built\",\"meter_reading\",'precip_depth_1_hr',\n'sea_level_pressure', 'wind_direction','wind_speed'],axis =1)\ny = alltrain[\"meter_reading\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\n\nlgb_train = lgb.Dataset(X_train, y_train)\n\nlgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)\n\nparams = {\n    \"objective\": \"regression\",\n    \"boosting\": \"gbdt\",\n    \"num_leaves\": 40,\n    \"learning_rate\": 0.05,\n    \"feature_fraction\": 0.85,\n    \"reg_lambda\": 2,\n    \"metric\": \"rmse\"\n}\n\n\ngbm = lgb.train(params,\n                train_set=lgb_train,\n               \n                num_boost_round=300,\n                valid_sets=lgb_eval,\n                early_stopping_rounds=150,categorical_feature = categorical_columns)\n\ny_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)\n\nprint('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gbm.save_model('lgb2_regression.txt', num_iteration=gbm.best_iteration) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = lgb.plot_importance(gbm, figsize=(15,15))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('../input/ashrae-energy-prediction/test.csv')\nweather_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\nbuild = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\ntest =test.merge(build, on='building_id', how='left')\nalltest= test.merge(weather_test, on=['site_id', 'timestamp'], how='left')\ndel test, weather_test,build\ngc.collect()\nprint('Data is imported')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltest = prep_data(alltest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltest[\"building_id\"] = alltest[\"building_id\"].astype('category')\nalltest[\"site_id\"] = alltest[\"site_id\"].astype('category')\nalltest[\"month\"] = alltest[\"month\"].astype('category')\nalltest[\"meter\"] = alltest[\"meter\"].astype('category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le = LabelEncoder()\nalltest.part_of_day =le.fit_transform(alltest[\"part_of_day\"])\nalltest.primary_use =le.fit_transform(alltest[\"primary_use\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltest.drop([\"year_built\",\"square_feet\",'precip_depth_1_hr',\n'sea_level_pressure', 'wind_direction','wind_speed'],axis =1,inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"alltest.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gbm = lgb.Booster(model_file='../input/lgb2-reg/lgb2_regression.txt')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_result = gbm.predict(alltest, num_iteration=gbm.best_iteration)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')\nsubmission['meter_reading'] = np.exp(y_result)\nsubmission.loc[submission['meter_reading']<0, 'meter_reading'] = 0\nsubmission.to_csv('submission2.csv', index=False)\n","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":4}