{"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-24T22:30:02.778705Z","iopub.execute_input":"2022-07-24T22:30:02.779095Z","iopub.status.idle":"2022-07-24T22:30:02.888085Z","shell.execute_reply.started":"2022-07-24T22:30:02.778987Z","shell.execute_reply":"2022-07-24T22:30:02.887124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.891290Z","iopub.execute_input":"2022-07-24T22:30:02.891619Z","iopub.status.idle":"2022-07-24T22:30:02.901087Z","shell.execute_reply.started":"2022-07-24T22:30:02.891582Z","shell.execute_reply":"2022-07-24T22:30:02.900019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.903031Z","iopub.execute_input":"2022-07-24T22:30:02.903649Z","iopub.status.idle":"2022-07-24T22:30:02.933417Z","shell.execute_reply.started":"2022-07-24T22:30:02.903604Z","shell.execute_reply":"2022-07-24T22:30:02.932585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.935232Z","iopub.execute_input":"2022-07-24T22:30:02.935556Z","iopub.status.idle":"2022-07-24T22:30:02.964406Z","shell.execute_reply.started":"2022-07-24T22:30:02.935518Z","shell.execute_reply":"2022-07-24T22:30:02.963254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.966134Z","iopub.execute_input":"2022-07-24T22:30:02.967956Z","iopub.status.idle":"2022-07-24T22:30:02.982291Z","shell.execute_reply.started":"2022-07-24T22:30:02.967890Z","shell.execute_reply":"2022-07-24T22:30:02.981396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 요기에서 스플릿하는 것이 너무 번거로워서 요렇게 타입을 바꾸었습니다!\ntrain['datetime'] = pd.to_datetime(train['datetime'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.983378Z","iopub.execute_input":"2022-07-24T22:30:02.983849Z","iopub.status.idle":"2022-07-24T22:30:02.995804Z","shell.execute_reply.started":"2022-07-24T22:30:02.983815Z","shell.execute_reply":"2022-07-24T22:30:02.995096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 그러면 요렇게 datetime type으로!\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:02.996944Z","iopub.execute_input":"2022-07-24T22:30:02.997167Z","iopub.status.idle":"2022-07-24T22:30:03.019995Z","shell.execute_reply.started":"2022-07-24T22:30:02.997142Z","shell.execute_reply":"2022-07-24T22:30:03.018753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 그래서 요렇게 원하는 속성을 뽑을 수 있습니다! :)\ntrain['year'] = train['datetime'].dt.year\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.021249Z","iopub.execute_input":"2022-07-24T22:30:03.021651Z","iopub.status.idle":"2022-07-24T22:30:03.052206Z","shell.execute_reply.started":"2022-07-24T22:30:03.021618Z","shell.execute_reply":"2022-07-24T22:30:03.051331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['month'] = train['datetime'].dt.month","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.053704Z","iopub.execute_input":"2022-07-24T22:30:03.054069Z","iopub.status.idle":"2022-07-24T22:30:03.061254Z","shell.execute_reply.started":"2022-07-24T22:30:03.054025Z","shell.execute_reply":"2022-07-24T22:30:03.060302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['day'] = train['datetime'].dt.day","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.063693Z","iopub.execute_input":"2022-07-24T22:30:03.064314Z","iopub.status.idle":"2022-07-24T22:30:03.074705Z","shell.execute_reply.started":"2022-07-24T22:30:03.064278Z","shell.execute_reply":"2022-07-24T22:30:03.073830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['hour'] = train['datetime'].dt.hour","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.076061Z","iopub.execute_input":"2022-07-24T22:30:03.076492Z","iopub.status.idle":"2022-07-24T22:30:03.087050Z","shell.execute_reply.started":"2022-07-24T22:30:03.076459Z","shell.execute_reply":"2022-07-24T22:30:03.086071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['minute'] = train['datetime'].dt.minute\ntrain['second'] = train['datetime'].dt.second","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.088274Z","iopub.execute_input":"2022-07-24T22:30:03.088671Z","iopub.status.idle":"2022-07-24T22:30:03.102311Z","shell.execute_reply.started":"2022-07-24T22:30:03.088603Z","shell.execute_reply":"2022-07-24T22:30:03.101327Z"},"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-24T22:30:03.103789Z","iopub.execute_input":"2022-07-24T22:30:03.104667Z","iopub.status.idle":"2022-07-24T22:30:03.147251Z","shell.execute_reply.started":"2022-07-24T22:30:03.104618Z","shell.execute_reply":"2022-07-24T22:30:03.145860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['season'] = train['season'].map({1: 'spring', 2: 'summer', 3: 'fall', 4: 'winter'})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.148711Z","iopub.execute_input":"2022-07-24T22:30:03.149028Z","iopub.status.idle":"2022-07-24T22:30:03.157325Z","shell.execute_reply.started":"2022-07-24T22:30:03.148990Z","shell.execute_reply":"2022-07-24T22:30:03.156539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['weather'] = train['weather'].map({1: 'clear', 2: 'mist, few clouds', 3: 'light snow, rain, thunderstorm', 4: 'heavy rain, thunderstorm, snow, fog'})","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.158482Z","iopub.execute_input":"2022-07-24T22:30:03.158882Z","iopub.status.idle":"2022-07-24T22:30:03.174769Z","shell.execute_reply.started":"2022-07-24T22:30:03.158847Z","shell.execute_reply":"2022-07-24T22:30:03.173930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.175947Z","iopub.execute_input":"2022-07-24T22:30:03.176698Z","iopub.status.idle":"2022-07-24T22:30:03.203122Z","shell.execute_reply.started":"2022-07-24T22:30:03.176662Z","shell.execute_reply":"2022-07-24T22:30:03.202194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:03.204455Z","iopub.execute_input":"2022-07-24T22:30:03.204736Z","iopub.status.idle":"2022-07-24T22:30:04.235994Z","shell.execute_reply.started":"2022-07-24T22:30:03.204692Z","shell.execute_reply":"2022-07-24T22:30:04.234989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt로 해도 되는 것 같아서 굳이 바꿔보았습니다ㅎㅎ\nplt.rc('font', size = 15)\nsns.displot(train['count'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:04.238861Z","iopub.execute_input":"2022-07-24T22:30:04.239132Z","iopub.status.idle":"2022-07-24T22:30:04.732085Z","shell.execute_reply.started":"2022-07-24T22:30:04.239102Z","shell.execute_reply":"2022-07-24T22:30:04.730923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 차이가 있나 확인하기 위해 히스토그램으로도 한 번!ㅎㅎ\nsns.histplot(train['count'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:04.733945Z","iopub.execute_input":"2022-07-24T22:30:04.734247Z","iopub.status.idle":"2022-07-24T22:30:05.063283Z","shell.execute_reply.started":"2022-07-24T22:30:04.734213Z","shell.execute_reply":"2022-07-24T22:30:05.062614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(np.log(train['count']))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:05.064765Z","iopub.execute_input":"2022-07-24T22:30:05.065231Z","iopub.status.idle":"2022-07-24T22:30:05.365820Z","shell.execute_reply.started":"2022-07-24T22:30:05.065190Z","shell.execute_reply":"2022-07-24T22:30:05.364946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['count'] <= 20]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:05.367319Z","iopub.execute_input":"2022-07-24T22:30:05.368318Z","iopub.status.idle":"2022-07-24T22:30:05.407197Z","shell.execute_reply.started":"2022-07-24T22:30:05.368278Z","shell.execute_reply":"2022-07-24T22:30:05.406298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.log(train['count'])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:05.408636Z","iopub.execute_input":"2022-07-24T22:30:05.409164Z","iopub.status.idle":"2022-07-24T22:30:05.418830Z","shell.execute_reply.started":"2022-07-24T22:30:05.409118Z","shell.execute_reply":"2022-07-24T22:30:05.417965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 심플하게 가즈아!!!!ㅎㅎ\nfor feature in ['year', 'month', 'day', 'hour', 'minute', 'second'] : \n    sns.barplot(x = feature, y = 'count', data = train)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:05.420280Z","iopub.execute_input":"2022-07-24T22:30:05.420642Z","iopub.status.idle":"2022-07-24T22:30:09.068185Z","shell.execute_reply.started":"2022-07-24T22:30:05.420607Z","shell.execute_reply":"2022-07-24T22:30:09.067338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 심플하게 가즈아2!!!!ㅎㅎ\nfor feature in ['season', 'weather', 'holiday', 'workingday'] : \n    sns.boxplot(x = feature, y = 'count', data = train)\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:09.069509Z","iopub.execute_input":"2022-07-24T22:30:09.069799Z","iopub.status.idle":"2022-07-24T22:30:09.959084Z","shell.execute_reply.started":"2022-07-24T22:30:09.069767Z","shell.execute_reply":"2022-07-24T22:30:09.958426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 심플하게 가즈아3!!!!ㅎㅎ\nfor feature in ['season', 'weather', 'holiday', 'workingday', 'weekday'] : \n    plt.figure(figsize = (20,4))\n    sns.pointplot(x = 'hour', y = 'count', data = train, hue = feature)\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:09.960219Z","iopub.execute_input":"2022-07-24T22:30:09.960873Z","iopub.status.idle":"2022-07-24T22:30:25.217701Z","shell.execute_reply.started":"2022-07-24T22:30:09.960839Z","shell.execute_reply":"2022-07-24T22:30:25.216795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 심플하게 가즈아4!!!!ㅎㅎ\nfor feature in ['temp', 'atemp', 'windspeed', 'humidity'] : \n    sns.regplot(x = feature, y = 'count', data = train)\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:25.218922Z","iopub.execute_input":"2022-07-24T22:30:25.219158Z","iopub.status.idle":"2022-07-24T22:30:28.386220Z","shell.execute_reply.started":"2022-07-24T22:30:25.219131Z","shell.execute_reply":"2022-07-24T22:30:28.385545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrMat = train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()\nsns.heatmap(corrMat, annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:28.387212Z","iopub.execute_input":"2022-07-24T22:30:28.387885Z","iopub.status.idle":"2022-07-24T22:30:28.768424Z","shell.execute_reply.started":"2022-07-24T22:30:28.387853Z","shell.execute_reply":"2022-07-24T22:30:28.767828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 심플하게 가즈아4!!!!ㅎㅎ\nfor feature in ['temp', 'atemp', 'windspeed', 'humidity'] : \n    sns.regplot(x = feature, y = 'count', data = train, scatter_kws = {'alpha' : 0.2}, line_kws = {'color' : 'hotpink'})\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:28.771976Z","iopub.execute_input":"2022-07-24T22:30:28.772702Z","iopub.status.idle":"2022-07-24T22:30:32.122877Z","shell.execute_reply.started":"2022-07-24T22:30:28.772660Z","shell.execute_reply":"2022-07-24T22:30:32.121875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrMat = train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()\nsns.heatmap(corrMat, annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:32.124659Z","iopub.execute_input":"2022-07-24T22:30:32.124981Z","iopub.status.idle":"2022-07-24T22:30:32.509703Z","shell.execute_reply.started":"2022-07-24T22:30:32.124948Z","shell.execute_reply":"2022-07-24T22:30:32.508844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 이 방법도 좋은 것 같아서 공유합니다\ncorr = train[['temp', 'atemp', 'humidity', 'windspeed', 'count']].corr()\ncorr.style.background_gradient(cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:32.511327Z","iopub.execute_input":"2022-07-24T22:30:32.511850Z","iopub.status.idle":"2022-07-24T22:30:32.594695Z","shell.execute_reply.started":"2022-07-24T22:30:32.511806Z","shell.execute_reply":"2022-07-24T22:30:32.593887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-24T22:34:36.905853Z","iopub.execute_input":"2022-07-24T22:34:36.906621Z","iopub.status.idle":"2022-07-24T22:34:36.956951Z","shell.execute_reply.started":"2022-07-24T22:34:36.906584Z","shell.execute_reply":"2022-07-24T22:34:36.956132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train['weather'] != 4]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:38.521843Z","iopub.execute_input":"2022-07-24T22:34:38.522157Z","iopub.status.idle":"2022-07-24T22:34:38.529221Z","shell.execute_reply.started":"2022-07-24T22:34:38.522124Z","shell.execute_reply":"2022-07-24T22:34:38.528287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data_temp = pd.concat([train, test])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:39.417091Z","iopub.execute_input":"2022-07-24T22:34:39.417745Z","iopub.status.idle":"2022-07-24T22:34:39.427067Z","shell.execute_reply.started":"2022-07-24T22:34:39.417673Z","shell.execute_reply":"2022-07-24T22:34:39.426239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data_temp","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:39.897383Z","iopub.execute_input":"2022-07-24T22:34:39.897703Z","iopub.status.idle":"2022-07-24T22:34:39.922171Z","shell.execute_reply.started":"2022-07-24T22:34:39.897670Z","shell.execute_reply":"2022-07-24T22:34:39.921321Z"},"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-24T22:34:41.066424Z","iopub.execute_input":"2022-07-24T22:34:41.067296Z","iopub.status.idle":"2022-07-24T22:34:41.097584Z","shell.execute_reply.started":"2022-07-24T22:34:41.067248Z","shell.execute_reply":"2022-07-24T22:34:41.096644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nall_data['date'] = all_data['datetime'].apply(lambda x : x.split()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:42.520866Z","iopub.execute_input":"2022-07-24T22:34:42.521183Z","iopub.status.idle":"2022-07-24T22:34:42.536591Z","shell.execute_reply.started":"2022-07-24T22:34:42.521147Z","shell.execute_reply":"2022-07-24T22:34:42.535534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['year'] = all_data['datetime'].apply(lambda x : x.split()[0].split('-')[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:43.431079Z","iopub.execute_input":"2022-07-24T22:34:43.431770Z","iopub.status.idle":"2022-07-24T22:34:43.449120Z","shell.execute_reply.started":"2022-07-24T22:34:43.431732Z","shell.execute_reply":"2022-07-24T22:34:43.448475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['month'] = all_data['datetime'].apply(lambda x : x.split()[0].split('-')[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:44.129763Z","iopub.execute_input":"2022-07-24T22:34:44.130085Z","iopub.status.idle":"2022-07-24T22:34:44.150352Z","shell.execute_reply.started":"2022-07-24T22:34:44.130043Z","shell.execute_reply":"2022-07-24T22:34:44.149275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['hour'] = all_data['datetime'].apply(lambda x : x.split()[1].split(':')[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:44.438664Z","iopub.execute_input":"2022-07-24T22:34:44.439261Z","iopub.status.idle":"2022-07-24T22:34:44.459799Z","shell.execute_reply.started":"2022-07-24T22:34:44.439217Z","shell.execute_reply":"2022-07-24T22:34:44.459035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data['weekday'] = all_data['date'].apply(lambda dateString : datetime.strptime(dateString, '%Y-%m-%d').weekday())","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:44.734843Z","iopub.execute_input":"2022-07-24T22:34:44.735143Z","iopub.status.idle":"2022-07-24T22:34:44.924857Z","shell.execute_reply.started":"2022-07-24T22:34:44.735103Z","shell.execute_reply":"2022-07-24T22:34:44.923858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']\ndrop_features = ['casual', 'registered', 'datetime', 'date', 'month']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:45.213842Z","iopub.execute_input":"2022-07-24T22:34:45.214161Z","iopub.status.idle":"2022-07-24T22:34:45.219186Z","shell.execute_reply.started":"2022-07-24T22:34:45.214127Z","shell.execute_reply":"2022-07-24T22:34:45.218045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = all_data.drop(drop_features, axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:47.710530Z","iopub.execute_input":"2022-07-24T22:34:47.711252Z","iopub.status.idle":"2022-07-24T22:34:47.725111Z","shell.execute_reply.started":"2022-07-24T22:34:47.711211Z","shell.execute_reply":"2022-07-24T22:34:47.724226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-24T22:34:48.117913Z","iopub.execute_input":"2022-07-24T22:34:48.118215Z","iopub.status.idle":"2022-07-24T22:34:48.129129Z","shell.execute_reply.started":"2022-07-24T22:34:48.118185Z","shell.execute_reply":"2022-07-24T22:34:48.127898Z"},"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)\n\ny = train['count']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:48.949650Z","iopub.execute_input":"2022-07-24T22:34:48.949977Z","iopub.status.idle":"2022-07-24T22:34:48.960297Z","shell.execute_reply.started":"2022-07-24T22:34:48.949944Z","shell.execute_reply":"2022-07-24T22:34:48.959302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:49.170516Z","iopub.execute_input":"2022-07-24T22:34:49.170875Z","iopub.status.idle":"2022-07-24T22:34:49.188045Z","shell.execute_reply.started":"2022-07-24T22:34:49.170837Z","shell.execute_reply":"2022-07-24T22:34:49.187107Z"},"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-24T22:34:49.436255Z","iopub.execute_input":"2022-07-24T22:34:49.436776Z","iopub.status.idle":"2022-07-24T22:34:49.442995Z","shell.execute_reply.started":"2022-07-24T22:34:49.436713Z","shell.execute_reply":"2022-07-24T22:34:49.442029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\nlinear_reg_model = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:49.976592Z","iopub.execute_input":"2022-07-24T22:34:49.977412Z","iopub.status.idle":"2022-07-24T22:34:50.234385Z","shell.execute_reply.started":"2022-07-24T22:34:49.977373Z","shell.execute_reply":"2022-07-24T22:34:50.233370Z"},"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-24T22:34:50.236169Z","iopub.execute_input":"2022-07-24T22:34:50.236647Z","iopub.status.idle":"2022-07-24T22:34:50.271807Z","shell.execute_reply.started":"2022-07-24T22:34:50.236581Z","shell.execute_reply":"2022-07-24T22:34:50.270842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = linear_reg_model.predict(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:50.507075Z","iopub.execute_input":"2022-07-24T22:34:50.507919Z","iopub.status.idle":"2022-07-24T22:34:50.535865Z","shell.execute_reply.started":"2022-07-24T22:34:50.507876Z","shell.execute_reply":"2022-07-24T22:34:50.534540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rmsle(log_y, preds, True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:51.244287Z","iopub.execute_input":"2022-07-24T22:34:51.244588Z","iopub.status.idle":"2022-07-24T22:34:51.253881Z","shell.execute_reply.started":"2022-07-24T22:34:51.244557Z","shell.execute_reply":"2022-07-24T22:34:51.252965Z"},"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-24T22:34:51.475455Z","iopub.execute_input":"2022-07-24T22:34:51.475787Z","iopub.status.idle":"2022-07-24T22:34:51.532742Z","shell.execute_reply.started":"2022-07-24T22:34:51.475750Z","shell.execute_reply":"2022-07-24T22:34:51.531476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# p. 224 모델 개선하기!!","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:40:58.971808Z","iopub.execute_input":"2022-07-24T22:40:58.972204Z","iopub.status.idle":"2022-07-24T22:40:58.977289Z","shell.execute_reply.started":"2022-07-24T22:40:58.972168Z","shell.execute_reply":"2022-07-24T22:40:58.976165Z"},"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-24T22:41:50.561943Z","iopub.execute_input":"2022-07-24T22:41:50.562388Z","iopub.status.idle":"2022-07-24T22:41:50.567688Z","shell.execute_reply.started":"2022-07-24T22:41:50.562354Z","shell.execute_reply":"2022-07-24T22:41:50.566783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_params = {'max_iter' : [3000], '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)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:42:53.937718Z","iopub.execute_input":"2022-07-24T22:42:53.938257Z","iopub.status.idle":"2022-07-24T22:42:53.943249Z","shell.execute_reply.started":"2022-07-24T22:42:53.938221Z","shell.execute_reply":"2022-07-24T22:42:53.942342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gridsearch_ridge_model = GridSearchCV(estimator=ridge_model, param_grid=ridge_params, scoring=rmsle_scorer, cv = 5)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:43:43.381621Z","iopub.execute_input":"2022-07-24T22:43:43.381945Z","iopub.status.idle":"2022-07-24T22:43:43.386540Z","shell.execute_reply.started":"2022-07-24T22:43:43.381913Z","shell.execute_reply":"2022-07-24T22:43:43.385738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_y = np.log(y)\ngridsearch_ridge_model.fit(X_train, log_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:44:02.726669Z","iopub.execute_input":"2022-07-24T22:44:02.727197Z","iopub.status.idle":"2022-07-24T22:44:04.775041Z","shell.execute_reply.started":"2022-07-24T22:44:02.727130Z","shell.execute_reply":"2022-07-24T22:44:04.774101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gridsearch_ridge_model.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:44:11.385278Z","iopub.execute_input":"2022-07-24T22:44:11.385857Z","iopub.status.idle":"2022-07-24T22:44:11.391223Z","shell.execute_reply.started":"2022-07-24T22:44:11.385818Z","shell.execute_reply":"2022-07-24T22:44:11.390343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_preds = gridsearch_ridge_model.best_estimator_.predict(X_train)\nprint(rmsle(log_y, ridge_preds, True))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:45:11.682069Z","iopub.execute_input":"2022-07-24T22:45:11.682383Z","iopub.status.idle":"2022-07-24T22:45:11.705921Z","shell.execute_reply.started":"2022-07-24T22:45:11.682353Z","shell.execute_reply":"2022-07-24T22:45:11.704886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridge_preds = gridsearch_ridge_model.best_estimator_.predict(X_test)\n\nsubmission['count'] = np.exp(ridge_preds)\nsubmission.to_csv('ridge_submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:46:42.397964Z","iopub.execute_input":"2022-07-24T22:46:42.398812Z","iopub.status.idle":"2022-07-24T22:46:42.457927Z","shell.execute_reply.started":"2022-07-24T22:46:42.398768Z","shell.execute_reply":"2022-07-24T22:46:42.456760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# p. 228 (라쏘 회귀는 생략!)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:48:15.480529Z","iopub.execute_input":"2022-07-24T22:48:15.480914Z","iopub.status.idle":"2022-07-24T22:48:15.485127Z","shell.execute_reply.started":"2022-07-24T22:48:15.480875Z","shell.execute_reply":"2022-07-24T22:48:15.484314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nrf_model = RandomForestRegressor()\n\nrf_params = {'random_state' : [42], 'n_estimators' : [100,120,140,160,180,200]}\ngridsearch_rf_model = GridSearchCV(estimator=rf_model, param_grid=rf_params, scoring = rmsle_scorer, cv = 5)\n\nlog_y = np.log(y)\ngridsearch_rf_model.fit(X_train, log_y)\nprint(gridsearch_rf_model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:50:20.996636Z","iopub.execute_input":"2022-07-24T22:50:20.997317Z","iopub.status.idle":"2022-07-24T22:52:53.288156Z","shell.execute_reply.started":"2022-07-24T22:50:20.997275Z","shell.execute_reply":"2022-07-24T22:52:53.286955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid_preds = gridsearch_rf_model.best_estimator_.predict(X_train)\nprint(rmsle(log_y, rf_grid_preds, True))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:53:24.612155Z","iopub.execute_input":"2022-07-24T22:53:24.612504Z","iopub.status.idle":"2022-07-24T22:53:24.940258Z","shell.execute_reply.started":"2022-07-24T22:53:24.612470Z","shell.execute_reply":"2022-07-24T22:53:24.939283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_grid_preds = gridsearch_rf_model.best_estimator_.predict(X_test)\nsubmission['count'] = np.exp(rf_grid_preds)\nsubmission.to_csv('rf_grid_submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:53:29.500622Z","iopub.execute_input":"2022-07-24T22:53:29.502788Z","iopub.status.idle":"2022-07-24T22:53:29.723091Z","shell.execute_reply.started":"2022-07-24T22:53:29.502743Z","shell.execute_reply":"2022-07-24T22:53:29.722325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}