{"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":"SEED = 42\nnp.random.seed(SEED)\n\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import LabelEncoder\nfrom catboost import Pool, CatBoostRegressor\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\")\ntest = pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\")\n\ntrain_weather = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\")\ntest_weather = pd.read_csv(\"../input/ashrae-energy-prediction/weather_test.csv\")\n\nmetadata = pd.read_csv(\"../input/ashrae-energy-prediction/building_metadata.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"merged_train = pd.merge(train, metadata, how=\"left\", on=[\"building_id\"])\nmerged_test = pd.merge(test, metadata, how=\"left\", on=[\"building_id\"])\n\ndel train, test, metadata\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def simple_mem_reduce(df):\n    for col in df.columns:\n        if df[col].dtype == int:\n            m = df[col].max()\n            if m > np.iinfo(np.uint32).max:\n                df[col] = df[col].astype(np.uint64)\n            elif m > np.iinfo(np.uint16).max:\n                df[col] = df[col].astype(np.uint32)\n            elif m > np.iinfo(np.uint8).max:\n                df[col] = df[col].astype(np.uint16)\n            elif m < np.iinfo(np.uint8).max:\n                df[col] = df[col].astype(np.uint8)\n                \n        elif df[col].dtype == float:\n            m = df[col].max()\n            if m > np.finfo(np.float32).max:\n                df[col] = df[col].astype(np.float64)\n            elif m > np.finfo(np.float16).max:\n                df[col] = df[col].astype(np.float32)\n            elif m < np.finfo(np.float32).max:\n                df[col] = df[col].astype(np.float16)\n        \n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"merged_train = simple_mem_reduce(merged_train)\nmerged_test = simple_mem_reduce(merged_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_weather = simple_mem_reduce(train_weather)\ntest_weather = simple_mem_reduce(test_weather)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.merge(merged_train, train_weather, \\\n                    how=\"left\", on=[\"site_id\", \"timestamp\"])\ntest_df = pd.merge(merged_test, test_weather, \\\n                    how=\"left\", on=[\"site_id\", \"timestamp\"])\n\ndel merged_train, merged_test, train_weather, test_weather\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"timestamp\"] = pd.to_datetime(train_df[\"timestamp\"])\ntest_df[\"timestamp\"] = pd.to_datetime(test_df[\"timestamp\"])\n\n\ntrain_df[\"year\"] = train_df[\"timestamp\"].dt.year.astype(np.uint16)\ntrain_df[\"month\"] = train_df[\"timestamp\"].dt.month.astype(np.uint8)\ntrain_df[\"day\"] = train_df[\"timestamp\"].dt.day.astype(np.uint8)\ntrain_df[\"hour\"] = train_df[\"timestamp\"].dt.hour.astype(np.uint8)\n\ntest_df[\"year\"] = test_df[\"timestamp\"].dt.year.astype(np.uint16)\ntest_df[\"month\"] = test_df[\"timestamp\"].dt.month.astype(np.uint8)\ntest_df[\"day\"] = test_df[\"timestamp\"].dt.day.astype(np.uint8)\ntest_df[\"hour\"] = test_df[\"timestamp\"].dt.hour.astype(np.uint8)\n\ndel train_df[\"timestamp\"], test_df[\"timestamp\"]\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"encoder = LabelEncoder()\n\ntrain_df[\"primary_use\"] = encoder.fit_transform(train_df[\"primary_use\"]).astype(np.uint8)\ntest_df[\"primary_use\"] = encoder.fit_transform(test_df[\"primary_use\"]).astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Build Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"def RMSLE(y_true, y_pred, *args, **kwargs):\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df = train_df.fillna(-999)\n# test_df = test_df.fillna(-999)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train_df[\"meter_reading\"]\n\ndel train_df[\"meter_reading\"]\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = CatBoostRegressor(loss_function=\"RMSE\",\n                           eval_metric=\"RMSE\",\n                           task_type=\"GPU\",\n                           learning_rate=0.01,\n                           iterations=180000,\n                           l2_leaf_reg=5,\n                           random_seed=42,\n                           od_type=\"Iter\",\n                           depth=7,\n                           early_stopping_rounds=3000,\n                           border_count=32\n                          )\n\ntrain_data = Pool(train_df[:-100000], label=np.log1p(y)[:-100000])\nvalid_data = Pool(train_df[-100000:], label=np.log1p(y)[-100000:])\n\nclf = model.fit(train_data,\n                    eval_set=valid_data,\n                    use_best_model=True,\n                    verbose=2000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_pred = clf.predict(train_df[-100000:])\nval_pred = np.exp(val_pred)\n\nRMSLE(y[-100000:], val_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = clf.predict(test_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv(\"../input/ashrae-energy-prediction/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub[\"meter_reading\"] = np.exp(pred)\nsub.to_csv(\"submission.csv\", index=False)\nsub.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}