{"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":"# 데이터 탐색","metadata":{}},{"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\ndata_path='/kaggle/input/bike-sharing-demand/'","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.338589Z","iopub.execute_input":"2022-07-23T14:42:38.339501Z","iopub.status.idle":"2022-07-23T14:42:38.344271Z","shell.execute_reply.started":"2022-07-23T14:42:38.339459Z","shell.execute_reply":"2022-07-23T14:42:38.343040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = 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-23T14:42:38.354765Z","iopub.execute_input":"2022-07-23T14:42:38.355289Z","iopub.status.idle":"2022-07-23T14:42:38.407341Z","shell.execute_reply.started":"2022-07-23T14:42:38.355243Z","shell.execute_reply":"2022-07-23T14:42:38.406602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.409033Z","iopub.execute_input":"2022-07-23T14:42:38.409358Z","iopub.status.idle":"2022-07-23T14:42:38.416134Z","shell.execute_reply.started":"2022-07-23T14:42:38.409318Z","shell.execute_reply":"2022-07-23T14:42:38.415220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.447690Z","iopub.execute_input":"2022-07-23T14:42:38.448200Z","iopub.status.idle":"2022-07-23T14:42:38.464982Z","shell.execute_reply.started":"2022-07-23T14:42:38.448154Z","shell.execute_reply":"2022-07-23T14:42:38.464129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.541971Z","iopub.execute_input":"2022-07-23T14:42:38.542275Z","iopub.status.idle":"2022-07-23T14:42:38.557223Z","shell.execute_reply.started":"2022-07-23T14:42:38.542245Z","shell.execute_reply":"2022-07-23T14:42:38.556370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.586784Z","iopub.execute_input":"2022-07-23T14:42:38.587498Z","iopub.status.idle":"2022-07-23T14:42:38.597651Z","shell.execute_reply.started":"2022-07-23T14:42:38.587453Z","shell.execute_reply":"2022-07-23T14:42:38.596941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:38.686065Z","iopub.execute_input":"2022-07-23T14:42:38.686542Z","iopub.status.idle":"2022-07-23T14:42:38.702563Z","shell.execute_reply.started":"2022-07-23T14:42:38.686486Z","shell.execute_reply":"2022-07-23T14:42:38.701432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 피쳐 엔지니어링","metadata":{}},{"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-23T14:42:38.795277Z","iopub.execute_input":"2022-07-23T14:42:38.795958Z","iopub.status.idle":"2022-07-23T14:42:38.803524Z","shell.execute_reply.started":"2022-07-23T14:42:38.795907Z","shell.execute_reply":"2022-07-23T14:42:38.802472Z"},"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-23T14:42:38.861697Z","iopub.execute_input":"2022-07-23T14:42:38.862101Z","iopub.status.idle":"2022-07-23T14:42:38.870176Z","shell.execute_reply.started":"2022-07-23T14:42:38.862053Z","shell.execute_reply":"2022-07-23T14:42:38.869489Z"},"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-23T14:42:38.906937Z","iopub.execute_input":"2022-07-23T14:42:38.907521Z","iopub.status.idle":"2022-07-23T14:42:38.916278Z","shell.execute_reply.started":"2022-07-23T14:42:38.907487Z","shell.execute_reply":"2022-07-23T14:42:38.915210Z"},"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-23T14:42:38.988099Z","iopub.execute_input":"2022-07-23T14:42:38.988688Z","iopub.status.idle":"2022-07-23T14:42:39.061952Z","shell.execute_reply.started":"2022-07-23T14:42:38.988650Z","shell.execute_reply":"2022-07-23T14:42:39.061188Z"},"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-23T14:42:39.063673Z","iopub.execute_input":"2022-07-23T14:42:39.064119Z","iopub.status.idle":"2022-07-23T14:42:39.073433Z","shell.execute_reply.started":"2022-07-23T14:42:39.064076Z","shell.execute_reply":"2022-07-23T14:42:39.072656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weekday']=train['date'].apply(lambda dateString: \n                                    calendar.day_name[datetime.strptime(dateString, \"%Y-%m-%d\").weekday()])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:39.101749Z","iopub.execute_input":"2022-07-23T14:42:39.102303Z","iopub.status.idle":"2022-07-23T14:42:39.295499Z","shell.execute_reply.started":"2022-07-23T14:42:39.102258Z","shell.execute_reply":"2022-07-23T14:42:39.294724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['season'] = train['season'].map({1: 'Spring',\n                                      2: 'Summer',\n                                      3: 'Fall',\n                                      4: 'Winter'})\ntrain['weather'] = train['weather'].map({1: 'Clear',\n                                        2: 'Mist, Few clouds',\n                                        3: 'Light Snow, Rain, Thunderstorm',\n                                        4: 'Heavy Rain, Thunderstorm, Snow, Fog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:39.298226Z","iopub.execute_input":"2022-07-23T14:42:39.298563Z","iopub.status.idle":"2022-07-23T14:42:39.308301Z","shell.execute_reply.started":"2022-07-23T14:42:39.298523Z","shell.execute_reply":"2022-07-23T14:42:39.307678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:39.309213Z","iopub.execute_input":"2022-07-23T14:42:39.309747Z","iopub.status.idle":"2022-07-23T14:42:39.338937Z","shell.execute_reply.started":"2022-07-23T14:42:39.309713Z","shell.execute_reply":"2022-07-23T14:42:39.338103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 데이터 시각화","metadata":{}},{"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-23T14:42:39.340589Z","iopub.execute_input":"2022-07-23T14:42:39.340840Z","iopub.status.idle":"2022-07-23T14:42:39.346711Z","shell.execute_reply.started":"2022-07-23T14:42:39.340813Z","shell.execute_reply":"2022-07-23T14:42:39.345880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### distribution plot","metadata":{}},{"cell_type":"code","source":"mpl.rc('font', size=15)\nsns.displot(train['count']);","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:39.348334Z","iopub.execute_input":"2022-07-23T14:42:39.348767Z","iopub.status.idle":"2022-07-23T14:42:39.754225Z","shell.execute_reply.started":"2022-07-23T14:42:39.348727Z","shell.execute_reply":"2022-07-23T14:42:39.753351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(np.log(train['count']))","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:39.756538Z","iopub.execute_input":"2022-07-23T14:42:39.757156Z","iopub.status.idle":"2022-07-23T14:42:40.155896Z","shell.execute_reply.started":"2022-07-23T14:42:39.757111Z","shell.execute_reply":"2022-07-23T14:42:40.154971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### barplot","metadata":{}},{"cell_type":"markdown","source":"#### (1) m행 n열 figure 준비","metadata":{}},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:40.157383Z","iopub.execute_input":"2022-07-23T14:42:40.157709Z","iopub.status.idle":"2022-07-23T14:42:40.923393Z","shell.execute_reply.started":"2022-07-23T14:42:40.157669Z","shell.execute_reply":"2022-07-23T14:42:40.922559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axes","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:40.925596Z","iopub.execute_input":"2022-07-23T14:42:40.926306Z","iopub.status.idle":"2022-07-23T14:42:40.932928Z","shell.execute_reply.started":"2022-07-23T14:42:40.926260Z","shell.execute_reply":"2022-07-23T14:42:40.932000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:40.934392Z","iopub.execute_input":"2022-07-23T14:42:40.934773Z","iopub.status.idle":"2022-07-23T14:42:41.564591Z","shell.execute_reply.started":"2022-07-23T14:42:40.934738Z","shell.execute_reply":"2022-07-23T14:42:41.563827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### (2) 각 축에 서브플롯 할당","metadata":{}},{"cell_type":"code","source":"sns.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])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:41.565847Z","iopub.execute_input":"2022-07-23T14:42:41.566151Z","iopub.status.idle":"2022-07-23T14:42:44.099256Z","shell.execute_reply.started":"2022-07-23T14:42:41.566119Z","shell.execute_reply":"2022-07-23T14:42:44.098406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:44.100561Z","iopub.execute_input":"2022-07-23T14:42:44.100891Z","iopub.status.idle":"2022-07-23T14:42:44.672577Z","shell.execute_reply.started":"2022-07-23T14:42:44.100851Z","shell.execute_reply":"2022-07-23T14:42:44.671583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### (3) (선택) 세부 설정","metadata":{}},{"cell_type":"code","source":"axes[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,1].set(title='Rental amounts by second')\nfigure","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:44.674209Z","iopub.execute_input":"2022-07-23T14:42:44.674527Z","iopub.status.idle":"2022-07-23T14:42:45.299720Z","shell.execute_reply.started":"2022-07-23T14:42:44.674487Z","shell.execute_reply":"2022-07-23T14:42:45.299113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axes[1,0].tick_params(axis='x', labelrotation=90)\naxes[1,1].tick_params(axis='x', labelrotation=90)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:45.300636Z","iopub.execute_input":"2022-07-23T14:42:45.301180Z","iopub.status.idle":"2022-07-23T14:42:45.324406Z","shell.execute_reply.started":"2022-07-23T14:42:45.301151Z","shell.execute_reply":"2022-07-23T14:42:45.323721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:45.327001Z","iopub.execute_input":"2022-07-23T14:42:45.327369Z","iopub.status.idle":"2022-07-23T14:42:45.962008Z","shell.execute_reply.started":"2022-07-23T14:42:45.327339Z","shell.execute_reply":"2022-07-23T14:42:45.961161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:green\">(ㄱ) 박스플롯</span>","metadata":{}},{"cell_type":"code","source":"# 스텝1 : m행 m열 figure 준비 \nfigure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(10, 10)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:45.963486Z","iopub.execute_input":"2022-07-23T14:42:45.963933Z","iopub.status.idle":"2022-07-23T14:42:46.581677Z","shell.execute_reply.started":"2022-07-23T14:42:45.963893Z","shell.execute_reply":"2022-07-23T14:42:46.580726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#스텝2 : 서브플롯 할당 \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\n#스텝3 : 세부 설정 \n# 3-1 : 서브플롯에 제목 달기 \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\n# 3-2 ; x축 라벨 겹침 해결 \naxes[0,1].tick_params(axis='x', labelrotation=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:46.583123Z","iopub.execute_input":"2022-07-23T14:42:46.583511Z","iopub.status.idle":"2022-07-23T14:42:46.742081Z","shell.execute_reply.started":"2022-07-23T14:42:46.583468Z","shell.execute_reply":"2022-07-23T14:42:46.741344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:46.743064Z","iopub.execute_input":"2022-07-23T14:42:46.743291Z","iopub.status.idle":"2022-07-23T14:42:47.114046Z","shell.execute_reply.started":"2022-07-23T14:42:46.743265Z","shell.execute_reply":"2022-07-23T14:42:47.113264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:green\">(ㄴ) 포인트플롯</span>","metadata":{}},{"cell_type":"code","source":"#스텝 1 : m행 n열 Figure 준비\nmpl.rc('font', size=11)\nfigure, axes = plt.subplots(nrows=5)\nfigure.set_size_inches(12, 18)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:42:47.115063Z","iopub.execute_input":"2022-07-23T14:42:47.115272Z","iopub.status.idle":"2022-07-23T14:42:47.783807Z","shell.execute_reply.started":"2022-07-23T14:42:47.115247Z","shell.execute_reply":"2022-07-23T14:42:47.782994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 스텝2 : 서브플롯 할당 \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-23T14:42:47.784926Z","iopub.execute_input":"2022-07-23T14:42:47.785153Z","iopub.status.idle":"2022-07-23T14:43:02.672621Z","shell.execute_reply.started":"2022-07-23T14:42:47.785127Z","shell.execute_reply":"2022-07-23T14:43:02.671818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:02.673911Z","iopub.execute_input":"2022-07-23T14:43:02.674139Z","iopub.status.idle":"2022-07-23T14:43:04.699468Z","shell.execute_reply.started":"2022-07-23T14:43:02.674112Z","shell.execute_reply":"2022-07-23T14:43:04.698395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:green\">(ㄷ) 회귀선을 포함한 산점도 그래프 </span>","metadata":{}},{"cell_type":"code","source":"#figure 준비 \nmpl.rc('font', size=15)\nfigure, axes = plt.subplots(nrows=2, ncols=2)\nplt.tight_layout()\nfigure.set_size_inches(7,6)\n\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-23T14:43:04.701392Z","iopub.execute_input":"2022-07-23T14:43:04.701665Z","iopub.status.idle":"2022-07-23T14:43:07.773705Z","shell.execute_reply.started":"2022-07-23T14:43:04.701630Z","shell.execute_reply":"2022-07-23T14:43:07.773022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:green\">(ㄹ) 히트맵 </span>","metadata":{}},{"cell_type":"code","source":"train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:07.774717Z","iopub.execute_input":"2022-07-23T14:43:07.775376Z","iopub.status.idle":"2022-07-23T14:43:07.793437Z","shell.execute_reply.started":"2022-07-23T14:43:07.775340Z","shell.execute_reply":"2022-07-23T14:43:07.792852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#피쳐 간 상관관계 매트릭스 \ncorrMat = 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-23T14:43:07.794562Z","iopub.execute_input":"2022-07-23T14:43:07.795286Z","iopub.status.idle":"2022-07-23T14:43:08.200245Z","shell.execute_reply.started":"2022-07-23T14:43:07.795246Z","shell.execute_reply":"2022-07-23T14:43:08.199349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 베이스라인 모델 ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ndata_path = '/kaggle/input/bike-sharing-demand/'","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.201560Z","iopub.execute_input":"2022-07-23T14:43:08.201834Z","iopub.status.idle":"2022-07-23T14:43:08.209339Z","shell.execute_reply.started":"2022-07-23T14:43:08.201799Z","shell.execute_reply":"2022-07-23T14:43:08.208348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = 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-23T14:43:08.210590Z","iopub.execute_input":"2022-07-23T14:43:08.210880Z","iopub.status.idle":"2022-07-23T14:43:08.265123Z","shell.execute_reply.started":"2022-07-23T14:43:08.210848Z","shell.execute_reply":"2022-07-23T14:43:08.264496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **feature engineering**","metadata":{}},{"cell_type":"markdown","source":"### get rid of ","metadata":{}},{"cell_type":"code","source":"train = train[train['weather']!=4]","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.266116Z","iopub.execute_input":"2022-07-23T14:43:08.266805Z","iopub.status.idle":"2022-07-23T14:43:08.272260Z","shell.execute_reply.started":"2022-07-23T14:43:08.266759Z","shell.execute_reply":"2022-07-23T14:43:08.271719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 데이터 합치기","metadata":{}},{"cell_type":"code","source":"all_data = pd.concat([train, test], ignore_index=True)\nall_data","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.274743Z","iopub.execute_input":"2022-07-23T14:43:08.275349Z","iopub.status.idle":"2022-07-23T14:43:08.310066Z","shell.execute_reply.started":"2022-07-23T14:43:08.275312Z","shell.execute_reply":"2022-07-23T14:43:08.309224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 파생 피쳐 추가","metadata":{}},{"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(lambda dateString: datetime.strptime(dateString, \"%Y-%m-%d\").weekday())","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.311317Z","iopub.execute_input":"2022-07-23T14:43:08.311554Z","iopub.status.idle":"2022-07-23T14:43:08.594297Z","shell.execute_reply.started":"2022-07-23T14:43:08.311529Z","shell.execute_reply":"2022-07-23T14:43:08.593399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 필요 없는 피쳐 제거","metadata":{}},{"cell_type":"code","source":"drop_features = ['casual', 'registered', 'datetime', 'date', 'month']\nall_data = all_data.drop(drop_features, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.595593Z","iopub.execute_input":"2022-07-23T14:43:08.596101Z","iopub.status.idle":"2022-07-23T14:43:08.607752Z","shell.execute_reply.started":"2022-07-23T14:43:08.596054Z","shell.execute_reply":"2022-07-23T14:43:08.606907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 데이터 나누기","metadata":{}},{"cell_type":"code","source":"X_train = all_data[~pd.isnull(all_data['count'])]\nX_test = all_data[pd.isnull(all_data['count'])]","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.609172Z","iopub.execute_input":"2022-07-23T14:43:08.609561Z","iopub.status.idle":"2022-07-23T14:43:08.620315Z","shell.execute_reply.started":"2022-07-23T14:43:08.609517Z","shell.execute_reply":"2022-07-23T14:43:08.619527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.drop(['count'], axis=1)\nX_test = X_test.drop(['count'], axis=1)\ny = train['count']","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.621752Z","iopub.execute_input":"2022-07-23T14:43:08.622215Z","iopub.status.idle":"2022-07-23T14:43:08.635514Z","shell.execute_reply.started":"2022-07-23T14:43:08.622172Z","shell.execute_reply":"2022-07-23T14:43:08.634781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.641016Z","iopub.execute_input":"2022-07-23T14:43:08.641773Z","iopub.status.idle":"2022-07-23T14:43:08.660704Z","shell.execute_reply.started":"2022-07-23T14:43:08.641734Z","shell.execute_reply":"2022-07-23T14:43:08.659898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.661936Z","iopub.execute_input":"2022-07-23T14:43:08.662357Z","iopub.status.idle":"2022-07-23T14:43:08.684202Z","shell.execute_reply.started":"2022-07-23T14:43:08.662328Z","shell.execute_reply":"2022-07-23T14:43:08.683674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **평가지표 계산 함수 작성**","metadata":{}},{"cell_type":"code","source":"import numpy as np\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-23T14:43:08.685598Z","iopub.execute_input":"2022-07-23T14:43:08.686050Z","iopub.status.idle":"2022-07-23T14:43:08.692083Z","shell.execute_reply.started":"2022-07-23T14:43:08.686019Z","shell.execute_reply":"2022-07-23T14:43:08.691092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **모델 훈련**","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\nlinear_reg_model = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.693295Z","iopub.execute_input":"2022-07-23T14:43:08.693788Z","iopub.status.idle":"2022-07-23T14:43:08.704908Z","shell.execute_reply.started":"2022-07-23T14:43:08.693748Z","shell.execute_reply":"2022-07-23T14:43:08.704139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_y = np.log(y)\nlinear_reg_model.fit(X_train, log_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.706156Z","iopub.execute_input":"2022-07-23T14:43:08.706575Z","iopub.status.idle":"2022-07-23T14:43:08.746597Z","shell.execute_reply.started":"2022-07-23T14:43:08.706545Z","shell.execute_reply":"2022-07-23T14:43:08.746042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **모델 성능검증**","metadata":{}},{"cell_type":"code","source":"preds_linear = linear_reg_model.predict(X_train)\nprint(f'선형 회귀의 RMSLE 값 : {rmsle(log_y, preds_linear, True):.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:08.748481Z","iopub.execute_input":"2022-07-23T14:43:08.749132Z","iopub.status.idle":"2022-07-23T14:43:08.786399Z","shell.execute_reply.started":"2022-07-23T14:43:08.749083Z","shell.execute_reply":"2022-07-23T14:43:08.785470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **성능개선 : 라쏘 회귀 모델**","metadata":{}},{"cell_type":"markdown","source":"## 하이퍼 파라미터 최적화","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import metrics\n\nrmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)\n\n#모델 생성 \nlasso_model = Lasso()\n#하이퍼 파라미터값 목록 \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-23T14:43:08.788225Z","iopub.execute_input":"2022-07-23T14:43:08.788874Z","iopub.status.idle":"2022-07-23T14:43:13.419337Z","shell.execute_reply.started":"2022-07-23T14:43:08.788826Z","shell.execute_reply":"2022-07-23T14:43:13.418408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 성능 검증","metadata":{}},{"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-23T14:43:13.421239Z","iopub.execute_input":"2022-07-23T14:43:13.421935Z","iopub.status.idle":"2022-07-23T14:43:13.460212Z","shell.execute_reply.started":"2022-07-23T14:43:13.421886Z","shell.execute_reply":"2022-07-23T14:43:13.459283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **성능개선 : 랜덤 포레스트 회귀 모델**","metadata":{}},{"cell_type":"markdown","source":"## 하이퍼 파라미터 최적화","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nrandomforest_model = RandomForestRegressor()\n\nrf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}\ngridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,\n                                             param_grid=rf_params,\n                                             scoring=rmsle_scorer,\n                                             cv=5)\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-23T14:43:13.462134Z","iopub.execute_input":"2022-07-23T14:43:13.462843Z","iopub.status.idle":"2022-07-23T14:44:16.950202Z","shell.execute_reply.started":"2022-07-23T14:43:13.462793Z","shell.execute_reply":"2022-07-23T14:44:16.949632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 모델 성능 검증","metadata":{}},{"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-23T14:44:16.951343Z","iopub.execute_input":"2022-07-23T14:44:16.951761Z","iopub.status.idle":"2022-07-23T14:44:17.254832Z","shell.execute_reply.started":"2022-07-23T14:44:16.951732Z","shell.execute_reply":"2022-07-23T14:44:17.253936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **예측 및 결과 제출**","metadata":{}},{"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)\n\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-23T14:44:17.256030Z","iopub.execute_input":"2022-07-23T14:44:17.256272Z","iopub.status.idle":"2022-07-23T14:44:17.983307Z","shell.execute_reply.started":"2022-07-23T14:44:17.256244Z","shell.execute_reply":"2022-07-23T14:44:17.982418Z"},"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-23T14:44:17.984870Z","iopub.execute_input":"2022-07-23T14:44:17.985410Z","iopub.status.idle":"2022-07-23T14:44:18.019329Z","shell.execute_reply.started":"2022-07-23T14:44:17.985378Z","shell.execute_reply":"2022-07-23T14:44:18.018697Z"},"trusted":true},"execution_count":null,"outputs":[]}]}