{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Imports"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder\nfrom lightgbm import LGBMRegressor\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import train_test_split\nimport lightgbm as lgb\nfrom tqdm import tqdm\n\n\"\"\"\ninspiration from:\nhttps://www.kaggle.com/gouherdanishiitkgp/ashrae-basic-eda-and-feature-engineering\nhttps://www.kaggle.com/isaienkov/lightgbm-fe-1-19\nhttps://www.kaggle.com/rohanrao/ashrae-divide-and-conquer\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Changing the dtypes of the rows as recommended.\nRecomendation from: https://www.kaggle.com/kulkarnivishwanath/ashrae-great-energy-predictor-iii-eda-model"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"metadata_dtype = {\"site_id\":\"uint8\",\"building_id\":\"uint16\",\"square_feet\":\"float32\",\"year_built\":\"float32\",\"floor_count\":\"float16\"}\nmetadata = pd.read_csv(\"../input/ashrae-energy-prediction/building_metadata.csv\",dtype=metadata_dtype)\nmetadata.drop(\"floor_count\", axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_dtype = {\"site_id\":\"uint8\"}\nweather_train = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\",parse_dates=[\"timestamp\"],dtype=weather_dtype)\nweather_test = pd.read_csv(\"../input/ashrae-energy-prediction/weather_test.csv\",parse_dates=[\"timestamp\"],dtype=weather_dtype)\nweather_train.drop([\"sea_level_pressure\", \"wind_direction\", \"wind_speed\"], axis=1, inplace=True)\nweather_test.drop([\"sea_level_pressure\", \"wind_direction\", \"wind_speed\"], axis=1, inplace=True)\nweather_train = weather_train.groupby(\"site_id\").apply(lambda group: group.interpolate(limit_direction=\"both\"))\nweather_test = weather_test.groupby(\"site_id\").apply(lambda group: group.interpolate(limit_direction=\"both\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dtype = {\"meter\":\"uint8\",\"building_id\":\"uint16\",\"meter_reading\":\"float32\"}\ntrain = pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\",parse_dates=[\"timestamp\"],dtype=train_dtype)\ntest_dtype = {\"meter\":\"uint8\",\"building_id\":\"uint16\"}\ntest_cols_to_read = [\"building_id\",\"meter\",\"timestamp\"]\ntest = pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\",parse_dates=[\"timestamp\"],usecols=test_cols_to_read,dtype=test_dtype)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def lag_features(dataset):\n    features = [\"air_temperature\", \"cloud_coverage\", \"dew_temperature\", \"precip_depth_1_hr\"]\n    grouped = dataset.groupby(\"site_id\")\n    rolled = grouped[features].rolling(window=15, min_periods=0)\n    dataset_mean = rolled.mean().reset_index().astype(np.float16)\n    dataset_median = rolled.median().reset_index().astype(np.float16)\n    dataset_min = rolled.min().reset_index().astype(np.float16)\n    dataset_max = rolled.max().reset_index().astype(np.float16)\n    \n    for feature in features:\n        dataset[f\"{feature}_mean_lag\"] = dataset_mean[feature]\n        dataset[f\"{feature}_median_lag\"] = dataset_median[feature]\n        dataset[f\"{feature}_max_lag\"] = dataset_max[feature]\n        dataset[f\"{feature}_min_lag\"] = dataset_min[feature]\n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train = lag_features(weather_train)\nweather_test = lag_features(weather_test)\nweather_train.drop([\"air_temperature\", \"cloud_coverage\", \"dew_temperature\", \"precip_depth_1_hr\"], axis=1, inplace=True)\nweather_test.drop([\"air_temperature\", \"cloud_coverage\", \"dew_temperature\", \"precip_depth_1_hr\"], axis=1, inplace=True)\nprint(weather_train.shape)\nprint(weather_test.shape)\ntrain = train.merge(metadata, left_on = \"building_id\", right_on = \"building_id\", how = \"left\")\ntrain = train.merge(weather_train, left_on = [\"timestamp\", \"site_id\"], right_on=[\"timestamp\", \"site_id\"], how = \"left\")\ntest = test.merge(metadata, left_on = \"building_id\", right_on = \"building_id\", how = \"left\")\ntest = test.merge(weather_test, left_on = [\"timestamp\", \"site_id\"], right_on=[\"timestamp\", \"site_id\"], how = \"left\")\nprint(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"day\"] = train[\"timestamp\"].dt.weekday.astype(\"uint8\")\ntrain[\"hour\"] = train[\"timestamp\"].dt.hour.astype(\"uint8\")\ntest[\"day\"] = test[\"timestamp\"].dt.weekday.astype(\"uint8\")\ntest[\"hour\"] = test[\"timestamp\"].dt.hour.astype(\"uint8\")\ntrain.drop(\"timestamp\", axis=1, inplace=True)\ntest.drop(\"timestamp\", axis=1, inplace=True)\nbuilding_meter = train.groupby([\"building_id\", \"meter\"]).agg(mean_building_meter=(\"meter_reading\", \"mean\"), \n                                                                median_building_meter=(\"meter_reading\", \"median\")).reset_index()\ntrain = train.merge(building_meter, on=[\"building_id\", \"meter\"])\ntest = test.merge(building_meter, on=[\"building_id\", \"meter\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = train[train[\"meter_reading\"]>0.0]\nprint(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le = LabelEncoder()\ntrain[\"primary_use\"] = le.fit_transform(train[\"primary_use\"])\ntest[\"primary_use\"] = le.fit_transform(test[\"primary_use\"])\ncategorical = [\"site_id\", \"building_id\", \"primary_use\", \"hour\", \"day\", \"meter\"]\nfeatures = [col for col in train.columns if col != \"meter_reading\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = np.log1p(train[\"meter_reading\"])\nparams = {\"objective\": \"regression\",\n          \"metric\": \"rmse\",\n          \"num_leaves\": 30,\n          \"learning_rate\": 0.05,\n          \"bagging_fraction\": 0.50,\n          \"feature_fraction\": 0.80,\n          \"bagging_freq\": 5}\n\nkf = StratifiedKFold(n_splits = 2, shuffle=True)\nmodel_arr = []\nfor train_id, val_id in kf.split(train, train[\"building_id\"]):\n    X_train = train[features].iloc[train_id]\n    X_test = train[features].iloc[val_id]\n    y_train = labels.iloc[train_id]\n    y_test = labels.iloc[val_id]\n    lgb_train = lgb.Dataset(X_train, y_train, categorical_feature=categorical)\n    lgb_test = lgb.Dataset(X_test, y_test, categorical_feature=categorical)\n    gbm = lgb.train(params, lgb_train, num_boost_round = 500, valid_sets = (lgb_train, lgb_test), \n                    early_stopping_rounds = 100, verbose_eval = 100)\n    model_arr.append(gbm)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"start = 0\npreds = []\ninterval = 50000\nfor _ in tqdm(range(int(np.ceil(len(test)/interval)))):\n    preds.append(np.expm1(sum([model.predict(test.iloc[start:start+interval]) for model in model_arr])/2))\n    start += interval\n\nsub = pd.DataFrame({\"row_id:\":test.index, \"meter_reading\":np.concatenate(preds)})\nsub.to_csv(\"submission.csv\", index=False, float_format=\"%.4f\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(sub.head())\nprint(sub.tail())\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\")\nleak0 = pd.read_csv(\"../input/site0.csv\")\nleak1 = pd.read_csv(\"../input/site1.csv\")\nleak2 = pd.read_csv(\"../input/site2.csv\")\nleak4 = pd.read_csv(\"../input/site4.csv\")\nleak15 = pd.read_csv(\"../input/site15.csv\")\n\nleak = pd.concat([leak0, leak1, leak2, leak4, leak15])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"leak.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = test[test.building_id.isin(leak.building_id.unique())]\nleak = leak.merge(test, on=[\"building_id\", \"meter\", \"timestamp\"])\nsub = sub.merge(leak[[\"row_id\", \"meter_reading_scraped\"]], on=[\"row_id\"], how=\"left\")\nsub.loc[sub.meter_reading_scraped.notnull(), \"meter_reading\"] = sub.loc[sub.meter_reading_scraped.notnull(), \"meter_reading_scraped\"]\nsub.drop([\"meter_reading_scraped\"], axis=1, inplace=True)\nsub.head()\n\nsub.to_csv(\"submission_lk.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}