{"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":"**准备工作**\n\n调用相关的库","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport sklearn\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor\nfrom sklearn.linear_model import Lasso\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom datetime import datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T11:58:54.623447Z","iopub.execute_input":"2022-07-13T11:58:54.623827Z","iopub.status.idle":"2022-07-13T11:58:54.631224Z","shell.execute_reply.started":"2022-07-13T11:58:54.623798Z","shell.execute_reply":"2022-07-13T11:58:54.629678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"读取训练集、测试集","metadata":{}},{"cell_type":"code","source":"train_set = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')\ntest_set = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:38.257039Z","iopub.execute_input":"2022-07-13T10:29:38.257604Z","iopub.status.idle":"2022-07-13T10:29:38.299389Z","shell.execute_reply.started":"2022-07-13T10:29:38.257574Z","shell.execute_reply":"2022-07-13T10:29:38.298431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"检查数据是否缺失","metadata":{}},{"cell_type":"code","source":"train_set.info()\ntest_set.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:38.387361Z","iopub.execute_input":"2022-07-13T10:29:38.387946Z","iopub.status.idle":"2022-07-13T10:29:38.410630Z","shell.execute_reply.started":"2022-07-13T10:29:38.387911Z","shell.execute_reply":"2022-07-13T10:29:38.409674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:38.515709Z","iopub.execute_input":"2022-07-13T10:29:38.516336Z","iopub.status.idle":"2022-07-13T10:29:38.532235Z","shell.execute_reply.started":"2022-07-13T10:29:38.516303Z","shell.execute_reply":"2022-07-13T10:29:38.531312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:38.645902Z","iopub.execute_input":"2022-07-13T10:29:38.646585Z","iopub.status.idle":"2022-07-13T10:29:38.695969Z","shell.execute_reply.started":"2022-07-13T10:29:38.646545Z","shell.execute_reply":"2022-07-13T10:29:38.694755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**数据处理与分析**  \n\n**1.检查数据异常值**\n\n首先检查训练集count的分布情况。","metadata":{}},{"cell_type":"code","source":"plt.figure()\nsns.histplot(train_set['count'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:38.767825Z","iopub.execute_input":"2022-07-13T10:29:38.768835Z","iopub.status.idle":"2022-07-13T10:29:39.014971Z","shell.execute_reply.started":"2022-07-13T10:29:38.768797Z","shell.execute_reply":"2022-07-13T10:29:39.013684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"观察发现，count的分布呈现出明显的倾斜与长尾，故去除距离均值3个标准差以外的异常样本，并取对数，使分布更均匀。","metadata":{}},{"cell_type":"code","source":"train_set = train_set[np.abs(train_set['count']-\n                        train_set['count'].mean())<=(3*train_set['count'].std())] \nlabel = train_set['count']\nlabel_log = np.log(label)\n\nplt.figure()\nsns.histplot(label_log)\nplt.xlabel('count_log')\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:39.016960Z","iopub.execute_input":"2022-07-13T10:29:39.017325Z","iopub.status.idle":"2022-07-13T10:29:39.253011Z","shell.execute_reply.started":"2022-07-13T10:29:39.017287Z","shell.execute_reply":"2022-07-13T10:29:39.251938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"下面处理其余数据,由于需要同时处理训练集、测试集，将两个数据集合并。","metadata":{}},{"cell_type":"code","source":"data_process = pd.concat([train_set,test_set],ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:39.255002Z","iopub.execute_input":"2022-07-13T10:29:39.255314Z","iopub.status.idle":"2022-07-13T10:29:39.263955Z","shell.execute_reply.started":"2022-07-13T10:29:39.255285Z","shell.execute_reply":"2022-07-13T10:29:39.262739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**2.时间转换为具体的年、月、日、星期几**","metadata":{}},{"cell_type":"code","source":"# 时间转为具体的年、月、日\ndef split_time(date):\n    time0 = date.split(' ')\n    weekday = datetime.strptime(time0[0],'%Y-%m-%d').isoweekday()\n    time1 = date.split('-')\n    year = int(time1[0])\n    month = int(time1[1])\n    time2 = time1[2].split(' ')\n    day = int(time2[0])\n    time3 = time2[1].split(':')\n    hour = int(time3[0])\n    return [year,month,day,hour,weekday]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:39.265815Z","iopub.execute_input":"2022-07-13T10:29:39.266770Z","iopub.status.idle":"2022-07-13T10:29:39.275782Z","shell.execute_reply.started":"2022-07-13T10:29:39.266724Z","shell.execute_reply":"2022-07-13T10:29:39.274744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"column_name = ['year','month','day','hour','weekday']\nfor i in range(5):\n    data_process[column_name[i]] = list(map(lambda x: split_time(x)[i],data_process['datetime']))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:39.284894Z","iopub.execute_input":"2022-07-13T10:29:39.285300Z","iopub.status.idle":"2022-07-13T10:29:40.619410Z","shell.execute_reply.started":"2022-07-13T10:29:39.285263Z","shell.execute_reply":"2022-07-13T10:29:40.618568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_process.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:40.621084Z","iopub.execute_input":"2022-07-13T10:29:40.621638Z","iopub.status.idle":"2022-07-13T10:29:40.641592Z","shell.execute_reply.started":"2022-07-13T10:29:40.621605Z","shell.execute_reply":"2022-07-13T10:29:40.640547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3.检查其他数值变量**","metadata":{}},{"cell_type":"code","source":"# 温度\nplt.figure()\nsns.histplot(data_process['temp'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:40.642802Z","iopub.execute_input":"2022-07-13T10:29:40.643114Z","iopub.status.idle":"2022-07-13T10:29:40.883258Z","shell.execute_reply.started":"2022-07-13T10:29:40.643087Z","shell.execute_reply":"2022-07-13T10:29:40.882162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 体感温度\nplt.figure()\nsns.histplot(data_process['atemp'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:40.886384Z","iopub.execute_input":"2022-07-13T10:29:40.886825Z","iopub.status.idle":"2022-07-13T10:29:41.138234Z","shell.execute_reply.started":"2022-07-13T10:29:40.886780Z","shell.execute_reply":"2022-07-13T10:29:41.137206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 湿度\nplt.figure()\nsns.histplot(data_process['humidity'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:41.139425Z","iopub.execute_input":"2022-07-13T10:29:41.141410Z","iopub.status.idle":"2022-07-13T10:29:41.423366Z","shell.execute_reply.started":"2022-07-13T10:29:41.141364Z","shell.execute_reply":"2022-07-13T10:29:41.421535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 风速\nplt.figure()\nsns.histplot(data_process['windspeed'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:41.424792Z","iopub.execute_input":"2022-07-13T10:29:41.425113Z","iopub.status.idle":"2022-07-13T10:29:41.749826Z","shell.execute_reply.started":"2022-07-13T10:29:41.425083Z","shell.execute_reply":"2022-07-13T10:29:41.749047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"发现风速变量出现明显异常，很大一部分样本风速被记录为0，而0-5附近没有数据。将0风速视为缺失值，通过相关系数判断和风速相关的变量，采用随机森林预测。","metadata":{}},{"cell_type":"code","source":"corrDf = data_process.corr()\ncorrDf['windspeed'].sort_values(ascending =False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:41.751179Z","iopub.execute_input":"2022-07-13T10:29:41.751690Z","iopub.status.idle":"2022-07-13T10:29:41.776514Z","shell.execute_reply.started":"2022-07-13T10:29:41.751655Z","shell.execute_reply":"2022-07-13T10:29:41.775684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"根据相关系数，选择season,weather,atemp,humidity,month,hour这些特征对风速缺失值进行预测。","metadata":{}},{"cell_type":"code","source":"# 划分风速训练集（非0）与测试集（0）\ndata_process['windspeed_rf'] = data_process['windspeed']\ndata_0 = data_process[data_process['windspeed_rf']==0]\ndata_1 = data_process[data_process['windspeed_rf']!=0]\nrf_windspeed = RandomForestRegressor(n_estimators=1000,random_state=42)\ncolumns1 = ['season','weather','atemp','humidity','month','hour']\n\n# 模型训练与预测\nrf_windspeed.fit(data_1[columns1],data_1['windspeed_rf'])\nwind0 = rf_windspeed.predict(data_0[columns1])\ndata_0.loc[:,'windspeed_rf'] = wind0\ndata_process = data_1.append(data_0)\ndata_process.reset_index(inplace=True)\ndata_process.drop('index',inplace=True,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:31:02.619574Z","iopub.execute_input":"2022-07-13T10:31:02.619957Z","iopub.status.idle":"2022-07-13T10:31:34.047535Z","shell.execute_reply.started":"2022-07-13T10:31:02.619925Z","shell.execute_reply":"2022-07-13T10:31:34.046207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"预测并填补完风速值后，重新观察风速分布，比较预测前后的分布情况，修补后明显更合理。","metadata":{}},{"cell_type":"code","source":"# 风速\nplt.figure(figsize=(14,4))\nplt.subplot(1,2,1)\nsns.histplot(data_process['windspeed'])\nplt.ylabel('distribution')\nplt.subplot(1,2,2)\nsns.histplot(data_process['windspeed_rf'])\nplt.ylabel('distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:31:41.923774Z","iopub.execute_input":"2022-07-13T10:31:41.924179Z","iopub.status.idle":"2022-07-13T10:31:42.512700Z","shell.execute_reply.started":"2022-07-13T10:31:41.924144Z","shell.execute_reply":"2022-07-13T10:31:42.511446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"处理完风速后，重新绘制相关系数热力图。","metadata":{}},{"cell_type":"code","source":"plt.subplots(figsize=(12, 10))\nsns.heatmap(data_process.corr().round(2),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:31:45.600884Z","iopub.execute_input":"2022-07-13T10:31:45.602153Z","iopub.status.idle":"2022-07-13T10:31:47.007545Z","shell.execute_reply.started":"2022-07-13T10:31:45.602103Z","shell.execute_reply":"2022-07-13T10:31:47.006145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"下面进行数据可视化，观察相关系数较高的几个特征特征对count的影响。","metadata":{}},{"cell_type":"code","source":"# season\nplt.figure()\ndata_process.groupby('season')['count'].mean().plot.line()\nplt.xticks(np.arange(1,5))\nplt.grid()\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-季节变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:12.669611Z","iopub.execute_input":"2022-07-13T10:32:12.670006Z","iopub.status.idle":"2022-07-13T10:32:12.892710Z","shell.execute_reply.started":"2022-07-13T10:32:12.669976Z","shell.execute_reply":"2022-07-13T10:32:12.891647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"从图中可以发现，春季count均值明显低于其他三季节。","metadata":{}},{"cell_type":"code","source":"# weather\nplt.figure()\ndata_process.groupby('weather')['count'].mean().plot.line()\nplt.xticks(np.arange(1,5))\nplt.grid()\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-天气变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:19.986224Z","iopub.execute_input":"2022-07-13T10:32:19.986729Z","iopub.status.idle":"2022-07-13T10:32:20.238436Z","shell.execute_reply.started":"2022-07-13T10:32:19.986683Z","shell.execute_reply":"2022-07-13T10:32:20.237151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"发现异常，按照天气越差应该数量递减，但天气4出现反弹。取天气4样本进行分析。","metadata":{}},{"cell_type":"code","source":"data_process[data_process['weather']==4]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:23.987831Z","iopub.execute_input":"2022-07-13T10:32:23.988235Z","iopub.status.idle":"2022-07-13T10:32:24.009758Z","shell.execute_reply.started":"2022-07-13T10:32:23.988204Z","shell.execute_reply":"2022-07-13T10:32:24.008407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"发现其中只有1个样本为训练集，2个样本为预测值，训练集样本出现异常高值的原因可能是工作日晚高峰，因此直接删除该值。","metadata":{}},{"cell_type":"code","source":"data_process.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:28.289550Z","iopub.execute_input":"2022-07-13T10:32:28.289966Z","iopub.status.idle":"2022-07-13T10:32:28.297183Z","shell.execute_reply.started":"2022-07-13T10:32:28.289929Z","shell.execute_reply":"2022-07-13T10:32:28.296048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_process.drop(data_process.loc[(data_process['weather']==4)*(data_process['count']>0)].index,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:30.884798Z","iopub.execute_input":"2022-07-13T10:32:30.885193Z","iopub.status.idle":"2022-07-13T10:32:30.895505Z","shell.execute_reply.started":"2022-07-13T10:32:30.885162Z","shell.execute_reply":"2022-07-13T10:32:30.894384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_process.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:35.121171Z","iopub.execute_input":"2022-07-13T10:32:35.121796Z","iopub.status.idle":"2022-07-13T10:32:35.128206Z","shell.execute_reply.started":"2022-07-13T10:32:35.121759Z","shell.execute_reply":"2022-07-13T10:32:35.127486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# temp\nplt.figure()\ndata_process.groupby('temp')['count'].mean().plot()\nplt.grid()\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-温度变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:50.587016Z","iopub.execute_input":"2022-07-13T10:32:50.587412Z","iopub.status.idle":"2022-07-13T10:32:50.853032Z","shell.execute_reply.started":"2022-07-13T10:32:50.587379Z","shell.execute_reply":"2022-07-13T10:32:50.851916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"count随温度增加而递增，在36度之后下降。","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:18:23.853563Z","iopub.execute_input":"2022-07-13T10:18:23.854024Z","iopub.status.idle":"2022-07-13T10:18:23.879668Z","shell.execute_reply.started":"2022-07-13T10:18:23.853986Z","shell.execute_reply":"2022-07-13T10:18:23.878916Z"}}},{"cell_type":"code","source":"# atemp\nplt.figure()\ndata_process.groupby('atemp')['count'].mean().plot.line()\nplt.grid()\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-体感温度变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:32:57.285887Z","iopub.execute_input":"2022-07-13T10:32:57.286291Z","iopub.status.idle":"2022-07-13T10:32:57.529285Z","shell.execute_reply.started":"2022-07-13T10:32:57.286254Z","shell.execute_reply":"2022-07-13T10:32:57.528546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"count随体感温度增加而递增，略有波动。在28,44附近出现明显反弹。但查看发现这些数据太多，不是某个异常值，可能由其他因素引起。","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:47.966996Z","iopub.status.idle":"2022-07-13T10:29:47.967384Z","shell.execute_reply.started":"2022-07-13T10:29:47.967201Z","shell.execute_reply":"2022-07-13T10:29:47.967217Z"}}},{"cell_type":"code","source":"data_process[(data_process['atemp']>40)*(data_process['count']<250)].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:41:18.938584Z","iopub.execute_input":"2022-07-13T10:41:18.939022Z","iopub.status.idle":"2022-07-13T10:41:18.948032Z","shell.execute_reply.started":"2022-07-13T10:41:18.938985Z","shell.execute_reply":"2022-07-13T10:41:18.946916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_process[(data_process['atemp']>27)*(data_process['atemp']<28)*(data_process['count']<150)].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:41:13.906152Z","iopub.execute_input":"2022-07-13T10:41:13.906538Z","iopub.status.idle":"2022-07-13T10:41:13.916509Z","shell.execute_reply.started":"2022-07-13T10:41:13.906509Z","shell.execute_reply":"2022-07-13T10:41:13.915382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# humidity\nplt.figure()\ndata_process.groupby('humidity')['count'].mean().plot.line()\nplt.grid()\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-湿度变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:33:12.396017Z","iopub.execute_input":"2022-07-13T10:33:12.396399Z","iopub.status.idle":"2022-07-13T10:33:12.653574Z","shell.execute_reply.started":"2022-07-13T10:33:12.396369Z","shell.execute_reply":"2022-07-13T10:33:12.652698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# month\nplt.figure()\ndata_process.groupby('month')['count'].mean().plot.line()\nplt.grid()\nplt.xticks(np.arange(1,13))\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-月份变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:33:28.388717Z","iopub.execute_input":"2022-07-13T10:33:28.389198Z","iopub.status.idle":"2022-07-13T10:33:28.661085Z","shell.execute_reply.started":"2022-07-13T10:33:28.389149Z","shell.execute_reply":"2022-07-13T10:33:28.659955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hour\nplt.figure()\ndata_process.groupby('hour')['count'].mean().plot.line()\nplt.grid()\nplt.xticks(np.arange(0,24))\nplt.ylabel('count')\nplt.savefig('/kaggle/working/数量-小时变化图.png')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:33:42.584911Z","iopub.execute_input":"2022-07-13T10:33:42.585382Z","iopub.status.idle":"2022-07-13T10:33:42.947022Z","shell.execute_reply.started":"2022-07-13T10:33:42.585331Z","shell.execute_reply":"2022-07-13T10:33:42.946211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"从时间上看，count分布在早高峰(7-9)、晚高峰(16-19)两个时段更高，考虑到工作日会出现上下班高峰期，设一个类别变量peak表示时间是否属于上下班高峰期。","metadata":{}},{"cell_type":"code","source":"data_process['peak'] = data_process[['workingday', 'hour']].apply(lambda x: (0, 1)[x['workingday']== 1 and ((x['hour'] >=7 and x['hour'] <=9) or (x['hour'] >=16 and x['hour'] <=19))], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:50:32.787428Z","iopub.execute_input":"2022-07-13T10:50:32.787877Z","iopub.status.idle":"2022-07-13T10:50:33.251222Z","shell.execute_reply.started":"2022-07-13T10:50:32.787827Z","shell.execute_reply":"2022-07-13T10:50:33.250045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**4. 类别变量one-hot编码**","metadata":{}},{"cell_type":"code","source":"# season\nseason_name = ['spring','summer','autumn','winter']\ndata_process['season'] = list(map(lambda x: season_name[x-1],data_process['season']))\nfor i in range(4):\n    data_process[season_name[i]] = list(map(lambda x: int(x==season_name[i]),data_process['season']))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:44:20.449163Z","iopub.execute_input":"2022-07-13T10:44:20.449570Z","iopub.status.idle":"2022-07-13T10:44:20.462398Z","shell.execute_reply.started":"2022-07-13T10:44:20.449538Z","shell.execute_reply":"2022-07-13T10:44:20.461098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#year\nyear_list = [2011,2012]\nfor i in range(2):\n    data_process[str(year_list[i])] = list(map(lambda x: int(x==year_list[i]),data_process['year']))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:06:50.928729Z","iopub.execute_input":"2022-07-13T11:06:50.929212Z","iopub.status.idle":"2022-07-13T11:06:50.969214Z","shell.execute_reply.started":"2022-07-13T11:06:50.929174Z","shell.execute_reply":"2022-07-13T11:06:50.967901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"考虑到count基本随weather恶化而递减，由于训练集中已去掉weather4异常值，不再对weather做one-hot编码。","metadata":{}},{"cell_type":"code","source":"# weather\n# weather_name = ['weather1','weather2','weather3','weather4']\n# data_process['weather'] = list(map(lambda x: weather_name[x-1],data_process['weather']))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T10:29:47.977760Z","iopub.status.idle":"2022-07-13T10:29:47.978543Z","shell.execute_reply.started":"2022-07-13T10:29:47.978322Z","shell.execute_reply":"2022-07-13T10:29:47.978349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**构造与训练模型**\n\n**1.分离训练集、验证集**","metadata":{}},{"cell_type":"code","source":"data_process['count_log'] = np.log(data_process['count']+1)\ndata_process['reg_log'] = np.log(data_process['registered']+1)\ndata_process['cas_log'] = np.log(data_process['casual']+1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:33:42.691275Z","iopub.execute_input":"2022-07-13T11:33:42.691676Z","iopub.status.idle":"2022-07-13T11:33:42.700339Z","shell.execute_reply.started":"2022-07-13T11:33:42.691641Z","shell.execute_reply":"2022-07-13T11:33:42.699550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data_process[pd.notnull(data_process['count'])].sort_values(by=['datetime'])\ndata_test = data_process[~pd.notnull(data_process['count'])].sort_values(by=['datetime'])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:33:52.570841Z","iopub.execute_input":"2022-07-13T11:33:52.571234Z","iopub.status.idle":"2022-07-13T11:33:52.617374Z","shell.execute_reply.started":"2022-07-13T11:33:52.571203Z","shell.execute_reply":"2022-07-13T11:33:52.616466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"使用随机森林模型进行预测","metadata":{}},{"cell_type":"code","source":"# random forest\nparams = {'n_estimators': 500, 'max_depth': 15, 'random_state': 42}\nrf_model = RandomForestRegressor(**params)\n\nfeatures = ['spring', 'summer', 'autumn', 'winter','windspeed_rf'\n            ,'2011','2012','month','hour','humidity','temp', 'atemp'\n            ,'workingday','weekday','peak']\ntrain = data_train[features]\ntest = data_test[features]\nlabel = data_train['count_log']\nmodel1 = rf_model.fit(train,label)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:22:14.908218Z","iopub.execute_input":"2022-07-13T12:22:14.908614Z","iopub.status.idle":"2022-07-13T12:22:32.102747Z","shell.execute_reply.started":"2022-07-13T12:22:14.908578Z","shell.execute_reply":"2022-07-13T12:22:32.101915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.score(train, label)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:23:00.883765Z","iopub.execute_input":"2022-07-13T12:23:00.884195Z","iopub.status.idle":"2022-07-13T12:23:01.740464Z","shell.execute_reply.started":"2022-07-13T12:23:00.884160Z","shell.execute_reply":"2022-07-13T12:23:01.739361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rf result\npred_rf = model1.predict(test)\npred_rf = np.exp(pred_rf) - 1","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:23:11.271342Z","iopub.execute_input":"2022-07-13T12:23:11.271749Z","iopub.status.idle":"2022-07-13T12:23:11.861873Z","shell.execute_reply.started":"2022-07-13T12:23:11.271713Z","shell.execute_reply":"2022-07-13T12:23:11.860760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# write submission\ndf = pd.DataFrame()\ndf['datetime'] = test_set['datetime']\ndf['count'] = pred_rf\ndf.to_csv('submission_rf.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:23:21.041273Z","iopub.execute_input":"2022-07-13T12:23:21.042065Z","iopub.status.idle":"2022-07-13T12:23:21.082165Z","shell.execute_reply.started":"2022-07-13T12:23:21.042024Z","shell.execute_reply":"2022-07-13T12:23:21.081022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# random forest version 2\nparams = {'n_estimators': 1000, 'max_depth': 15, 'random_state': 42, 'min_samples_split' : 5}\nrf_reg = RandomForestRegressor(**params)\nrf_cas = RandomForestRegressor(**params)\n\nfeatures = ['spring', 'summer', 'autumn', 'winter','windspeed_rf'\n            ,'2011','2012','month','hour','humidity','temp', 'atemp'\n            ,'workingday','weekday','peak']\n\ntrain = data_train[features]\ntest = data_test[features]\nlabel_reg = data_train['reg_log']\nlabel_cas = data_train['cas_log']\nmodel_reg = rf_reg.fit(train, label_reg)\nmodel_cas = rf_cas.fit(train, label_cas)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:35:56.634013Z","iopub.execute_input":"2022-07-13T11:35:56.634452Z","iopub.status.idle":"2022-07-13T11:36:59.355359Z","shell.execute_reply.started":"2022-07-13T11:35:56.634418Z","shell.execute_reply":"2022-07-13T11:36:59.353808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_reg = model_reg.predict(test)\npred_reg = np.exp(pred_reg) - 1\npred_cas = model_cas.predict(test)\npred_cas = np.exp(pred_cas) - 1\npred_rf2 = pred_reg + pred_cas","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:37:52.519096Z","iopub.execute_input":"2022-07-13T11:37:52.519474Z","iopub.status.idle":"2022-07-13T11:37:54.599670Z","shell.execute_reply.started":"2022-07-13T11:37:52.519444Z","shell.execute_reply":"2022-07-13T11:37:54.598709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_reg.score(train, label_reg)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:57:06.860060Z","iopub.execute_input":"2022-07-13T11:57:06.860476Z","iopub.status.idle":"2022-07-13T11:57:08.637102Z","shell.execute_reply.started":"2022-07-13T11:57:06.860440Z","shell.execute_reply":"2022-07-13T11:57:08.636025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cas.score(train, label_cas)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:57:27.955642Z","iopub.execute_input":"2022-07-13T11:57:27.956110Z","iopub.status.idle":"2022-07-13T11:57:29.543582Z","shell.execute_reply.started":"2022-07-13T11:57:27.956071Z","shell.execute_reply":"2022-07-13T11:57:29.542514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"在训练集上的评分，分开预测不如合并预测。","metadata":{}},{"cell_type":"code","source":"# write submission\ndf2 = pd.DataFrame()\ndf2['datetime'] = test_set['datetime']\ndf2['count'] = pred_rf2\ndf2.to_csv('submission2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T11:38:10.093380Z","iopub.execute_input":"2022-07-13T11:38:10.093788Z","iopub.status.idle":"2022-07-13T11:38:10.130722Z","shell.execute_reply.started":"2022-07-13T11:38:10.093755Z","shell.execute_reply":"2022-07-13T11:38:10.129405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {'n_estimators': 400, 'max_depth': 8, 'random_state': 42, 'min_samples_leaf' : 10, 'learning_rate': 0.05, 'subsample': 0.7, 'loss': 'squared_error'}\ngbm_model = GradientBoostingRegressor(**params)\nfeatures = ['spring', 'summer', 'autumn', 'winter','windspeed_rf'\n            ,'2011','2012','month','hour','humidity','temp', 'atemp'\n            ,'workingday','weekday','peak']\ntrain = data_train[features]\ntest = data_test[features]\nlabel = data_train['count_log']\nmodel2 = gbm_model.fit(train,label)\npred_gbm = model2.predict(test)\npred_gbm = np.exp(pred_gbm) - 1\nmodel2.score(train,label)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:26:41.093062Z","iopub.execute_input":"2022-07-13T12:26:41.093428Z","iopub.status.idle":"2022-07-13T12:26:49.203764Z","shell.execute_reply.started":"2022-07-13T12:26:41.093399Z","shell.execute_reply":"2022-07-13T12:26:49.202940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# write submission\ndf_gbm = pd.DataFrame()\ndf_gbm['datetime'] = test_set['datetime']\ndf_gbm['count'] = pred_gbm\ndf_gbm.to_csv('submission_gbm.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:34.062419Z","iopub.execute_input":"2022-07-13T12:29:34.062852Z","iopub.status.idle":"2022-07-13T12:29:34.099565Z","shell.execute_reply.started":"2022-07-13T12:29:34.062817Z","shell.execute_reply":"2022-07-13T12:29:34.098592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"对非线性的变量，可能转换为类别变量能更提高效果。","metadata":{}},{"cell_type":"code","source":"params = {'n_estimators': 500, 'max_depth': 6, 'random_state': 42, 'min_samples_leaf' : 8, 'learning_rate': 0.05, 'subsample': 0.7, 'loss': 'squared_error'}\ngbm_model_1 = GradientBoostingRegressor(**params)\n\ntrain1 = train[features]\ntest1 = test[features]\nmonth_list = range(1,13)\nhour_list = range(0,24)\nfor i in range(12):\n    train1['month'+str(month_list[i])] = list(map(lambda x: int(x==month_list[i]),train1['month']))\n    test1['month'+str(month_list[i])] = list(map(lambda x: int(x==month_list[i]),test1['month']))\nfor i in range(24):\n    train1['hour'+str(hour_list[i])] = list(map(lambda x: int(x==hour_list[i]),train1['hour']))\n    test1['hour'+str(hour_list[i])] = list(map(lambda x: int(x==hour_list[i]),test1['hour'])) \ntrain1.drop(columns=['month','hour'],inplace=True)\ntest1.drop(columns=['month','hour'],inplace=True)\nlabel = data_train['count_log']\n\ngbm_1 = gbm_model_1.fit(train1,label)\npred_gbm_1 = gbm_1.predict(test1)\npred_gbm_1 = np.exp(pred_gbm_1) - 1\ngbm_1.score(train1,label)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:58:45.302124Z","iopub.execute_input":"2022-07-13T12:58:45.302514Z","iopub.status.idle":"2022-07-13T12:58:55.898660Z","shell.execute_reply.started":"2022-07-13T12:58:45.302483Z","shell.execute_reply":"2022-07-13T12:58:55.897590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# write submission\ndf_gbm = pd.DataFrame()\ndf_gbm['datetime'] = test_set['datetime']\ndf_gbm['count'] = pred_gbm_1\ndf_gbm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:57:51.645839Z","iopub.execute_input":"2022-07-13T12:57:51.646246Z","iopub.status.idle":"2022-07-13T12:57:51.685167Z","shell.execute_reply.started":"2022-07-13T12:57:51.646206Z","shell.execute_reply":"2022-07-13T12:57:51.683944Z"},"trusted":true},"execution_count":null,"outputs":[]}]}