{"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":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"ExecuteTime":{"end_time":"2022-05-26T04:50:51.839508Z","start_time":"2022-05-26T04:50:47.392782Z"},"papermill":{"duration":1.171865,"end_time":"2022-07-08T11:58:58.572725","exception":false,"start_time":"2022-07-08T11:58:57.400860","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:51.951260Z","iopub.execute_input":"2022-07-15T09:04:51.951726Z","iopub.status.idle":"2022-07-15T09:04:52.623806Z","shell.execute_reply.started":"2022-07-15T09:04:51.951633Z","shell.execute_reply":"2022-07-15T09:04:52.622586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 读入数据并预处理\n对因变量取对数，检查缺失值","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')\ntest = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')\n#train = pd.read_csv('train.csv')\n#test = pd.read_csv('test.csv')","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:03:57.648729Z","start_time":"2022-05-25T13:03:57.447134Z"},"papermill":{"duration":0.071487,"end_time":"2022-07-08T11:58:58.652219","exception":false,"start_time":"2022-07-08T11:58:58.580732","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.625873Z","iopub.execute_input":"2022-07-15T09:04:52.626212Z","iopub.status.idle":"2022-07-15T09:04:52.695232Z","shell.execute_reply.started":"2022-07-15T09:04:52.626181Z","shell.execute_reply":"2022-07-15T09:04:52.694010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:03:57.704126Z","start_time":"2022-05-25T13:03:57.651837Z"},"papermill":{"duration":0.039082,"end_time":"2022-07-08T11:58:58.699371","exception":false,"start_time":"2022-07-08T11:58:58.660289","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.696633Z","iopub.execute_input":"2022-07-15T09:04:52.697264Z","iopub.status.idle":"2022-07-15T09:04:52.724590Z","shell.execute_reply.started":"2022-07-15T09:04:52.697234Z","shell.execute_reply":"2022-07-15T09:04:52.723707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#取对数：+1防止出现log0的情况\nfor col in ['casual', 'registered', 'count']:\n    train['%s_log' % col] = np.log(train[col] + 1)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:52.727579Z","iopub.execute_input":"2022-07-15T09:04:52.728730Z","iopub.status.idle":"2022-07-15T09:04:52.758545Z","shell.execute_reply.started":"2022-07-15T09:04:52.728685Z","shell.execute_reply":"2022-07-15T09:04:52.757495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum()#没有缺失值，不需要填补","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:52.760023Z","iopub.execute_input":"2022-07-15T09:04:52.760337Z","iopub.status.idle":"2022-07-15T09:04:52.771388Z","shell.execute_reply.started":"2022-07-15T09:04:52.760309Z","shell.execute_reply":"2022-07-15T09:04:52.770460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 处理日期\n从日期中提取月份、日期等信息，并将休假和工作日分开","metadata":{}},{"cell_type":"code","source":"dt = pd.DatetimeIndex(train['datetime'])\ntrain.set_index(dt, inplace=True)\ndtt = pd.DatetimeIndex(test['datetime'])\ntest.set_index(dtt, inplace=True)\ndef get_day(day_start):\n    day_end = day_start + pd.offsets.DateOffset(hours=23)\n    return pd.date_range(day_start, day_end, freq=\"H\")\n\n# 纳税日需要工作\ntrain.loc[get_day(pd.datetime(2011, 4, 15)), \"workingday\"] = 1\ntrain.loc[get_day(pd.datetime(2012, 4, 16)), \"workingday\"] = 1\n# 感恩节不需要工作\ntest.loc[get_day(pd.datetime(2011, 11, 25)), \"workingday\"] = 0\ntest.loc[get_day(pd.datetime(2012, 11, 23)), \"workingday\"] = 0\n# 圣诞节不需要工作\ntest.loc[get_day(pd.datetime(2011, 12, 24)), \"workingday\"] = 0\ntest.loc[get_day(pd.datetime(2011, 12, 31)), \"workingday\"] = 0\ntest.loc[get_day(pd.datetime(2012, 12, 26)), \"workingday\"] = 0\ntest.loc[get_day(pd.datetime(2012, 12, 31)), \"workingday\"] = 0\n\n#暴雨\ntest.loc[get_day(pd.datetime(2012, 5, 21)), \"holiday\"] = 1\n#海啸\ntrain.loc[get_day(pd.datetime(2012, 6, 1)), \"holiday\"] = 1","metadata":{"papermill":{"duration":0.050121,"end_time":"2022-07-08T11:59:11.579688","exception":false,"start_time":"2022-07-08T11:59:11.529567","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.772858Z","iopub.execute_input":"2022-07-15T09:04:52.773351Z","iopub.status.idle":"2022-07-15T09:04:52.815975Z","shell.execute_reply.started":"2022-07-15T09:04:52.773320Z","shell.execute_reply":"2022-07-15T09:04:52.815016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#提取年份、月份、日期等信息\nfrom datetime import datetime\n\ndef time_process(df):\n    df['year'] = pd.DatetimeIndex(df.index).year\n    df['month'] = pd.DatetimeIndex(df.index).month\n    df['day'] = pd.DatetimeIndex(df.index).day\n    df['hour'] = pd.DatetimeIndex(df.index).hour\n    df['week'] = pd.DatetimeIndex(df.index).weekofyear\n    df['weekday'] = pd.DatetimeIndex(df.index).dayofweek\n    return df\n\ntrain = time_process(train)\ntest = time_process(test)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:03:58.083139Z","start_time":"2022-05-25T13:03:57.707311Z"},"papermill":{"duration":0.060338,"end_time":"2022-07-08T11:58:58.799289","exception":false,"start_time":"2022-07-08T11:58:58.738951","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.817180Z","iopub.execute_input":"2022-07-15T09:04:52.817675Z","iopub.status.idle":"2022-07-15T09:04:52.857280Z","shell.execute_reply.started":"2022-07-15T09:04:52.817646Z","shell.execute_reply":"2022-07-15T09:04:52.856418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(2)","metadata":{"papermill":{"duration":0.040165,"end_time":"2022-07-08T11:58:58.847547","exception":false,"start_time":"2022-07-08T11:58:58.807382","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.858434Z","iopub.execute_input":"2022-07-15T09:04:52.858931Z","iopub.status.idle":"2022-07-15T09:04:52.883208Z","shell.execute_reply.started":"2022-07-15T09:04:52.858901Z","shell.execute_reply":"2022-07-15T09:04:52.882474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 可视化分析\n### 1.一天中不同时间段的影响","metadata":{}},{"cell_type":"code","source":"#工作日\nsns.boxplot(x='hour',y='count',data=train[train['workingday'] == 1])","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:15.584906Z","start_time":"2022-05-25T13:04:14.804326Z"},"papermill":{"duration":0.491063,"end_time":"2022-07-08T11:59:12.117305","exception":false,"start_time":"2022-07-08T11:59:11.626242","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:52.884272Z","iopub.execute_input":"2022-07-15T09:04:52.885192Z","iopub.status.idle":"2022-07-15T09:04:53.408447Z","shell.execute_reply.started":"2022-07-15T09:04:52.885157Z","shell.execute_reply":"2022-07-15T09:04:53.407582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#非工作日\nsns.boxplot(x='hour',y='count',data=train[train['workingday'] == 0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:53.412295Z","iopub.execute_input":"2022-07-15T09:04:53.412639Z","iopub.status.idle":"2022-07-15T09:04:53.945711Z","shell.execute_reply.started":"2022-07-15T09:04:53.412610Z","shell.execute_reply":"2022-07-15T09:04:53.944585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"可以发现，在工作日中的高峰期是早上8点和下午17点、18点；在非工作日中的高峰期是10点到19点。将这些时间段标记为高峰期。","metadata":{}},{"cell_type":"code","source":"#星期几对应的使用量。可以明显发现非工作日和工作日的使用曲线分别高度重合，具有普遍规律\nsns.pointplot(x='hour',y='count',hue='weekday',join=True,data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:25.102624Z","start_time":"2022-05-25T13:04:19.709018Z"},"papermill":{"duration":4.456742,"end_time":"2022-07-08T11:59:21.167529","exception":false,"start_time":"2022-07-08T11:59:16.710787","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:53.947046Z","iopub.execute_input":"2022-07-15T09:04:53.947425Z","iopub.status.idle":"2022-07-15T09:04:54.132293Z","shell.execute_reply.started":"2022-07-15T09:04:53.947392Z","shell.execute_reply":"2022-07-15T09:04:54.130722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['peak'] = train[['hour', 'workingday']].apply(lambda x: (0, 1)[(x['workingday'] == 1 and  ( x['hour'] == 8 or 17 <= x['hour'] <= 18  or 12 <= x['hour'] <= 13)) or (x['workingday'] == 0 and  10 <= x['hour'] <= 19)], axis = 1)\ntest['peak'] = test[['hour', 'workingday']].apply(lambda x: (0, 1)[(x['workingday'] == 1 and  ( x['hour'] == 8 or 17 <= x['hour'] <= 18  or 12 <= x['hour'] <= 13)) or (x['workingday'] == 0 and  10 <= x['hour'] <= 19)], axis = 1)","metadata":{"papermill":{"duration":0.424197,"end_time":"2022-07-08T11:59:12.550736","exception":false,"start_time":"2022-07-08T11:59:12.126539","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.133331Z","iopub.status.idle":"2022-07-15T09:04:54.133990Z","shell.execute_reply.started":"2022-07-15T09:04:54.133756Z","shell.execute_reply":"2022-07-15T09:04:54.133778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. 不同月份对使用量的影响\n可以发现集中在夏季","metadata":{}},{"cell_type":"code","source":"sns.boxplot(x='month',y='count',data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:15.844843Z","start_time":"2022-05-25T13:04:15.586903Z"},"papermill":{"duration":0.428831,"end_time":"2022-07-08T11:59:12.989369","exception":false,"start_time":"2022-07-08T11:59:12.560538","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.135293Z","iopub.status.idle":"2022-07-15T09:04:54.135739Z","shell.execute_reply.started":"2022-07-15T09:04:54.135532Z","shell.execute_reply":"2022-07-15T09:04:54.135551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.不同天气\n可见天气情况对使用量有明显影响","metadata":{}},{"cell_type":"code","source":"sns.boxplot(x='weather',y='count',data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:16.298855Z","start_time":"2022-05-25T13:04:15.997959Z"},"papermill":{"duration":0.323414,"end_time":"2022-07-08T11:59:13.537386","exception":false,"start_time":"2022-07-08T11:59:13.213972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.137451Z","iopub.status.idle":"2022-07-15T09:04:54.138273Z","shell.execute_reply.started":"2022-07-15T09:04:54.138044Z","shell.execute_reply":"2022-07-15T09:04:54.138070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4.星期几对使用量的影响\n可见工作日和非工作日有明显的差距；工作日之间没有显著的差距","metadata":{}},{"cell_type":"code","source":"sns.boxplot(x='weekday',y='count',data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:16.496905Z","start_time":"2022-05-25T13:04:16.301908Z"},"papermill":{"duration":0.253561,"end_time":"2022-07-08T11:59:13.801551","exception":false,"start_time":"2022-07-08T11:59:13.547990","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.139793Z","iopub.status.idle":"2022-07-15T09:04:54.140158Z","shell.execute_reply.started":"2022-07-15T09:04:54.139981Z","shell.execute_reply":"2022-07-15T09:04:54.139998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"是否是工作日：可见工作日使用量明显高于非工作日","metadata":{}},{"cell_type":"code","source":"sns.boxplot(x='workingday',y='count',data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:16.62711Z","start_time":"2022-05-25T13:04:16.498792Z"},"papermill":{"duration":0.179734,"end_time":"2022-07-08T11:59:13.992637","exception":false,"start_time":"2022-07-08T11:59:13.812903","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.141775Z","iopub.status.idle":"2022-07-15T09:04:54.142143Z","shell.execute_reply.started":"2022-07-15T09:04:54.141967Z","shell.execute_reply":"2022-07-15T09:04:54.141983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.是否注册用户\n可见注册用户具有和【工作日中的时间-使用量】相似的使用曲线；而非注册用户则具有和【非工作日中的时间-使用量】相似的使用曲线","metadata":{}},{"cell_type":"code","source":"sns.pointplot(x='hour',y='registered',hue=None,join=True,data=train)\nsns.pointplot(x='hour',y='casual',hue=None,join=True,data=train)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:26.904869Z","start_time":"2022-05-25T13:04:25.10549Z"},"papermill":{"duration":1.606998,"end_time":"2022-07-08T11:59:22.787439","exception":false,"start_time":"2022-07-08T11:59:21.180441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.143344Z","iopub.status.idle":"2022-07-15T09:04:54.143754Z","shell.execute_reply.started":"2022-07-15T09:04:54.143567Z","shell.execute_reply":"2022-07-15T09:04:54.143584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"进一步分析可以发现，和注册用户相比，非注册用户更倾向于在非工作日中使用自行车","metadata":{}},{"cell_type":"code","source":"#注册用户\nsns.pointplot(x='hour',y='registered',hue='workingday',join=True,data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:54.145302Z","iopub.status.idle":"2022-07-15T09:04:54.145692Z","shell.execute_reply.started":"2022-07-15T09:04:54.145476Z","shell.execute_reply":"2022-07-15T09:04:54.145515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#非注册用户\nsns.pointplot(x='hour',y='casual',hue='workingday',join=True,data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:54.147341Z","iopub.status.idle":"2022-07-15T09:04:54.147703Z","shell.execute_reply.started":"2022-07-15T09:04:54.147527Z","shell.execute_reply":"2022-07-15T09:04:54.147543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.环境对使用量的影响","metadata":{}},{"cell_type":"code","source":"#温度--使用量\nfig = plt.subplots(figsize=(16,4))\nsns.pointplot(x='temp',y='count',join=True,data=train)\nplt.xticks([0,10,20,30,40])","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:29.398915Z","start_time":"2022-05-25T13:04:26.908887Z"},"papermill":{"duration":0.873692,"end_time":"2022-07-08T11:59:23.674428","exception":false,"start_time":"2022-07-08T11:59:22.800736","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.148869Z","iopub.status.idle":"2022-07-15T09:04:54.149568Z","shell.execute_reply.started":"2022-07-15T09:04:54.149328Z","shell.execute_reply":"2022-07-15T09:04:54.149348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#体感温度--使用量\nfig = plt.subplots(figsize=(16,4))\nsns.pointplot(x='atemp',y='count',join=True,data=train)\nplt.xticks([0,10,20,30,40])","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:32.010842Z","start_time":"2022-05-25T13:04:29.401075Z"},"papermill":{"duration":0.847443,"end_time":"2022-07-08T11:59:24.535766","exception":false,"start_time":"2022-07-08T11:59:23.688323","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.150707Z","iopub.status.idle":"2022-07-15T09:04:54.151067Z","shell.execute_reply.started":"2022-07-15T09:04:54.150890Z","shell.execute_reply":"2022-07-15T09:04:54.150907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#湿度-使用量\nfig = plt.subplots(figsize=(16,4))\nsns.pointplot(x='humidity',y='count',join=True,data=train)\nplt.xticks([0,10,20,30,40,50,60,70,80,90])","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:34.868518Z","start_time":"2022-05-25T13:04:32.041975Z"},"papermill":{"duration":0.867318,"end_time":"2022-07-08T11:59:25.417929","exception":false,"start_time":"2022-07-08T11:59:24.550611","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.152326Z","iopub.status.idle":"2022-07-15T09:04:54.152729Z","shell.execute_reply.started":"2022-07-15T09:04:54.152540Z","shell.execute_reply":"2022-07-15T09:04:54.152558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#风速-使用量\nfig = plt.subplots(figsize=(16,4))\nsns.pointplot(x='windspeed',y='count',join=True,data=train)\nplt.xticks([0,5,10,15,20,25])","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:37.776015Z","start_time":"2022-05-25T13:04:34.871944Z"},"papermill":{"duration":0.867796,"end_time":"2022-07-08T11:59:26.302147","exception":false,"start_time":"2022-07-08T11:59:25.434351","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.154653Z","iopub.status.idle":"2022-07-15T09:04:54.156672Z","shell.execute_reply.started":"2022-07-15T09:04:54.156422Z","shell.execute_reply":"2022-07-15T09:04:54.156443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 相关性分析\n对连续型变量分析其相关性，可见体感温度和温度有极大的相关性。事实上，体感温度取决于温度和湿度，因而可以直接去除体感温度。","metadata":{}},{"cell_type":"code","source":"corr = train[['count','temp','atemp','humidity','windspeed']].corr()\nmask = np.array(corr)\nmask[np.tril_indices_from(mask)] = False\nfig,ax = plt.subplots()\nfig.set_size_inches(15,8)\nsns.heatmap(corr,mask=mask,vmax=.8,square=True,annot=True)","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:38.093934Z","start_time":"2022-05-25T13:04:37.800695Z"},"papermill":{"duration":0.485113,"end_time":"2022-07-08T11:59:26.804332","exception":false,"start_time":"2022-07-08T11:59:26.319219","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.157768Z","iopub.status.idle":"2022-07-15T09:04:54.158633Z","shell.execute_reply.started":"2022-07-15T09:04:54.158393Z","shell.execute_reply":"2022-07-15T09:04:54.158413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:54.160034Z","iopub.status.idle":"2022-07-15T09:04:54.160686Z","shell.execute_reply.started":"2022-07-15T09:04:54.160454Z","shell.execute_reply":"2022-07-15T09:04:54.160474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 训练\n采用xgboost+random forest，分开预测casual和registered。虽然包括random forest在内的模型在cross validation上表现很不错，但实际上还是存在过拟合，且使用随机交换特征等数据增强的效果也不怎么好。\n\n由于这两种模型都是tree-based的方法，它们**不适合使用包括one-hot以及异常值去除等**数据处理方式，甚至也不需要归一化，只需要将数据处理得尽可能接近正态分布即可（取log）","metadata":{}},{"cell_type":"code","source":"rf_columns = [\n    'weather', 'temp', 'windspeed',\n    'workingday', 'season', 'holiday',\n    'hour', 'weekday', 'week', 'peak',\n]\ngb_columns =[\n    'weather', 'temp', 'humidity', 'windspeed',\n    'holiday', 'workingday', 'season',\n    'hour', 'weekday', 'year', \n]","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:39.427637Z","start_time":"2022-05-25T13:04:39.409742Z"},"papermill":{"duration":0.028407,"end_time":"2022-07-08T11:59:27.880935","exception":false,"start_time":"2022-07-08T11:59:27.852528","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.161845Z","iopub.status.idle":"2022-07-15T09:04:54.162456Z","shell.execute_reply.started":"2022-07-15T09:04:54.162247Z","shell.execute_reply":"2022-07-15T09:04:54.162266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#训练数据\nrf_x_train=train[rf_columns].values\nrf_x_test=test[rf_columns].values\n\ngb_x_train=train[gb_columns].values\ngb_x_test=test[gb_columns].values\n\ny_casual=train['casual_log'].values\ny_registered=train['registered_log'].values\ny=train['count_log'].values\n\nx_date=test['datetime'].values","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:04:39.519326Z","start_time":"2022-05-25T13:04:39.429632Z"},"papermill":{"duration":0.082501,"end_time":"2022-07-08T11:59:27.981558","exception":false,"start_time":"2022-07-08T11:59:27.899057","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.163610Z","iopub.status.idle":"2022-07-15T09:04:54.164204Z","shell.execute_reply.started":"2022-07-15T09:04:54.164010Z","shell.execute_reply":"2022-07-15T09:04:54.164029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#random forest\nfrom sklearn.ensemble import RandomForestRegressor\nparams = {'n_estimators': 1000, \n          'max_depth': 15, \n          'random_state': 0, \n          'min_samples_split' : 2, #为什么设为2：（1）因为存在0-1变量（2）存在过拟合\n          'n_jobs': -1}\n\nrf_c = RandomForestRegressor(**params)\nrf_c.fit(rf_x_train,y_casual)\nprint('casual:',rf_c.score(rf_x_train,y_casual))\n\nrf_r = RandomForestRegressor(**params)\nrf_r.fit(rf_x_train,y_registered)\nprint('registered:',rf_r.score(rf_x_train,y_registered))","metadata":{"ExecuteTime":{"end_time":"2022-05-25T13:11:38.03917Z","start_time":"2022-05-25T13:06:09.483007Z"},"papermill":{"duration":36.646543,"end_time":"2022-07-08T12:00:52.216819","exception":false,"start_time":"2022-07-08T12:00:15.570276","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.165311Z","iopub.status.idle":"2022-07-15T09:04:54.165964Z","shell.execute_reply.started":"2022-07-15T09:04:54.165765Z","shell.execute_reply":"2022-07-15T09:04:54.165784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#xgb\nimport xgboost as xgb\nxgb_c = xgb.XGBRegressor(max_depth=5, learning_rate=0.1, random_state = 0,n_estimators=200)\nxgb_c.fit(gb_x_train, y_casual)\nprint('casual:',xgb_c.score(gb_x_train,y_casual))\n\nxgb_r = xgb.XGBRegressor(max_depth=5, learning_rate=0.1, random_state = 0,n_estimators=200)\nxgb_r.fit(gb_x_train, y_registered)\nprint('registered:',xgb_r.score(gb_x_train,y_registered))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T09:04:54.167133Z","iopub.status.idle":"2022-07-15T09:04:54.167768Z","shell.execute_reply.started":"2022-07-15T09:04:54.167571Z","shell.execute_reply":"2022-07-15T09:04:54.167591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GB（没有xgb的效果好，弃之）\nfrom sklearn.ensemble import GradientBoostingRegressor\n\nparams2 = {'n_estimators': 150, \n           'max_depth': 5, \n           'random_state': 0, \n           'min_samples_leaf' : 10, \n           'learning_rate': 0.1, \n           'subsample': 0.7, \n           'loss': 'ls'}\n\ngb_c = GradientBoostingRegressor(**params2)\ngb_c.fit(gb_x_train,y_casual)\nprint('casual:',gb_c.score(gb_x_train,y_casual))\n\ngb_r = GradientBoostingRegressor(**params2)\ngb_r.fit(gb_x_train,y_registered)\nprint('registered:',gb_r.score(gb_x_train,y_registered))","metadata":{"papermill":{"duration":6.027621,"end_time":"2022-07-08T12:00:58.264147","exception":false,"start_time":"2022-07-08T12:00:52.236526","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.168926Z","iopub.status.idle":"2022-07-15T09:04:54.169566Z","shell.execute_reply.started":"2022-07-15T09:04:54.169338Z","shell.execute_reply":"2022-07-15T09:04:54.169368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_pre_count = np.exp(xgb_c.predict(gb_x_test))+np.exp(rf_r.predict(rf_x_test))-2\nxgb_pre_count = np.exp(xgb_c.predict(gb_x_test))+np.exp(xgb_r.predict(gb_x_test))-2\n\npre_count=np.round(0.2*rf_pre_count+0.8*xgb_pre_count,0)#最后记得round一下\nsubmit = pd.DataFrame({'datetime':x_date,'count':pre_count})\nsubmit.to_csv('/kaggle/working/submisssion.csv',index=False)","metadata":{"papermill":{"duration":0.567224,"end_time":"2022-07-08T12:01:00.063200","exception":false,"start_time":"2022-07-08T12:00:59.495976","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-15T09:04:54.170845Z","iopub.status.idle":"2022-07-15T09:04:54.171268Z","shell.execute_reply.started":"2022-07-15T09:04:54.171052Z","shell.execute_reply":"2022-07-15T09:04:54.171078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}