{"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_minor":4,"nbformat":4,"cells":[{"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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-24T05:38:32.694628Z","iopub.execute_input":"2022-06-24T05:38:32.695530Z","iopub.status.idle":"2022-06-24T05:38:32.709507Z","shell.execute_reply.started":"2022-06-24T05:38:32.695492Z","shell.execute_reply":"2022-06-24T05:38:32.708375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nbuilding = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\nweath_train = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')\n# weath_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\ntrain = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\n# test = pd.read_csv('../input/ashrae-energy-prediction/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:16.148679Z","iopub.execute_input":"2022-06-24T05:38:16.149334Z","iopub.status.idle":"2022-06-24T05:38:32.645900Z","shell.execute_reply.started":"2022-06-24T05:38:16.149294Z","shell.execute_reply":"2022-06-24T05:38:32.644756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA\n- 결측치 확인 및 채움","metadata":{}},{"cell_type":"code","source":"weath_train.isnull().sum()\n## 결측치 60%이상 - cloud_coverage 제외 (50%기준)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:32.647832Z","iopub.execute_input":"2022-06-24T05:38:32.648480Z","iopub.status.idle":"2022-06-24T05:38:32.690254Z","shell.execute_reply.started":"2022-06-24T05:38:32.648442Z","shell.execute_reply":"2022-06-24T05:38:32.689299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preproposal(weath_train):\n    # 날짜별 분류 -> 날짜별 결측치에 mean값으로 채움\n    weath_train[\"datetime\"] = pd.to_datetime(weath_train[\"timestamp\"])\n    weath_train[\"day\"] = weath_train[\"datetime\"].dt.day\n    weath_train[\"week\"] = weath_train[\"datetime\"].dt.week\n    weath_train[\"month\"] = weath_train[\"datetime\"].dt.month\n    weath_train = weath_train.set_index(['site_id','day','month'])  \n\n    # air_temperature\n    air_temperature_fill = pd.DataFrame(weath_train.groupby(['site_id','day','month'])['air_temperature'].mean(),columns=[\"air_temperature\"])\n    weath_train.update(air_temperature_fill,overwrite=False)  \n    # cloud_coverage\n    cloud_coverage_fill = weath_train.groupby(['site_id','day','month'])['cloud_coverage'].mean()\n    cloud_coverage_fill = pd.DataFrame(cloud_coverage_fill.fillna(method='ffill'),columns=[\"cloud_coverage\"])\n    weath_train.update(cloud_coverage_fill,overwrite=False)\n    #dew_temperature\n    due_temperature_fill = pd.DataFrame(weath_train.groupby(['site_id','day','month'])['dew_temperature'].mean(),columns=[\"dew_temperature\"])\n    weath_train.update(due_temperature_fill,overwrite=False)\n    #sea_level_pressure\n    sea_level_fill = weath_train.groupby(['site_id','day','month'])['sea_level_pressure'].mean()\n    sea_level_fill = pd.DataFrame(sea_level_fill.fillna(method='ffill'),columns=['sea_level_pressure'])\n    weath_train.update(sea_level_fill,overwrite=False)\n    #wind_direction\n    wind_direction_fill =  pd.DataFrame(weath_train.groupby(['site_id','day','month'])['wind_direction'].mean(),columns=['wind_direction'])\n    weath_train.update(wind_direction_fill,overwrite=False)\n    #wind_speed\n    wind_speed_fill =  pd.DataFrame(weath_train.groupby(['site_id','day','month'])['wind_speed'].mean(),columns=['wind_speed'])\n    weath_train.update(wind_speed_fill,overwrite=False)\n    #precip_depth_1_hr\n    precip_depth_fill = weath_train.groupby(['site_id','day','month'])['precip_depth_1_hr'].mean()\n    precip_depth_fill = pd.DataFrame(precip_depth_fill.fillna(method='ffill'),columns=['precip_depth_1_hr'])\n    weath_train.update(precip_depth_fill,overwrite=False)\n\n    weath_train = weath_train.reset_index()\n    weath_train = weath_train.drop(['datetime','day','week','month'],axis=1)\n    return weath_train","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:48.920214Z","iopub.execute_input":"2022-06-24T05:38:48.920635Z","iopub.status.idle":"2022-06-24T05:38:48.941831Z","shell.execute_reply.started":"2022-06-24T05:38:48.920595Z","shell.execute_reply":"2022-06-24T05:38:48.940939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df, use_float16=False):\n    \"\"\"\n    Iterate through all the columns of a dataframe and modify the data type to reduce memory usage.        \n    \"\"\"\n    \n    start_mem = df.memory_usage().sum() / 1024**2\n    print(\"Memory usage of dataframe is {:.2f} MB\".format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        \n        if col_type != object:\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 use_float16 and 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        else:\n            df[col] = df[col].astype(\"category\")\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","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:49.269246Z","iopub.execute_input":"2022-06-24T05:38:49.269670Z","iopub.status.idle":"2022-06-24T05:38:49.289177Z","shell.execute_reply.started":"2022-06-24T05:38:49.269633Z","shell.execute_reply":"2022-06-24T05:38:49.288162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weath_train = preproposal(weath_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:49.600180Z","iopub.execute_input":"2022-06-24T05:38:49.601292Z","iopub.status.idle":"2022-06-24T05:38:52.809307Z","shell.execute_reply.started":"2022-06-24T05:38:49.601251Z","shell.execute_reply":"2022-06-24T05:38:52.808149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weath_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:52.811476Z","iopub.execute_input":"2022-06-24T05:38:52.811857Z","iopub.status.idle":"2022-06-24T05:38:52.836600Z","shell.execute_reply.started":"2022-06-24T05:38:52.811819Z","shell.execute_reply":"2022-06-24T05:38:52.835432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:52.839723Z","iopub.execute_input":"2022-06-24T05:38:52.840535Z","iopub.status.idle":"2022-06-24T05:38:54.915809Z","shell.execute_reply.started":"2022-06-24T05:38:52.840495Z","shell.execute_reply":"2022-06-24T05:38:54.914870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:54.919050Z","iopub.execute_input":"2022-06-24T05:38:54.919324Z","iopub.status.idle":"2022-06-24T05:38:54.928016Z","shell.execute_reply.started":"2022-06-24T05:38:54.919300Z","shell.execute_reply":"2022-06-24T05:38:54.927056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## floor_count, square_feet와의 상관계수가 높으므로 square_feet만 사용.\ncorr = building.loc[:,['floor_count','square_feet']].corr()\nprint(corr)\ngc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:54.929640Z","iopub.execute_input":"2022-06-24T05:38:54.930307Z","iopub.status.idle":"2022-06-24T05:38:55.047133Z","shell.execute_reply.started":"2022-06-24T05:38:54.930247Z","shell.execute_reply":"2022-06-24T05:38:55.046194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 전처리\n1. Categorical 수정\n2. Data merge \n3. 시간순으로 정렬\n4. 시간/week day 추가\n5. square_feet log 변환\n6. 사용하지 않는 feature drop\n","metadata":{}},{"cell_type":"code","source":"## Categorical 수정\nbuilding['primary_use'] = pd.factorize(building['primary_use'])[0]","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:55.048851Z","iopub.execute_input":"2022-06-24T05:38:55.049264Z","iopub.status.idle":"2022-06-24T05:38:55.056033Z","shell.execute_reply.started":"2022-06-24T05:38:55.049228Z","shell.execute_reply":"2022-06-24T05:38:55.053983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building = reduce_mem_usage(building, use_float16=True)\ngc.collect()\ntrain = reduce_mem_usage(train, use_float16=True)\ngc.collect()\nweath_train = reduce_mem_usage(weath_train, use_float16=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:55.057313Z","iopub.execute_input":"2022-06-24T05:38:55.058105Z","iopub.status.idle":"2022-06-24T05:38:57.538331Z","shell.execute_reply.started":"2022-06-24T05:38:55.058067Z","shell.execute_reply":"2022-06-24T05:38:57.537120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## data merge\n## 1. traing + building\nmerge_df = pd.merge(train, building, on = 'building_id', how='left')\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:38:57.540256Z","iopub.execute_input":"2022-06-24T05:38:57.540630Z","iopub.status.idle":"2022-06-24T05:39:00.453830Z","shell.execute_reply.started":"2022-06-24T05:38:57.540592Z","shell.execute_reply":"2022-06-24T05:39:00.452912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:00.455143Z","iopub.execute_input":"2022-06-24T05:39:00.455563Z","iopub.status.idle":"2022-06-24T05:39:00.818900Z","shell.execute_reply.started":"2022-06-24T05:39:00.455528Z","shell.execute_reply":"2022-06-24T05:39:00.817877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##2. trian,builing + weather\nmerge_df = pd.merge(merge_df, weath_train, on = ['site_id', 'timestamp'], how='left')\ngc.collect()\nmerge_df","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:00.823050Z","iopub.execute_input":"2022-06-24T05:39:00.823328Z","iopub.status.idle":"2022-06-24T05:39:04.963347Z","shell.execute_reply.started":"2022-06-24T05:39:00.823303Z","shell.execute_reply":"2022-06-24T05:39:04.962411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(12,6))\n# sns.lineplot(data=merge_df.iloc[:2000], x=\"timestamp\", y=\"air_temperature\")\n# plt.show()\nx = merge_df.groupby('timestamp')['air_temperature'].sum()\nx.plot()\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:04.967985Z","iopub.execute_input":"2022-06-24T05:39:04.970406Z","iopub.status.idle":"2022-06-24T05:39:05.561771Z","shell.execute_reply.started":"2022-06-24T05:39:04.970356Z","shell.execute_reply":"2022-06-24T05:39:05.560895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_interp = merge_df.loc[:, ['air_temperature', 'dew_temperature','precip_depth_1_hr', 'sea_level_pressure', 'wind_direction','wind_speed']]\n\nfor i in merge_interp.columns:\n    merge_df[i] = merge_df[i].interpolate(method = 'linear')\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:05.563369Z","iopub.execute_input":"2022-06-24T05:39:05.563769Z","iopub.status.idle":"2022-06-24T05:39:13.557600Z","shell.execute_reply.started":"2022-06-24T05:39:05.563726Z","shell.execute_reply":"2022-06-24T05:39:13.556646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = merge_df.groupby('timestamp')['air_temperature'].sum()\nx.plot()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:13.558882Z","iopub.execute_input":"2022-06-24T05:39:13.559344Z","iopub.status.idle":"2022-06-24T05:39:14.083326Z","shell.execute_reply.started":"2022-06-24T05:39:13.559305Z","shell.execute_reply":"2022-06-24T05:39:14.082450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 전체 상관계수 확인.\ncorr = merge_df.drop(columns='meter_reading').corrwith(merge_df['meter_reading'])\ncorr.sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:14.084872Z","iopub.execute_input":"2022-06-24T05:39:14.085237Z","iopub.status.idle":"2022-06-24T05:39:20.855920Z","shell.execute_reply.started":"2022-06-24T05:39:14.085201Z","shell.execute_reply":"2022-06-24T05:39:20.854910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_eng(merge_df) :\n    #전처리 시간 순으로 정렬\n    merge_df.sort_values(\"timestamp\")\n    merge_df.reset_index(drop=True)\n\n    # Add features\n    merge_df[\"timestamp\"] = pd.to_datetime(merge_df[\"timestamp\"],format=\"%Y-%m-%d %H:%M:%S\")\n    merge_df[\"weekday\"] = merge_df[\"timestamp\"].dt.weekday\n    merge_df[\"hour\"] = merge_df[\"timestamp\"].dt.hour\n    merge_df[\"month\"] = merge_df[\"timestamp\"].dt.month\n    merge_df['square_feet'] =  np.log1p(merge_df['square_feet'])\n    merge_df['meter_reading'] =  np.log1p(merge_df['meter_reading'])\n    ## 주의 할점 min/max -- log변환 차이(분포가 바뀜)\n\n    drop = ['timestamp', 'year_built', 'floor_count', 'cloud_coverage']\n    merge_df = merge_df.drop(drop, axis=1)\n    gc.collect()\n    return merge_df\n\ndef feature_eng_test(merge_df) :\n    #전처리 시간 순으로 정렬\n    merge_df.sort_values(\"timestamp\")\n    merge_df.reset_index(drop=True)\n\n    # Add features\n    merge_df[\"timestamp\"] = pd.to_datetime(merge_df[\"timestamp\"],format=\"%Y-%m-%d %H:%M:%S\")\n    merge_df[\"weekday\"] = merge_df[\"timestamp\"].dt.weekday\n    merge_df[\"hour\"] = merge_df[\"timestamp\"].dt.hour\n    merge_df[\"month\"] = merge_df[\"timestamp\"].dt.month\n    merge_df['square_feet'] =  np.log1p(merge_df['square_feet'])\n#     merge_df['meter_reading'] =  np.log1p(merge_df['meter_reading'])\n    ## 주의 할점 min/max -- log변환 차이(분포가 바뀜)\n\n    drop = ['timestamp', 'year_built', 'floor_count', 'cloud_coverage']\n    merge_df = merge_df.drop(drop, axis=1)\n    gc.collect()\n    return merge_df","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:20.857370Z","iopub.execute_input":"2022-06-24T05:39:20.857924Z","iopub.status.idle":"2022-06-24T05:39:20.869000Z","shell.execute_reply.started":"2022-06-24T05:39:20.857886Z","shell.execute_reply":"2022-06-24T05:39:20.868069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df= feature_eng(merge_df)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:20.870200Z","iopub.execute_input":"2022-06-24T05:39:20.871021Z","iopub.status.idle":"2022-06-24T05:39:31.966633Z","shell.execute_reply.started":"2022-06-24T05:39:20.870986Z","shell.execute_reply":"2022-06-24T05:39:31.965648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = merge_df.drop(columns='meter_reading').corrwith(merge_df['meter_reading'])\ncorr.sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:31.968101Z","iopub.execute_input":"2022-06-24T05:39:31.968675Z","iopub.status.idle":"2022-06-24T05:39:38.293873Z","shell.execute_reply.started":"2022-06-24T05:39:31.968636Z","shell.execute_reply":"2022-06-24T05:39:38.292966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:38.295242Z","iopub.execute_input":"2022-06-24T05:39:38.296196Z","iopub.status.idle":"2022-06-24T05:39:38.308145Z","shell.execute_reply.started":"2022-06-24T05:39:38.296156Z","shell.execute_reply":"2022-06-24T05:39:38.307037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df.isnull().sum()\n# optuna는 점심시간에\n# lgbm gpu로 도는지 체크","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:38.309883Z","iopub.execute_input":"2022-06-24T05:39:38.310558Z","iopub.status.idle":"2022-06-24T05:39:39.046001Z","shell.execute_reply.started":"2022-06-24T05:39:38.310521Z","shell.execute_reply":"2022-06-24T05:39:39.044937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df = reduce_mem_usage(merge_df, use_float16=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:39.047368Z","iopub.execute_input":"2022-06-24T05:39:39.047994Z","iopub.status.idle":"2022-06-24T05:39:43.832413Z","shell.execute_reply.started":"2022-06-24T05:39:39.047941Z","shell.execute_reply":"2022-06-24T05:39:43.831409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## training","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor\nfrom lightgbm.sklearn import LGBMRegressor\n\nparam_grid = {\n    \"max_bin\" : 20,\n    \"learning_rate\" : 0.01,\n    \"objective\" : \"regression\",\n    \"boosting_type\" : \"gbdt\",\n    \"metric\" : \"mae\",\n    \"sub_feature\" : 0.345,\n    \"bagging_fraction\" : 0.85,\n    \"bagging_freq\" : 40,\n    \"num_leaves\" : 512,\n    \"min_data\" : 500,\n    \"min_hessian\" : 0.05,\n    \"verbose\" : 2,\n    \"feature_fraction_seed\" : 2,\n    \"bagging_seed\" : 3,\n    'device' : 'gpu'\n}\n\nmodel = LGBMRegressor(**param_grid)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:43.833727Z","iopub.execute_input":"2022-06-24T05:39:43.834631Z","iopub.status.idle":"2022-06-24T05:39:46.022971Z","shell.execute_reply.started":"2022-06-24T05:39:43.834591Z","shell.execute_reply":"2022-06-24T05:39:46.021918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 첫번째 테스트용으로 사용하고, 실제 학습시에는 K-Fold CV를 사용합니다.\nfrom sklearn.model_selection import train_test_split\n\nX = merge_df.drop(columns=['meter_reading'])  # 학습에 사용하지 않을 column들을 drop.\ny = merge_df.meter_reading\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0xC0FFEE)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:46.027806Z","iopub.execute_input":"2022-06-24T05:39:46.028098Z","iopub.status.idle":"2022-06-24T05:39:55.077315Z","shell.execute_reply.started":"2022-06-24T05:39:46.028072Z","shell.execute_reply":"2022-06-24T05:39:55.076313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nFitting LightGBM...\")\nmodel.fit(X_train, y_train)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:39:55.078612Z","iopub.execute_input":"2022-06-24T05:39:55.079227Z","iopub.status.idle":"2022-06-24T05:46:21.165888Z","shell.execute_reply.started":"2022-06-24T05:39:55.079188Z","shell.execute_reply":"2022-06-24T05:46:21.164892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## prediction","metadata":{}},{"cell_type":"code","source":"# metric은 그때마다 맞게 바꿔줘야 합니다.\n##RMSLE 설정\nfrom sklearn.metrics import mean_squared_log_error\n\nevaluation_metric = mean_squared_log_error\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:46:21.167342Z","iopub.execute_input":"2022-06-24T05:46:21.167699Z","iopub.status.idle":"2022-06-24T05:46:21.173201Z","shell.execute_reply.started":"2022-06-24T05:46:21.167664Z","shell.execute_reply":"2022-06-24T05:46:21.172194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmsle(y,y1):\n    return np.sqrt(np.mean(np.square(np.log1p(y)-np.log1p(y1))))","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:46:21.174657Z","iopub.execute_input":"2022-06-24T05:46:21.175751Z","iopub.status.idle":"2022-06-24T05:46:21.184395Z","shell.execute_reply.started":"2022-06-24T05:46:21.175712Z","shell.execute_reply":"2022-06-24T05:46:21.183402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Prediction\")\npred_train = model.predict(X_train)\ngc.collect()\npred_test = model.predict(X_test)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:46:21.186246Z","iopub.execute_input":"2022-06-24T05:46:21.187113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_score =rmsle(y_train, pred_train)\ntest_score = rmsle(y_test, pred_test)\nprint(\"Train Score : %.4f\" % train_score)\nprint(\"Test Score : %.4f\" % test_score)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_train\n# np.exp(pred_train)-1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## hyper-parameter tuning","metadata":{}},{"cell_type":"code","source":"# from sklearn.model_selection import GridSearchCV\n\n# param_grid = {\n#     \"max_depth\" : [8, 16, None],\n#     \"n_estimators\" : [100, 300, 500],\n#     \"max_bin\" : [20],\n#     \"learning_rate\" : [0.001, 0.0025, 0.003],\n#     \"objective\" : [\"regression\"],\n#     \"boosting_type\" : [\"gbdt\"],\n#     \"metric\" : [\"mae\"],\n#     \"sub_feature\" : [0.345],\n#     \"bagging_fraction\" : [0.7, 0.75, 0.85],\n#     \"bagging_freq\" : [40],\n#     \"num_leaves\" : [256, 512],\n#     \"min_data\" : [500],\n#     \"verbose\" : [-1], # 필수\n#     \"min_hessian\" : [0.05],\n#     \"feature_fraction_seed\" : [2],\n#     \"bagging_seed\" : [3]\n# }  # 3 x 3 x 3 x 3 x 2 = 162 ---> GridSearch가 탐색하는 Hyper-parameter 조합 수.\n\n# # CV(Cross-Validation) : KFold(K등분)\n# gcv = GridSearchCV(estimator=model, param_grid=param_grid, cv=5,\n#                   n_jobs=-1, verbose=1)\n\n# gcv.fit(X_train, y_train)  # 5 x 162 = 810.\n# print(\"Best Estimator : \", gcv.best_estimator_) \n# gc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"Prediction with Best Estimator\")\n# gcv_pred_train = gcv.predict(X_train)\n# gc.collect()\n# gcv_pred_test = gcv.predict(X_test)\n# gc.collect()\n\n# gcv_train_score = rmsle(y_train, gcv_pred_train)\n# gcv_test_score = rmsle(y_test, gcv_pred_test)\n\n# print(\"Train R2 Score : %.4f\" % gcv_train_score)\n# print(\"Test R2 Score : %.4f\" % gcv_test_score)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"Performance Gain\") # 이걸로 성능 향상 확인.\n# print(\"in train : \", -(train_score - gcv_train_score))\n# print(\"in test : \", -(test_score - gcv_test_score))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def optimizer(trial, X, y, K):\n    # 조절할 hyper-parameter 조합을 적어줍니다.\n#     n_estimators = trial.suggest_int('n_estimators', 100, 200)\n#     max_depth = trial.suggest_int('max_depth', 4, 8)\n#     max_features = trial.suggest_categorical('max_features', ['sqrt', 'auto', 'log2'])\n    \n    \n#     # 원하는 모델을 지정합니다, optuna는 시간이 오래걸리기 때문에 저는 보통 RF로 일단 테스트를 해본 뒤에 LGBM을 사용합니다.\n#     model = RandomForestRegressor(n_estimators=n_estimators,\n#                                  max_depth=max_depth,\n#                                  max_features=max_features)\n    \n    max_depth = trial.suggest_int('max_depth', 5, 20)\n    n_estimators = trial.suggest_int('n_estimators', 50, 200)\n    max_bin = trial.suggest_int('max_bin', 20,100)\n    learning_rate = trial.suggest_float('learning_rate', 0.001, 0.01)\n \n    model = LGBMRegressor(max_depth=max_depth,\n                          n_estimators=n_estimators,\n                          max_bin=max_bin,\n                          learning_rate=learning_rate,\n                         device='gpu')\n\n    # K-Fold Cross validation을 구현합니다.\n    folds = KFold(n_splits=K)\n    losses = []\n    \n    for train_idx, val_idx in folds.split(X, y):\n        X_train = X.iloc[train_idx, :]\n        y_train = y.iloc[train_idx]\n        \n        X_val = X.iloc[val_idx, :]\n        y_val = y.iloc[val_idx]\n        \n        model.fit(X_train, y_train)\n        preds = model.predict(X_val)\n        loss = rmsle(y_val, preds)\n        losses.append(loss)\n    \n    \n    # K-Fold의 평균 loss값을 돌려줍니다.\n    return np.mean(losses)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:00:29.580081Z","iopub.execute_input":"2022-06-24T05:00:29.581087Z","iopub.status.idle":"2022-06-24T05:00:29.592050Z","shell.execute_reply.started":"2022-06-24T05:00:29.581039Z","shell.execute_reply":"2022-06-24T05:00:29.590640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom functools import partial\nimport optuna\nK = 5 # Kfold 수\nopt_func = partial(optimizer, X=X_train, y=y_train, K=K)\n\nstudy = optuna.create_study(study_name = 'LGMB',direction=\"minimize\") # 최소/최대 어느 방향의 최적값을 구할 건지. # R2 score는 커질수록 좋음.\nstudy.optimize(opt_func, n_trials=10)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:06:25.452431Z","iopub.execute_input":"2022-06-24T05:06:25.452820Z","iopub.status.idle":"2022-06-24T05:27:29.645972Z","shell.execute_reply.started":"2022-06-24T05:06:25.452784Z","shell.execute_reply":"2022-06-24T05:27:29.644627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:28:50.808188Z","iopub.execute_input":"2022-06-24T05:28:50.808548Z","iopub.status.idle":"2022-06-24T05:28:50.937705Z","shell.execute_reply.started":"2022-06-24T05:28:50.808517Z","shell.execute_reply":"2022-06-24T05:28:50.936637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optuna가 시도했던 모든 실험 관련 데이터\nstudy.trials_dataframe()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:28:52.318860Z","iopub.execute_input":"2022-06-24T05:28:52.319220Z","iopub.status.idle":"2022-06-24T05:28:52.337147Z","shell.execute_reply.started":"2022-06-24T05:28:52.319188Z","shell.execute_reply":"2022-06-24T05:28:52.336011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Best Score: %.4f\" % study.best_value) # best score 출력\nprint(\"Best params: \", study.best_trial.params) # best score일 때의 하이퍼파라미터들","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:29:00.744512Z","iopub.execute_input":"2022-06-24T05:29:00.745419Z","iopub.status.idle":"2022-06-24T05:29:00.752009Z","shell.execute_reply.started":"2022-06-24T05:29:00.745371Z","shell.execute_reply":"2022-06-24T05:29:00.750924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 제출","metadata":{}},{"cell_type":"code","source":"## test 전처리\nweath_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\ntest = pd.read_csv('../input/ashrae-energy-prediction/test.csv')\nweath_test = preproposal(weath_test)\ngc.collect()\nweath_test = reduce_mem_usage(weath_test, use_float16=True)\ngc.collect()\ntest = reduce_mem_usage(test, use_float16=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:29:09.016800Z","iopub.execute_input":"2022-06-24T05:29:09.017525Z","iopub.status.idle":"2022-06-24T05:29:48.559885Z","shell.execute_reply.started":"2022-06-24T05:29:09.017483Z","shell.execute_reply":"2022-06-24T05:29:48.558814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\nmerge_df = pd.merge(test, building, on = 'building_id', how='left')\n##2. trian,builing + weather\nmerge_df = pd.merge(merge_df, weath_test, on = ['site_id', 'timestamp'], how='left')\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:29:48.561606Z","iopub.execute_input":"2022-06-24T05:29:48.562486Z","iopub.status.idle":"2022-06-24T05:30:03.505819Z","shell.execute_reply.started":"2022-06-24T05:29:48.562448Z","shell.execute_reply":"2022-06-24T05:30:03.504757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_interp = merge_df.loc[:, ['air_temperature', 'dew_temperature','precip_depth_1_hr', 'sea_level_pressure', 'wind_direction','wind_speed']]\n\nfor i in merge_interp.columns:\n    merge_df[i] = merge_df[i].interpolate(method = 'linear')\ngc.collect()\n\nmerge_df= feature_eng_test(merge_df)\ngc.collect()\nmerge_df = reduce_mem_usage(merge_df, use_float16=True)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T05:32:22.267509Z","iopub.execute_input":"2022-06-24T05:32:22.268020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = merge_df.drop(columns='meter_reading')\ngc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LGBMRegressor(max_depth=study.best_trial.params['max_depth'],\n                      n_estimators=study.best_trial.params['n_estimators'],\n                      max_bin=study.best_trial.params['max_bin'],\n                      learning_rate=study.best_trial.params['learning_rate'])\n\nmodel.fit(X, y) # 전체 데이터로 학습\npreds = model.predict(X_test)\npreds","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}