{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"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-07-23T12:08:57.380613Z","iopub.execute_input":"2022-07-23T12:08:57.381028Z","iopub.status.idle":"2022-07-23T12:08:57.390912Z","shell.execute_reply.started":"2022-07-23T12:08:57.380998Z","shell.execute_reply":"2022-07-23T12:08:57.389295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 데이터 둘러보기","metadata":{}},{"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-23T12:08:57.392654Z","iopub.execute_input":"2022-07-23T12:08:57.393143Z","iopub.status.idle":"2022-07-23T12:08:57.409310Z","shell.execute_reply.started":"2022-07-23T12:08:57.393106Z","shell.execute_reply":"2022-07-23T12:08:57.408307Z"},"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-23T12:08:57.410682Z","iopub.execute_input":"2022-07-23T12:08:57.411605Z","iopub.status.idle":"2022-07-23T12:08:57.467201Z","shell.execute_reply.started":"2022-07-23T12:08:57.411568Z","shell.execute_reply":"2022-07-23T12:08:57.465813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.469483Z","iopub.execute_input":"2022-07-23T12:08:57.469812Z","iopub.status.idle":"2022-07-23T12:08:57.476523Z","shell.execute_reply.started":"2022-07-23T12:08:57.469782Z","shell.execute_reply":"2022-07-23T12:08:57.475659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.477971Z","iopub.execute_input":"2022-07-23T12:08:57.478662Z","iopub.status.idle":"2022-07-23T12:08:57.500196Z","shell.execute_reply.started":"2022-07-23T12:08:57.478630Z","shell.execute_reply":"2022-07-23T12:08:57.499035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.501684Z","iopub.execute_input":"2022-07-23T12:08:57.501995Z","iopub.status.idle":"2022-07-23T12:08:57.517771Z","shell.execute_reply.started":"2022-07-23T12:08:57.501959Z","shell.execute_reply":"2022-07-23T12:08:57.515730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.519649Z","iopub.execute_input":"2022-07-23T12:08:57.519997Z","iopub.status.idle":"2022-07-23T12:08:57.531301Z","shell.execute_reply.started":"2022-07-23T12:08:57.519968Z","shell.execute_reply":"2022-07-23T12:08:57.529774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.533715Z","iopub.execute_input":"2022-07-23T12:08:57.534417Z","iopub.status.idle":"2022-07-23T12:08:57.554621Z","shell.execute_reply.started":"2022-07-23T12:08:57.534367Z","shell.execute_reply":"2022-07-23T12:08:57.553111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.558455Z","iopub.execute_input":"2022-07-23T12:08:57.559719Z","iopub.status.idle":"2022-07-23T12:08:57.574183Z","shell.execute_reply.started":"2022-07-23T12:08:57.559681Z","shell.execute_reply":"2022-07-23T12:08:57.573230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# feature engineering\n데이터 시각화 전 이 피처를 분석하기 적합하도록 변환","metadata":{}},{"cell_type":"markdown","source":"**1. 날짜, 시간 피처 만들기**","metadata":{}},{"cell_type":"code","source":"## object인 datetime 나누기 split함수\nprint(train['datetime'][100])\nprint(train['datetime'][100].split())\nprint(train['datetime'][100].split()[0])\nprint(train['datetime'][100].split()[1])\n\n##날짜 나누기\nprint(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])\n\n\n##시간 나누기\nprint(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-23T12:08:57.575473Z","iopub.execute_input":"2022-07-23T12:08:57.575953Z","iopub.status.idle":"2022-07-23T12:08:57.589936Z","shell.execute_reply.started":"2022-07-23T12:08:57.575914Z","shell.execute_reply":"2022-07-23T12:08:57.588367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## apply(), lambda함수를 이용해 DataFrame의 각 열에 대해 수행\n\n##날짜 피처\ntrain['date']= train['datetime'].apply(lambda x: x.split()[0])\n\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['date']= train['datetime'].apply(lambda x: x.split()[0].split(\"-\")[2])\n\n##각각 시, 분, 초\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-23T12:08:57.591692Z","iopub.execute_input":"2022-07-23T12:08:57.592423Z","iopub.status.idle":"2022-07-23T12:08:57.671073Z","shell.execute_reply.started":"2022-07-23T12:08:57.592377Z","shell.execute_reply":"2022-07-23T12:08:57.669900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**요일 피처 만들기**","metadata":{}},{"cell_type":"markdown","source":"책 189쪽이 안되서 석리송 코드 따라함","metadata":{}},{"cell_type":"code","source":"train['datetime']= pd.to_datetime(train['datetime'])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.672583Z","iopub.execute_input":"2022-07-23T12:08:57.672928Z","iopub.status.idle":"2022-07-23T12:08:57.684829Z","shell.execute_reply.started":"2022-07-23T12:08:57.672898Z","shell.execute_reply":"2022-07-23T12:08:57.683332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.686681Z","iopub.execute_input":"2022-07-23T12:08:57.688062Z","iopub.status.idle":"2022-07-23T12:08:57.712824Z","shell.execute_reply.started":"2022-07-23T12:08:57.687985Z","shell.execute_reply":"2022-07-23T12:08:57.711401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['year']= train['datetime'].dt.year\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.714696Z","iopub.execute_input":"2022-07-23T12:08:57.715023Z","iopub.status.idle":"2022-07-23T12:08:57.746321Z","shell.execute_reply.started":"2022-07-23T12:08:57.714995Z","shell.execute_reply":"2022-07-23T12:08:57.745427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['month']= train['datetime'].dt.month","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.747706Z","iopub.execute_input":"2022-07-23T12:08:57.748686Z","iopub.status.idle":"2022-07-23T12:08:57.755876Z","shell.execute_reply.started":"2022-07-23T12:08:57.748653Z","shell.execute_reply":"2022-07-23T12:08:57.754673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['day']= train['datetime'].dt.day","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.757250Z","iopub.execute_input":"2022-07-23T12:08:57.757824Z","iopub.status.idle":"2022-07-23T12:08:57.769446Z","shell.execute_reply.started":"2022-07-23T12:08:57.757791Z","shell.execute_reply":"2022-07-23T12:08:57.768231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['hour']= train['datetime'].dt.hour","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.770907Z","iopub.execute_input":"2022-07-23T12:08:57.771665Z","iopub.status.idle":"2022-07-23T12:08:57.781857Z","shell.execute_reply.started":"2022-07-23T12:08:57.771633Z","shell.execute_reply":"2022-07-23T12:08:57.780679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['minute']= train['datetime'].dt.minute","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.783685Z","iopub.execute_input":"2022-07-23T12:08:57.784026Z","iopub.status.idle":"2022-07-23T12:08:57.794570Z","shell.execute_reply.started":"2022-07-23T12:08:57.783997Z","shell.execute_reply":"2022-07-23T12:08:57.793400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['second']= train['datetime'].dt.second","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.796157Z","iopub.execute_input":"2022-07-23T12:08:57.796494Z","iopub.status.idle":"2022-07-23T12:08:57.806680Z","shell.execute_reply.started":"2022-07-23T12:08:57.796465Z","shell.execute_reply":"2022-07-23T12:08:57.805227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weekday']= train['datetime'].dt.day_name()\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.808477Z","iopub.execute_input":"2022-07-23T12:08:57.808806Z","iopub.status.idle":"2022-07-23T12:08:57.844950Z","shell.execute_reply.started":"2022-07-23T12:08:57.808778Z","shell.execute_reply":"2022-07-23T12:08:57.843771Z"},"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, Thunderstrom, Snow, Fog' })","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.846806Z","iopub.execute_input":"2022-07-23T12:08:57.847199Z","iopub.status.idle":"2022-07-23T12:08:57.857112Z","shell.execute_reply.started":"2022-07-23T12:08:57.847168Z","shell.execute_reply":"2022-07-23T12:08:57.856163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:57.858367Z","iopub.execute_input":"2022-07-23T12:08:57.858713Z","iopub.status.idle":"2022-07-23T12:08:57.886611Z","shell.execute_reply.started":"2022-07-23T12:08:57.858684Z","shell.execute_reply":"2022-07-23T12:08:57.885715Z"},"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-23T12:08:57.887719Z","iopub.execute_input":"2022-07-23T12:08:57.888899Z","iopub.status.idle":"2022-07-23T12:08:57.898586Z","shell.execute_reply.started":"2022-07-23T12:08:57.888864Z","shell.execute_reply":"2022-07-23T12:08:57.897430Z"},"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-23T12:08:57.904843Z","iopub.execute_input":"2022-07-23T12:08:57.905225Z","iopub.status.idle":"2022-07-23T12:08:58.286528Z","shell.execute_reply.started":"2022-07-23T12:08:57.905195Z","shell.execute_reply":"2022-07-23T12:08:58.285412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**count값이 너무 왼쪽으로 편향되어 있으니 로그 변환을 통해 정규분포를 만들어준다. **","metadata":{}},{"cell_type":"code","source":"sns.displot(np.log(train['count']));","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:58.288239Z","iopub.execute_input":"2022-07-23T12:08:58.288681Z","iopub.status.idle":"2022-07-23T12:08:58.796067Z","shell.execute_reply.started":"2022-07-23T12:08:58.288640Z","shell.execute_reply":"2022-07-23T12:08:58.795305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**막대그래프를 그려 각 범주형 데이터에 따라 평균 대여 수량이 어떻게 다른지 알아본다 **","metadata":{}},{"cell_type":"code","source":"## 3X2 figure준비\nmpl.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\n##서브플롯 간 간격이 넓어진다. \nfigure, axes=plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:58.797508Z","iopub.execute_input":"2022-07-23T12:08:58.798353Z","iopub.status.idle":"2022-07-23T12:08:59.952151Z","shell.execute_reply.started":"2022-07-23T12:08:58.798305Z","shell.execute_reply":"2022-07-23T12:08:59.950855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 3X2 figure준비\nmpl.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\n##서브플롯 간 간격이 넓어진다. \nfigure, axes=plt.subplots(nrows=3, ncols=2)\nplt.tight_layout()\n\n##각 배열에 data 집어넣기\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])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:08:59.954091Z","iopub.execute_input":"2022-07-23T12:08:59.954480Z","iopub.status.idle":"2022-07-23T12:09:04.180692Z","shell.execute_reply.started":"2022-07-23T12:08:59.954448Z","shell.execute_reply":"2022-07-23T12:09:04.179414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"axes\naxes.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:04.182232Z","iopub.execute_input":"2022-07-23T12:09:04.184910Z","iopub.status.idle":"2022-07-23T12:09:04.192588Z","shell.execute_reply.started":"2022-07-23T12:09:04.184847Z","shell.execute_reply":"2022-07-23T12:09:04.191550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 3X2 figure준비\nmpl.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\n\n##각 배열에 data 집어넣기\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\n## 세부 설정\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,1].set(title=\"Rental amounts by second\")\n\n## x축 라벨이 겹치지 않도록 개선\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-23T12:09:04.193985Z","iopub.execute_input":"2022-07-23T12:09:04.194605Z","iopub.status.idle":"2022-07-23T12:09:07.858768Z","shell.execute_reply.started":"2022-07-23T12:09:04.194569Z","shell.execute_reply":"2022-07-23T12:09:07.857604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**박스플롯 그려서 수치형 데이터 나타내기**","metadata":{}},{"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-23T12:09:07.860141Z","iopub.execute_input":"2022-07-23T12:09:07.860627Z","iopub.status.idle":"2022-07-23T12:09:08.785330Z","shell.execute_reply.started":"2022-07-23T12:09:07.860595Z","shell.execute_reply":"2022-07-23T12:09:08.784472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**포인트플롯을 그려 범주형 데이터에 따른 수치형 데이터의 평균, 신뢰구간 보기**\n한 화면에 여러 그래프를 그려 비교하기가 좋다.","metadata":{}},{"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='weather', ax=axes[3])\nsns.pointplot(x='hour', y='count', data=train, hue='season', ax=axes[4])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:08.786666Z","iopub.execute_input":"2022-07-23T12:09:08.787701Z","iopub.status.idle":"2022-07-23T12:09:25.917976Z","shell.execute_reply.started":"2022-07-23T12:09:08.787660Z","shell.execute_reply":"2022-07-23T12:09:25.916662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**회귀선을 포함한 산점도 그래프**\n수치형 데이터 간 상관관계 파악","metadata":{}},{"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], scatter_kws={'alpha':0.2}, line_kws={'color': 'red'})\nsns.regplot(x='atemp', y='count', data=train, ax=axes[0,1], scatter_kws={'alpha':0.2}, line_kws={'color': 'red'})\nsns.regplot(x='windspeed', y='count', data=train, ax=axes[1,0], scatter_kws={'alpha':0.2}, line_kws={'color': 'red'})\nsns.regplot(x='humidity', y='count', data=train, ax=axes[1,1], scatter_kws={'alpha':0.2}, line_kws={'color': 'red'})","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:25.919961Z","iopub.execute_input":"2022-07-23T12:09:25.920404Z","iopub.status.idle":"2022-07-23T12:09:29.448503Z","shell.execute_reply.started":"2022-07-23T12:09:25.920358Z","shell.execute_reply":"2022-07-23T12:09:29.446827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**히트맵** 수치형 데이터 간의 상관관계","metadata":{}},{"cell_type":"code","source":"train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:29.450541Z","iopub.execute_input":"2022-07-23T12:09:29.451081Z","iopub.status.idle":"2022-07-23T12:09:29.472092Z","shell.execute_reply.started":"2022-07-23T12:09:29.450967Z","shell.execute_reply":"2022-07-23T12:09:29.470827Z"},"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-23T12:09:29.473640Z","iopub.execute_input":"2022-07-23T12:09:29.474771Z","iopub.status.idle":"2022-07-23T12:09:29.786972Z","shell.execute_reply.started":"2022-07-23T12:09:29.474720Z","shell.execute_reply":"2022-07-23T12:09:29.785682Z"},"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/'\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-23T12:09:29.788278Z","iopub.execute_input":"2022-07-23T12:09:29.788659Z","iopub.status.idle":"2022-07-23T12:09:29.833780Z","shell.execute_reply.started":"2022-07-23T12:09:29.788629Z","shell.execute_reply":"2022-07-23T12:09:29.832594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**feature enginnering**","metadata":{}},{"cell_type":"markdown","source":"데이터 제거하기(이상치 제거/ 파생피처 추가/ 필요없는 피처 제거)","metadata":{}},{"cell_type":"code","source":"train= train[train['weather']!=4]","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:29.835165Z","iopub.execute_input":"2022-07-23T12:09:29.835507Z","iopub.status.idle":"2022-07-23T12:09:29.844875Z","shell.execute_reply.started":"2022-07-23T12:09:29.835477Z","shell.execute_reply":"2022-07-23T12:09:29.843755Z"},"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-23T12:09:29.846775Z","iopub.execute_input":"2022-07-23T12:09:29.847245Z","iopub.status.idle":"2022-07-23T12:09:29.879645Z","shell.execute_reply.started":"2022-07-23T12:09:29.847202Z","shell.execute_reply":"2022-07-23T12:09:29.878534Z"},"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])\n\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-23T12:09:29.881754Z","iopub.execute_input":"2022-07-23T12:09:29.882229Z","iopub.status.idle":"2022-07-23T12:09:30.168929Z","shell.execute_reply.started":"2022-07-23T12:09:29.882186Z","shell.execute_reply":"2022-07-23T12:09:30.167754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']\n\nall_data = all_data.drop(drop_features, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:30.170126Z","iopub.execute_input":"2022-07-23T12:09:30.170434Z","iopub.status.idle":"2022-07-23T12:09:30.182113Z","shell.execute_reply.started":"2022-07-23T12:09:30.170407Z","shell.execute_reply":"2022-07-23T12:09:30.180926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**데이터 나누기**","metadata":{}},{"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-23T12:09:30.183777Z","iopub.execute_input":"2022-07-23T12:09:30.184507Z","iopub.status.idle":"2022-07-23T12:09:30.196185Z","shell.execute_reply.started":"2022-07-23T12:09:30.184473Z","shell.execute_reply":"2022-07-23T12:09:30.195314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:30.199174Z","iopub.execute_input":"2022-07-23T12:09:30.199525Z","iopub.status.idle":"2022-07-23T12:09:30.216863Z","shell.execute_reply.started":"2022-07-23T12:09:30.199494Z","shell.execute_reply":"2022-07-23T12:09:30.216094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**평가지표 계산 함수 작성**","metadata":{}},{"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-23T12:09:30.218060Z","iopub.execute_input":"2022-07-23T12:09:30.218549Z","iopub.status.idle":"2022-07-23T12:09:30.229865Z","shell.execute_reply.started":"2022-07-23T12:09:30.218518Z","shell.execute_reply":"2022-07-23T12:09:30.228797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 모델 훈련 random forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import metrics\n\nrandomforest_model= RandomForestRegressor()\nrf_params= {'random_state':[42], 'n_estimators':[100, 120, 140]}\nrmsle_scorer= metrics.make_scorer(rmsle, greater_is_better=False)\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)\n\nprint('최적 하이퍼파라미터:', gridsearch_random_forest_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:09:30.231259Z","iopub.execute_input":"2022-07-23T12:09:30.231600Z","iopub.status.idle":"2022-07-23T12:10:20.606236Z","shell.execute_reply.started":"2022-07-23T12:09:30.231571Z","shell.execute_reply":"2022-07-23T12:10:20.605004Z"},"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-23T12:10:20.607781Z","iopub.execute_input":"2022-07-23T12:10:20.608096Z","iopub.status.idle":"2022-07-23T12:10:20.936654Z","shell.execute_reply.started":"2022-07-23T12:10:20.608067Z","shell.execute_reply":"2022-07-23T12:10:20.935843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"제출하기","metadata":{}},{"cell_type":"code","source":"randomforest_preds=gridsearch_random_forest_model.best_estimator_.predict(X_test)\nsubmission['count']= np.exp(randomforest_preds)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T12:16:35.049878Z","iopub.execute_input":"2022-07-23T12:16:35.050254Z","iopub.status.idle":"2022-07-23T12:16:35.291999Z","shell.execute_reply.started":"2022-07-23T12:16:35.050227Z","shell.execute_reply":"2022-07-23T12:16:35.290744Z"},"trusted":true},"execution_count":null,"outputs":[]}]}