{"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)\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":{"trusted":true},"cell_type":"code","source":"#Based on this great kernel https://www.kaggle.com/arjanso/reducing-dataframe-memory-size-by-65\ndef reduce_mem_usage(df):\n    start_mem_usg = df.memory_usage().sum() / 1024**2 \n    print(\"Memory usage of properties dataframe is :\",start_mem_usg,\" MB\")\n    NAlist = [] # Keeps track of columns that have missing values filled in. \n    for col in df.columns:\n        if df[col].dtype != object:  # Exclude strings            \n            # Print current column type\n            print(\"******************************\")\n            print(\"Column: \",col)\n            print(\"dtype before: \",df[col].dtype)            \n            # make variables for Int, max and min\n            IsInt = False\n            mx = df[col].max()\n            mn = df[col].min()\n            print(\"min for this col: \",mn)\n            print(\"max for this col: \",mx)\n            # Integer does not support NA, therefore, NA needs to be filled\n            if not np.isfinite(df[col]).all(): \n                NAlist.append(col)\n                df[col].fillna(mn-1,inplace=True)  \n                   \n            # test if column can be converted to an integer\n            asint = df[col].fillna(0).astype(np.int64)\n            result = (df[col] - asint)\n            result = result.sum()\n            if result > -0.01 and result < 0.01:\n                IsInt = True            \n            # Make Integer/unsigned Integer datatypes\n            if IsInt:\n                if mn >= 0:\n                    if mx < 255:\n                        df[col] = df[col].astype(np.uint8)\n                    elif mx < 65535:\n                        df[col] = df[col].astype(np.uint16)\n                    elif mx < 4294967295:\n                        df[col] = df[col].astype(np.uint32)\n                    else:\n                        df[col] = df[col].astype(np.uint64)\n                else:\n                    if mn > np.iinfo(np.int8).min and mx < np.iinfo(np.int8).max:\n                        df[col] = df[col].astype(np.int8)\n                    elif mn > np.iinfo(np.int16).min and mx < np.iinfo(np.int16).max:\n                        df[col] = df[col].astype(np.int16)\n                    elif mn > np.iinfo(np.int32).min and mx < np.iinfo(np.int32).max:\n                        df[col] = df[col].astype(np.int32)\n                    elif mn > np.iinfo(np.int64).min and mx < np.iinfo(np.int64).max:\n                        df[col] = df[col].astype(np.int64)    \n            # Make float datatypes 32 bit\n            else:\n                df[col] = df[col].astype(np.float32)\n            \n            # Print new column type\n            print(\"dtype after: \",df[col].dtype)\n            print(\"******************************\")\n    # Print final result\n    print(\"___MEMORY USAGE AFTER COMPLETION:___\")\n    mem_usg = df.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 df, NAlist","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"%%time\n\n# Read data...\nroot = '../input/ashrae-energy-prediction'\n\ntrain_df = pd.read_csv(os.path.join(root, 'train.csv'))\nweather_train_df = pd.read_csv(os.path.join(root, 'weather_train.csv'))\ntest_df = pd.read_csv(os.path.join(root, 'test.csv'))\nweather_test_df = pd.read_csv(os.path.join(root, 'weather_test.csv'))\nbuilding_meta_df = pd.read_csv(os.path.join(root, 'building_metadata.csv'))\nsample_submission = pd.read_csv(os.path.join(root, 'sample_submission.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# transfer to .feature format\n\ntrain_df.to_feather('train.feather')\ntest_df.to_feather('test.feather')\nweather_train_df.to_feather('weather_train.feather')\nweather_test_df.to_feather('weather_test.feather')\nbuilding_meta_df.to_feather('building_metadata.feather')\nsample_submission.to_feather('sample_submission.feather')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %%time\n\ntrain_df = pd.read_feather('train.feather')\nweather_train_df = pd.read_feather('weather_train.feather')\n# test_df = pd.read_feather('test.feather')\n# weather_test_df = pd.read_feather('weather_test.feather')\nbuilding_meta_df = pd.read_feather('building_metadata.feather')\nsample_submission = pd.read_feather('sample_submission.feather')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = train_df.merge(building_meta_df, left_on = \"building_id\", right_on = \"building_id\", how = \"left\")\ntrain_df = train_df.merge(weather_train_df, left_on = [\"site_id\", \"timestamp\"], right_on = [\"site_id\", \"timestamp\"], how = \"left\")\n# del weather_train_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":">## Extract features"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"timestamp\"] = pd.to_datetime(train_df[\"timestamp\"])\ntrain_df[\"hour\"] = train_df[\"timestamp\"].dt.hour\ntrain_df[\"day\"] = train_df[\"timestamp\"].dt.day\ntrain_df[\"weekend\"] = train_df[\"timestamp\"].dt.weekday\ntrain_df[\"month\"] = train_df[\"timestamp\"].dt.month\ntrain_df = train_df.drop(\"timestamp\", axis = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> ## Removing weired data on site_id 0\n\nAs you can see above, this data looks weired until May 20. It is reported in [this discussion](https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113054#656588) by @barnwellguy that **All electricity meter is 0 until May 20 for site_id == 0**. I will remove these data from training data.\n\nIt corresponds to `building_id <= 104`."},{"metadata":{"trusted":true},"cell_type":"code","source":"# # plot the log of (1+ meter_reading)\n# import plotly.express as px\n# def plot_date_usage(train_df, meter=0, building_id=0):\n#     train_temp_df = train_df[train_df['meter'] == meter]\n#     train_temp_df = train_temp_df[train_temp_df['building_id'] == building_id]    \n    \n#     train_temp_df_meter = train_temp_df.groupby('date')['meter_reading_log1p'].sum()\n#     train_temp_df_meter = train_temp_df_meter.to_frame().reset_index()\n#     fig = px.line(train_temp_df_meter, x='date', y='meter_reading_log1p')\n#     fig.show()\n# plot_date_usage(train_df, meter=0, building_id=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# building_meta_df[building_meta_df.site_id == 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df = train_df.query('not (building_id <= 104 & meter == 0 & timestamp <= \"2016-05-20\")')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":">## Transform features"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ntrain_df['primary_use'] = le.fit(train_df['primary_use']).transform(train_df['primary_use'])\ntrain_df['primary_use'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"categoricals = [\"building_id\", \"primary_use\", \"hour\", \"day\", \"weekend\", \"month\", \"meter\"]\ndrop_cols = [\"precip_depth_1_hr\", \"sea_level_pressure\", \"wind_direction\", \"wind_speed\"]\nnumericals = [\"square_feet\", \"year_built\", \"air_temperature\", \"cloud_coverage\",\n              \"dew_temperature\"]\nfeat_cols = categoricals + numericals\ntrain_df = train_df.drop(drop_cols + [\"site_id\", \"floor_count\"], axis = 1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['meter_reading_log1p'] = np.log1p(train_df['meter_reading'])\ndel train_df[\"meter_reading\"]\ntarget = 'meter_reading_log1p'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df, NAlist = reduce_mem_usage(train_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# (train_y<=0).sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb\ntarget = 'meter_reading_log1p'\nnum_folds = 5\nkf = KFold(n_splits = num_folds, shuffle = False, random_state = 42)\nerror = 0\nmodels = []\nfor i, (train_index, val_index) in enumerate(kf.split(train_df)):\n    if i + 1 < num_folds:\n        continue\n    print(train_index.max(), val_index.min())\n    train_X = train_df[feat_cols].iloc[train_index]\n    val_X = train_df[feat_cols].iloc[val_index]\n    train_y = train_df[target].iloc[train_index]\n    val_y = train_df[target].iloc[val_index]\n    \n    \n    lgb_train = lgb.Dataset(train_X, train_y > 0)\n    lgb_eval = lgb.Dataset(val_X, val_y > 0)\n    params = {\n            'boosting_type': 'gbdt',\n            'objective': 'binary',\n            'metric': {'binary_logloss'},\n            'learning_rate': 0.1,\n            'feature_fraction': 0.8,\n            'bagging_fraction': 0.8,\n            'bagging_freq' : 5\n            }\n    gbm_class = lgb.train(params,\n                lgb_train,\n                num_boost_round=2000,\n                valid_sets=(lgb_train, lgb_eval),\n               early_stopping_rounds=20,\n               verbose_eval = 20)\n    \n    \n    \n    \n    \n    \n    lgb_train = lgb.Dataset(train_X[train_y > 0], train_y[train_y > 0])\n    lgb_eval = lgb.Dataset(val_X[val_y > 0] , val_y[val_y > 0])\n    params = {\n            'boosting_type': 'gbdt',\n            'objective': 'regression',\n            'metric': {'rmse'},\n            'learning_rate': 0.5,\n            'feature_fraction': 0.8,\n            'bagging_fraction': 0.8,\n            'bagging_freq' : 5\n            }\n\n    gbm_regress = lgb.train(params,\n                lgb_train,\n                num_boost_round=2000,\n                valid_sets=(lgb_train, lgb_eval),\n               early_stopping_rounds=20,\n               verbose_eval = 20)\n#     models.append(gbm)\n\n    y_pred = (gbm_class.predict(val_X, num_iteration=gbm_class.best_iteration) > .5) *\\\n    (gbm_regress.predict(val_X, num_iteration=gbm_regress.best_iteration))\n    error += np.sqrt(mean_squared_error(y_pred, (val_y)))/num_folds\n    print(np.sqrt(mean_squared_error(y_pred, (val_y))))\n    break\nprint(error)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sorted(zip(gbm_regress.feature_importance(), gbm_regress.feature_name()),reverse = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc\ndel train_df\ndel train_X, val_X, lgb_train, lgb_eval, train_y, val_y, y_pred, target\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preparing test data\ntest_df = pd.read_feather('test.feather')\ntest_df, NAlist = reduce_mem_usage(test_df)\ntest_df = test_df.merge(building_meta_df, left_on = \"building_id\", right_on = \"building_id\", how = \"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del building_meta_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_test_df = pd.read_feather('weather_test.feather')\nweather_test_df = weather_test_df.drop(drop_cols, axis = 1)\ntest_df = test_df.merge(weather_test_df, left_on = [\"site_id\", \"timestamp\"], right_on = [\"site_id\", \"timestamp\"], how = \"left\")\ndel weather_test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"timestamp\"] = pd.to_datetime(test_df[\"timestamp\"])\ntest_df[\"hour\"] = test_df[\"timestamp\"].dt.hour.astype(np.uint8)\ntest_df[\"day\"] = test_df[\"timestamp\"].dt.day.astype(np.uint8)\ntest_df[\"weekend\"] = test_df[\"timestamp\"].dt.weekday.astype(np.uint8)\ntest_df[\"month\"] = test_df[\"timestamp\"].dt.month.astype(np.uint8)\ntest_df = test_df[feat_cols]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[\"primary_use\"] = le.transform(test_df[\"primary_use\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm\ni=0\nres=[]\nstep_size = 50000\nfor j in tqdm(range(int(np.ceil(test_df.shape[0]/50000)))):\n    \n    res.append(np.expm1((gbm_class.predict(test_df.iloc[i:i+step_size], num_iteration=gbm_class.best_iteration) > .5) *\\\n    (gbm_regress.predict(test_df.iloc[i:i+step_size], num_iteration=gbm_regress.best_iteration))))\n    i+=step_size\n\n    \ndel test_df\nres = np.concatenate(res)\npd.DataFrame(res).describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission[\"meter_reading\"] = res\nsample_submission.to_csv(\"submission.csv\", index = False)","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}