{"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":"markdown","source":"# 6장 자전거 대여 수요 예측 경진대회 환경 세팅된 노트북 양식","metadata":{"papermill":{"duration":0.026639,"end_time":"2021-08-16T04:01:01.662249","exception":false,"start_time":"2021-08-16T04:01:01.63561","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ndata_path = '/kaggle/input/bike-sharing-demand/'\n\ntrain = pd.read_csv(data_path + 'train.csv')\ntest = pd.read_csv(data_path + 'test.csv')\nsubmission = pd.read_csv(data_path + 'sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:30.857334Z","iopub.execute_input":"2022-07-24T12:13:30.857657Z","iopub.status.idle":"2022-07-24T12:13:30.931589Z","shell.execute_reply.started":"2022-07-24T12:13:30.857625Z","shell.execute_reply":"2022-07-24T12:13:30.930510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:30.933587Z","iopub.execute_input":"2022-07-24T12:13:30.933887Z","iopub.status.idle":"2022-07-24T12:13:30.943454Z","shell.execute_reply.started":"2022-07-24T12:13:30.933849Z","shell.execute_reply":"2022-07-24T12:13:30.942492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:30.944893Z","iopub.execute_input":"2022-07-24T12:13:30.945479Z","iopub.status.idle":"2022-07-24T12:13:30.969251Z","shell.execute_reply.started":"2022-07-24T12:13:30.945439Z","shell.execute_reply":"2022-07-24T12:13:30.968494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:30.971387Z","iopub.execute_input":"2022-07-24T12:13:30.972108Z","iopub.status.idle":"2022-07-24T12:13:30.987169Z","shell.execute_reply.started":"2022-07-24T12:13:30.972066Z","shell.execute_reply":"2022-07-24T12:13:30.986285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:30.988152Z","iopub.execute_input":"2022-07-24T12:13:30.988792Z","iopub.status.idle":"2022-07-24T12:13:31.019338Z","shell.execute_reply.started":"2022-07-24T12:13:30.988763Z","shell.execute_reply":"2022-07-24T12:13:31.018492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100])\nprint(train['datetime'][100].split())\nprint(train['datetime'][100].split()[0])\nprint(train['datetime'][100].split()[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.021541Z","iopub.execute_input":"2022-07-24T12:13:31.022005Z","iopub.status.idle":"2022-07-24T12:13:31.029586Z","shell.execute_reply.started":"2022-07-24T12:13:31.021961Z","shell.execute_reply":"2022-07-24T12:13:31.028788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100].split()[0])\nprint(train['datetime'][100].split()[0].split(\"-\"))\nprint(train['datetime'][100].split()[0].split(\"-\")[0])\nprint(train['datetime'][100].split()[0].split(\"-\")[1])\nprint(train['datetime'][100].split()[0].split(\"-\")[2])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.030747Z","iopub.execute_input":"2022-07-24T12:13:31.030970Z","iopub.status.idle":"2022-07-24T12:13:31.043204Z","shell.execute_reply.started":"2022-07-24T12:13:31.030944Z","shell.execute_reply":"2022-07-24T12:13:31.041746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train['datetime'][100].split()[1])\nprint(train['datetime'][100].split()[1].split(\":\"))\nprint(train['datetime'][100].split()[1].split(\":\")[0])\nprint(train['datetime'][100].split()[1].split(\":\")[1])\nprint(train['datetime'][100].split()[1].split(\":\")[2])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.044984Z","iopub.execute_input":"2022-07-24T12:13:31.045288Z","iopub.status.idle":"2022-07-24T12:13:31.054204Z","shell.execute_reply.started":"2022-07-24T12:13:31.045249Z","shell.execute_reply":"2022-07-24T12:13:31.053437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['date']=train['datetime'].apply(lambda x: x.split()[0])\n\ntrain['year']=train['datetime'].apply(lambda x: x.split()[0].split(\"-\")[0])\ntrain['month']=train['datetime'].apply(lambda x: x.split()[0].split(\"-\")[1])\ntrain['day']=train['datetime'].apply(lambda x: x.split()[0].split(\"-\")[2])\ntrain['hour']=train['datetime'].apply(lambda x: x.split()[1].split(\":\")[0])\ntrain['minute']=train['datetime'].apply(lambda x: x.split()[1].split(\":\")[1])\ntrain['second']=train['datetime'].apply(lambda x: x.split()[1].split(\":\")[2])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.055512Z","iopub.execute_input":"2022-07-24T12:13:31.055906Z","iopub.status.idle":"2022-07-24T12:13:31.128389Z","shell.execute_reply.started":"2022-07-24T12:13:31.055877Z","shell.execute_reply":"2022-07-24T12:13:31.127539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nimport calendar\n\nprint(train['date'][100])\nprint(datetime.strptime(train['date'][100], '%Y-%m-%d'))\nprint(datetime.strptime(train['date'][100], '%Y-%m-%d').weekday())\nprint(calendar.day_name[datetime.strptime(train['date'][100], '%Y-%m-%d').weekday()])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.130666Z","iopub.execute_input":"2022-07-24T12:13:31.130916Z","iopub.status.idle":"2022-07-24T12:13:31.138884Z","shell.execute_reply.started":"2022-07-24T12:13:31.130887Z","shell.execute_reply":"2022-07-24T12:13:31.138227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weekday']=train['date'].apply(\n    lambda dateString: calendar.day_name[datetime.strptime(dateString, '%Y-%m-%d').weekday()])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.140017Z","iopub.execute_input":"2022-07-24T12:13:31.140265Z","iopub.status.idle":"2022-07-24T12:13:31.337649Z","shell.execute_reply.started":"2022-07-24T12:13:31.140231Z","shell.execute_reply":"2022-07-24T12:13:31.336754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['season']=train['season'].map({1:'Spring', 2:'Summer', 3:'Fall', 4:'Winter'})\ntrain['weather']=train['weather'].map({1:'Clear', 2:'Mist, Few clouds', 3:'Light snow, Rain, Thunderstorm',\n                                      4:'Heavy Rain, Thunderstorm, Snow, Fog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.338820Z","iopub.execute_input":"2022-07-24T12:13:31.339052Z","iopub.status.idle":"2022-07-24T12:13:31.349569Z","shell.execute_reply.started":"2022-07-24T12:13:31.339019Z","shell.execute_reply":"2022-07-24T12:13:31.348835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.350519Z","iopub.execute_input":"2022-07-24T12:13:31.351074Z","iopub.status.idle":"2022-07-24T12:13:31.372525Z","shell.execute_reply.started":"2022-07-24T12:13:31.351045Z","shell.execute_reply":"2022-07-24T12:13:31.371696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:31.373718Z","iopub.execute_input":"2022-07-24T12:13:31.374178Z","iopub.status.idle":"2022-07-24T12:13:32.256727Z","shell.execute_reply.started":"2022-07-24T12:13:31.374150Z","shell.execute_reply":"2022-07-24T12:13:32.255815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=15)\nsns.displot(train['count'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:32.257911Z","iopub.execute_input":"2022-07-24T12:13:32.258157Z","iopub.status.idle":"2022-07-24T12:13:32.725354Z","shell.execute_reply.started":"2022-07-24T12:13:32.258131Z","shell.execute_reply":"2022-07-24T12:13:32.724468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(np.log(train['count']))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:13:32.726931Z","iopub.execute_input":"2022-07-24T12:13:32.727417Z","iopub.status.idle":"2022-07-24T12:13:33.120503Z","shell.execute_reply.started":"2022-07-24T12:13:32.727353Z","shell.execute_reply":"2022-07-24T12:13:33.119923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=14)\nmpl.rc('axes', titlesize=15)\nfigure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,9)\n\nsns.barplot(x='year', y='count', data=train, ax=axes[0, 0])\nsns.barplot(x='month', y='count', data=train, ax=axes[0, 1])\nsns.barplot(x='day', y='count', data=train, ax=axes[1, 0])\nsns.barplot(x='hour', y='count', data=train, ax=axes[1, 1])\nsns.barplot(x='minute', y='count', data=train, ax=axes[2, 0])\nsns.barplot(x='second', y='count', data=train, ax=axes[2, 1])\n\naxes[0,0].set(title='Rental amounts by year')\naxes[0,1].set(title='Rental amounts by month')\naxes[1,0].set(title='Rental amounts by day')\naxes[1,1].set(title='Rental amounts by hour')\naxes[2,0].set(title='Rental amounts by minute')\naxes[2,0].set(title='Rental amounts by second')\n\naxes[1,0].tick_params(axis='x', labelrotation=90)\naxes[1,1].tick_params(axis='x', labelrotation=90)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:18:40.408075Z","iopub.execute_input":"2022-07-24T12:18:40.408930Z","iopub.status.idle":"2022-07-24T12:18:44.020090Z","shell.execute_reply.started":"2022-07-24T12:18:40.408896Z","shell.execute_reply":"2022-07-24T12:18:44.019391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10,10)\n\nsns.boxplot(x='season', y='count', data=train, ax=axes[0,0])\nsns.boxplot(x='weather', y='count', data=train, ax=axes[0,1])\nsns.boxplot(x='holiday', y='count', data=train, ax=axes[1,0])\nsns.boxplot(x='workingday', y='count', data=train, ax=axes[1,1])\n\naxes[0,0].set(title='Box Plot On Count Across Season')\naxes[0,1].set(title='Box Plot On Count Across Weather')\naxes[1,0].set(title='Box Plot On Count Across Holiday')\naxes[1,1].set(title='Box Plot On Count Across Working Day')\n\naxes[0,1].tick_params(axis='x', labelrotation=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:22:54.992825Z","iopub.execute_input":"2022-07-24T12:22:54.993339Z","iopub.status.idle":"2022-07-24T12:22:55.769820Z","shell.execute_reply.started":"2022-07-24T12:22:54.993301Z","shell.execute_reply":"2022-07-24T12:22:55.768980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=11)\nfigure, axes = plt.subplots(nrows=5)\nfigure.set_size_inches(12,18)\n\nsns.pointplot(x='hour', y='count', data=train, hue='workingday', ax=axes[0])\nsns.pointplot(x='hour', y='count', data=train, hue='holiday', ax=axes[1])\nsns.pointplot(x='hour', y='count', data=train, hue='weekday', ax=axes[2])\nsns.pointplot(x='hour', y='count', data=train, hue='season', ax=axes[3])\nsns.pointplot(x='hour', y='count', data=train, hue='weather', ax=axes[4])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:28:37.865776Z","iopub.execute_input":"2022-07-24T12:28:37.866413Z","iopub.status.idle":"2022-07-24T12:28:55.509170Z","shell.execute_reply.started":"2022-07-24T12:28:37.866368Z","shell.execute_reply":"2022-07-24T12:28:55.508435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rc('font', size=15)\nfigure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(7,6)\n\nsns.regplot(x='temp', y='count', data=train, ax=axes[0,0], \n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='atemp', y='count', data=train, ax=axes[0,1], \n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='windspeed', y='count', data=train, ax=axes[1,0], \n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})\nsns.regplot(x='humidity', y='count', data=train, ax=axes[1,1], \n            scatter_kws={'alpha':0.2}, line_kws={'color':'blue'})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:39:01.208257Z","iopub.execute_input":"2022-07-24T12:39:01.208771Z","iopub.status.idle":"2022-07-24T12:39:04.255117Z","shell.execute_reply.started":"2022-07-24T12:39:01.208736Z","shell.execute_reply":"2022-07-24T12:39:04.254292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['temp','atemp','humidity', 'windspeed', 'count']].corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:42:17.277081Z","iopub.execute_input":"2022-07-24T12:42:17.277405Z","iopub.status.idle":"2022-07-24T12:42:17.298795Z","shell.execute_reply.started":"2022-07-24T12:42:17.277360Z","shell.execute_reply":"2022-07-24T12:42:17.298094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrMat = train[['temp','atemp','humidity', 'windspeed', 'count']].corr()\nfig, ax = plt.subplots()\nfig.set_size_inches(10, 10)\nsns.heatmap(corrMat, annot=True)\nax.set(title='Heatmap of Numerical Data')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:47.895101Z","iopub.execute_input":"2022-07-24T12:46:47.895432Z","iopub.status.idle":"2022-07-24T12:46:48.293256Z","shell.execute_reply.started":"2022-07-24T12:46:47.895392Z","shell.execute_reply":"2022-07-24T12:46:48.292408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"6.4 베이스라인 모델","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ndata_path = '/kaggle/input/bike-sharing-demand/'\n\ntrain = pd.read_csv(data_path + 'train.csv')\ntest = pd.read_csv(data_path + 'test.csv')\nsubmission = pd.read_csv(data_path + 'sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:25.255911Z","iopub.execute_input":"2022-07-24T13:58:25.256600Z","iopub.status.idle":"2022-07-24T13:58:25.306168Z","shell.execute_reply.started":"2022-07-24T13:58:25.256560Z","shell.execute_reply":"2022-07-24T13:58:25.305425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train['weather'] != 4]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:28.951266Z","iopub.execute_input":"2022-07-24T13:58:28.951544Z","iopub.status.idle":"2022-07-24T13:58:28.958247Z","shell.execute_reply.started":"2022-07-24T13:58:28.951517Z","shell.execute_reply":"2022-07-24T13:58:28.957575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = pd.concat([train, test], ignore_index=True)\nall_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:30.718961Z","iopub.execute_input":"2022-07-24T13:58:30.719276Z","iopub.status.idle":"2022-07-24T13:58:30.748956Z","shell.execute_reply.started":"2022-07-24T13:58:30.719245Z","shell.execute_reply":"2022-07-24T13:58:30.748167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\n\nall_data['date']=all_data['datetime'].apply(lambda x: x.split()[0])\nall_data['year']=all_data['datetime'].apply(lambda x: x.split()[0].split('-')[0])\nall_data['month']=all_data['datetime'].apply(lambda x: x.split()[0].split('-')[1])\nall_data['hour']=all_data['datetime'].apply(lambda x: x.split()[1].split(':')[0])\nall_data['weekday']=all_data['date'].apply(\n    lambda dateString: datetime.strptime(dateString, \"%Y-%m-%d\").weekday())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:33.325110Z","iopub.execute_input":"2022-07-24T13:58:33.325388Z","iopub.status.idle":"2022-07-24T13:58:33.596245Z","shell.execute_reply.started":"2022-07-24T13:58:33.325347Z","shell.execute_reply":"2022-07-24T13:58:33.595651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']\nall_data = all_data.drop(drop_features, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:36.415562Z","iopub.execute_input":"2022-07-24T13:58:36.416150Z","iopub.status.idle":"2022-07-24T13:58:36.426149Z","shell.execute_reply.started":"2022-07-24T13:58:36.416119Z","shell.execute_reply":"2022-07-24T13:58:36.425325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##훈련 데이터와 테스트 데이터 나누기\nX_train = all_data[~pd.isnull(all_data['count'])]\nX_test = all_data[pd.isnull(all_data['count'])]\n\n##타깃값 count 제거하기\nX_train = X_train.drop(['count'], axis=1)\nX_test = X_test.drop(['count'], axis=1)\n\ny = train['count']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:38.779435Z","iopub.execute_input":"2022-07-24T13:58:38.779972Z","iopub.status.idle":"2022-07-24T13:58:38.790603Z","shell.execute_reply.started":"2022-07-24T13:58:38.779937Z","shell.execute_reply":"2022-07-24T13:58:38.789786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:41.513118Z","iopub.execute_input":"2022-07-24T13:58:41.513826Z","iopub.status.idle":"2022-07-24T13:58:41.526666Z","shell.execute_reply.started":"2022-07-24T13:58:41.513789Z","shell.execute_reply":"2022-07-24T13:58:41.525941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ndef rmsle(y_true, y_pred, convertExp=True):\n    if convertExp:\n        y_true = np.exp(y_true)\n        y_pred = np.exp(y_pred)\n        \n    log_true = np.nan_to_num(np.log(y_true+1))\n    log_pred = np.nan_to_num(np.log(y_pred+1))\n        \n    output = np.sqrt(np.mean((log_true-log_pred)**2))\n    return output","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:58:43.524816Z","iopub.execute_input":"2022-07-24T13:58:43.525219Z","iopub.status.idle":"2022-07-24T13:58:43.531369Z","shell.execute_reply.started":"2022-07-24T13:58:43.525189Z","shell.execute_reply":"2022-07-24T13:58:43.530819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nlinear_reg_model = LinearRegression()\n\nlog_y = np.log(y)\nlinear_reg_model.fit(X_train, log_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:41:51.901646Z","iopub.execute_input":"2022-07-24T13:41:51.901926Z","iopub.status.idle":"2022-07-24T13:41:51.938766Z","shell.execute_reply.started":"2022-07-24T13:41:51.901898Z","shell.execute_reply":"2022-07-24T13:41:51.937772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = linear_reg_model.predict(X_train)\nprint(f'선형 회귀의 RMSLE 값: {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:51:33.725594Z","iopub.execute_input":"2022-07-24T13:51:33.726243Z","iopub.status.idle":"2022-07-24T13:51:33.732934Z","shell.execute_reply.started":"2022-07-24T13:51:33.726202Z","shell.execute_reply":"2022-07-24T13:51:33.732326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linearreg_preds = linear_reg_model.predict(X_test)\n\nsubmission['count'] = np.exp(linearreg_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T13:54:08.259071Z","iopub.execute_input":"2022-07-24T13:54:08.259358Z","iopub.status.idle":"2022-07-24T13:54:08.335351Z","shell.execute_reply.started":"2022-07-24T13:54:08.259320Z","shell.execute_reply":"2022-07-24T13:54:08.334110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import Ridge\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import metrics\n\nridge_model = Ridge()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:17:40.873201Z","iopub.execute_input":"2022-07-24T14:17:40.873502Z","iopub.status.idle":"2022-07-24T14:17:40.878855Z","shell.execute_reply.started":"2022-07-24T14:17:40.873472Z","shell.execute_reply":"2022-07-24T14:17:40.877900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_params = {'max_iter':[3000], \n                'alpha':[0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000]}\n\nrmsle_scorer = metrics.make_scorer(rmsle, greater_is_better = False)\ngridsearch_ridge_model = GridSearchCV(estimator=ridge_model,\n                                     param_grid = ridge_params,\n                                     scoring = rmsle_scorer,\n                                     cv=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:22:39.146415Z","iopub.execute_input":"2022-07-24T14:22:39.146861Z","iopub.status.idle":"2022-07-24T14:22:39.154277Z","shell.execute_reply.started":"2022-07-24T14:22:39.146820Z","shell.execute_reply":"2022-07-24T14:22:39.153499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_y = np.log(y)\ngridsearch_ridge_model.fit(X_train, log_y)\nprint('최적 하이퍼파라미터 :',gridsearch_ridge_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:31:44.270193Z","iopub.execute_input":"2022-07-24T14:31:44.270816Z","iopub.status.idle":"2022-07-24T14:31:46.638545Z","shell.execute_reply.started":"2022-07-24T14:31:44.270772Z","shell.execute_reply":"2022-07-24T14:31:46.637722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = gridsearch_ridge_model.best_estimator_.predict(X_train)\nprint(f'릿지 회귀 RMSLE 값: {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:35:10.250448Z","iopub.execute_input":"2022-07-24T14:35:10.250735Z","iopub.status.idle":"2022-07-24T14:35:10.282676Z","shell.execute_reply.started":"2022-07-24T14:35:10.250707Z","shell.execute_reply":"2022-07-24T14:35:10.281804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\n\nlasso_model = Lasso()\nlasso_alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000])\nlasso_params = {'max_iter':[3000], 'alpha':lasso_alpha}\n\ngridsearch_lasso_model = GridSearchCV(estimator=lasso_model,\n                                     param_grid = lasso_params,\n                                     scoring = rmsle_scorer,\n                                     cv=5)\n\nlog_y = np.log(y)\ngridsearch_lasso_model.fit(X_train, log_y)\nprint('최적 하이퍼파라미터 :',gridsearch_lasso_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:40:07.795298Z","iopub.execute_input":"2022-07-24T14:40:07.796065Z","iopub.status.idle":"2022-07-24T14:40:11.350099Z","shell.execute_reply.started":"2022-07-24T14:40:07.796006Z","shell.execute_reply":"2022-07-24T14:40:11.349160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = gridsearch_lasso_model.best_estimator_.predict(X_train)\nprint(f'라쏘 회귀 RMSLE 값: {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:40:53.475406Z","iopub.execute_input":"2022-07-24T14:40:53.475991Z","iopub.status.idle":"2022-07-24T14:40:53.502825Z","shell.execute_reply.started":"2022-07-24T14:40:53.475941Z","shell.execute_reply":"2022-07-24T14:40:53.501994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nrandomforest_model = RandomForestRegressor()\nrf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}\n\ngridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,\n                                     param_grid = rf_params,\n                                     scoring = rmsle_scorer,\n                                     cv=5)\n\nlog_y = np.log(y)\ngridsearch_random_forest_model.fit(X_train, log_y)\nprint('최적 하이퍼파라미터 :',gridsearch_random_forest_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:44:34.989316Z","iopub.execute_input":"2022-07-24T14:44:34.990079Z","iopub.status.idle":"2022-07-24T14:45:30.003677Z","shell.execute_reply.started":"2022-07-24T14:44:34.990024Z","shell.execute_reply":"2022-07-24T14:45:30.003074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = gridsearch_random_forest_model.best_estimator_.predict(X_train)\nprint(f'랜덤 포레스트 회귀 RMSLE 값: {rmsle(log_y, preds, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:46:55.998278Z","iopub.execute_input":"2022-07-24T14:46:55.999114Z","iopub.status.idle":"2022-07-24T14:46:56.297970Z","shell.execute_reply.started":"2022-07-24T14:46:55.999068Z","shell.execute_reply":"2022-07-24T14:46:56.296986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nrandomforest_preds = gridsearch_random_forest_model.best_estimator_.predict(X_test)\nfigure, axes = plt.subplots(ncols=2)\nfigure.set_size_inches(10, 4)\n\nsns.histplot(y, bins=50, ax=axes[0])\naxes[0].set_title('Train Data Distribution')\nsns.histplot(np.exp(randomforest_preds), bins=50, ax=axes[1])\naxes[1].set_title('Predicted Test Data Distribution');","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:50:38.152431Z","iopub.execute_input":"2022-07-24T14:50:38.152733Z","iopub.status.idle":"2022-07-24T14:50:38.916887Z","shell.execute_reply.started":"2022-07-24T14:50:38.152704Z","shell.execute_reply":"2022-07-24T14:50:38.915948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['count']=np.exp(randomforest_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T14:51:51.276731Z","iopub.execute_input":"2022-07-24T14:51:51.277015Z","iopub.status.idle":"2022-07-24T14:51:51.313400Z","shell.execute_reply.started":"2022-07-24T14:51:51.276987Z","shell.execute_reply":"2022-07-24T14:51:51.312477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}