kaggle共享单车数据分析及预测(随机森林) |
您所在的位置:网站首页 › 共享单车模型文件 › kaggle共享单车数据分析及预测(随机森林) |
文章目录
一、数据收集1.1、项目说明1.2、数据内容及变量说明
二、数据处理2.1、导入数据2.2、缺失值处理2.3、Label数据(即count)异常值处理2.4、其他数据异常值处理2.5、时间型数据数据处理
三、数据分析3.1 描述性分析3.2、探索性分析3.2.1、整体性分析3.2.2、相关性分析3.2.3、影响因素分析3.2.3.1、时段对租赁数量的影响3.2.3.2、温度对租赁数量的影响3.2.3.3、 湿度对租赁数量的影响3.2.3.4、年份、月份对租赁数量的影响3.2.3.5、季节对出行人数的影响3.2.3.6、天气情况对出行情况的影响3.2.3.7、风速对出行情况的影响3.2.3.8、日期对出行的影响
3.3、预测性分析3.3.1、选择特征值3.3.2、训练集、测试集分离3.3.3、多余特征值舍弃3.3.4、选择模型、训练模型3.3.5、预测测试集数据
一、数据收集
1.1、项目说明
自行车共享系统是一种租赁自行车的方法,注册会员、租车、还车都将通过城市中的站点网络自动完成,通过这个系统人们可以根据需要从一个地方租赁一辆自行车然后骑到自己的目的地归还。在这次比赛中,参与者需要结合历史天气数据下的使用模式,来预测D.C.华盛顿首都自行车共享项目的自行车租赁需求。 1.2、数据内容及变量说明比赛提供了跨越两年的每小时租赁数据,包含天气信息和日期信息,训练集由每月前19天的数据组成,测试集是每月第二十天到当月底的数据。
先观察温度的走势 #数据按小时统计展示起来太麻烦,希望能够按天汇总取一天的气温中位数 temp_df = Bike_data.groupby(['date','weekday'], as_index=False).agg({'year':'mean', 'month':'mean', 'temp':'median'}) #由于测试数据集中没有租赁信息,会导致折线图有断裂,所以将缺失的数据丢弃 temp_df.dropna ( axis = 0 , how ='any', inplace = True ) #预计按天统计的波动仍然很大,再按月取日平均值 temp_month = temp_df.groupby(['year','month'], as_index=False).agg({'weekday':'min', 'temp':'median'}) #将按天求和统计数据的日期转换成datetime格式 temp_df['date']=pd.to_datetime(temp_df['date']) #将按月统计数据设置一列时间序列 temp_month.rename(columns={'weekday':'day'},inplace=True) temp_month['date']=pd.to_datetime(temp_month[['year','month','day']]) #设置画框尺寸 fig = plt.figure(figsize=(18,6)) ax = fig.add_subplot(1,1,1) #使用折线图展示总体租赁情况(count)随时间的走势 plt.plot(temp_df['date'] , temp_df['temp'] , linewidth=1.3 , label='Daily average') ax.set_title('Change trend of average temperature per day in two years') plt.plot(temp_month['date'] , temp_month['temp'] , marker='o', linewidth=1.3 , label='Monthly average') ax.legend()
先观察湿度的走势: 4humidity_df = Bike_data.groupby('date', as_index=False).agg({'humidity':'mean'}) humidity_df['date']=pd.to_datetime(humidity_df['date']) #将日期设置为时间索引 humidity_df=humidity_df.set_index('date') humidity_month = Bike_data.groupby(['year','month'], as_index=False).agg({'weekday':'min', 'humidity':'mean'}) humidity_month.rename(columns={'weekday':'day'},inplace=True) humidity_month['date']=pd.to_datetime(humidity_month[['year','month','day']]) fig = plt.figure(figsize=(18,6)) ax = fig.add_subplot(1,1,1) plt.plot(humidity_df.index , humidity_df['humidity'] , linewidth=1.3,label='Daily average') plt.plot(humidity_month['date'], humidity_month['humidity'] ,marker='o', linewidth=1.3,label='Monthly average') ax.legend() ax.set_title('Change trend of average humidity per day in two years')
观察两年时间里,总租车数量随时间变化的趋势 #数据按小时统计展示起来太麻烦,希望能够按天汇总 count_df = Bike_data.groupby(['date','weekday'], as_index=False).agg({'year':'mean', 'month':'mean', 'casual':'sum', 'registered':'sum', 'count':'sum'}) #由于测试数据集中没有租赁信息,会导致折线图有断裂,所以将缺失的数据丢弃 count_df.dropna ( axis = 0 , how ='any', inplace = True ) #预计按天统计的波动仍然很大,再按月取日平均值 count_month = count_df.groupby(['year','month'], as_index=False).agg({'weekday':'min', 'casual':'mean', 'registered':'mean', 'count':'mean'}) #将按天求和统计数据的日期转换成datetime格式 count_df['date']=pd.to_datetime(count_df['date']) #将按月统计数据设置一列时间序列 count_month.rename(columns={'weekday':'day'},inplace=True) count_month['date']=pd.to_datetime(count_month[['year','month','day']]) #设置画框尺寸 fig = plt.figure(figsize=(18,6)) ax = fig.add_subplot(1,1,1) #使用折线图展示总体租赁情况(count)随时间的走势 plt.plot(count_df['date'] , count_df['count'] , linewidth=1.3 , label='Daily average') ax.set_title('Change trend of average number of rentals initiated per day in two years') plt.plot(count_month['date'] , count_month['count'] , marker='o', linewidth=1.3 , label='Monthly average') ax.legend()
在对年份月份因素的数据分析图中发现存在很多局部低谷,所以将租赁数量按季节取中位数展示,同时观察季节的温度变化 day_df=Bike_data.groupby('date').agg({'year':'mean','season':'mean', 'casual':'sum', 'registered':'sum' ,'count':'sum','temp':'mean', 'atemp':'mean'}) season_df = day_df.groupby(['year','season'], as_index=True).agg({'casual':'mean', 'registered':'mean', 'count':'mean'}) season_df .plot(figsize=(18,6),title = 'The trend of average number of rentals initiated per day changes with season')
考虑到不同天气的天数不同,例如非常糟糕的天气(4)会很少出现,查看一下不同天气等级的数据条数,再对租赁数量按天气等级取每小时平均值。 count_weather = Bike_data.groupby('weather') count_weather[['casual','registered','count']].count()
两年时间内风速的变化趋势 windspeed_df = Bike_data.groupby('date', as_index=False).agg({'windspeed_rfr':'mean'}) windspeed_df['date']=pd.to_datetime(windspeed_df['date']) #将日期设置为时间索引 windspeed_df=windspeed_df.set_index('date') windspeed_month = Bike_data.groupby(['year','month'], as_index=False).agg({'weekday':'min', 'windspeed_rfr':'mean'}) windspeed_month.rename(columns={'weekday':'day'},inplace=True) windspeed_month['date']=pd.to_datetime(windspeed_month[['year','month','day']]) fig = plt.figure(figsize=(18,6)) ax = fig.add_subplot(1,1,1) plt.plot(windspeed_df.index , windspeed_df['windspeed_rfr'] , linewidth=1.3,label='Daily average') plt.plot(windspeed_month['date'], windspeed_month['windspeed_rfr'] , marker='o', linewidth=1.3,label='Monthly average') ax.legend() ax.set_title('Change trend of average number of windspeed per day in two years')
考虑到相同日期是否工作日,星期几,以及所属年份等信息是一样的,把租赁数据按天求和,其它日期类数据取平均值 day_df = Bike_data.groupby(['date'], as_index=False).agg({'casual':'sum','registered':'sum', 'count':'sum', 'workingday':'mean', 'weekday':'mean','holiday':'mean', 'year':'mean'}) day_df.head()
节假日 由于节假日在一年中数量占比非常少,先来看一每年的节假日下有几天: holiday_coun=day_df.groupby('year', as_index=True).agg({'holiday':'sum'}) holiday_coun
根据前面的观察,决定将时段(hour)、温度(temp)、湿度(humidity)、年份(year)、月份(month)、季节(season)、天气等级(weather)、风速(windspeed_rfr)、星期几(weekday)、是否工作日(workingday)、是否假日(holiday),11项作为特征值。由于CART决策树使用二分类,所以将多类别型数据使用one-hot转化成多个二分型类别 dummies_month = pd.get_dummies(Bike_data['month'], prefix= 'month') dummies_season=pd.get_dummies(Bike_data['season'],prefix='season') dummies_weather=pd.get_dummies(Bike_data['weather'],prefix='weather') dummies_year=pd.get_dummies(Bike_data['year'],prefix='year') #把5个新的DF和原来的表连接起来 Bike_data=pd.concat([Bike_data,dummies_month,dummies_season,dummies_weather,dummies_year],axis=1) 3.3.2、训练集、测试集分离 dataTrain = Bike_data[pd.notnull(Bike_data['count'])] dataTest= Bike_data[~pd.notnull(Bike_data['count'])].sort_values(by=['datetime']) datetimecol = dataTest['datetime'] yLabels=dataTrain['count'] yLabels_log=np.log(yLabels) 3.3.3、多余特征值舍弃 dropFeatures = ['casual' , 'count' , 'datetime' , 'date' , 'registered' , 'windspeed' , 'atemp' , 'month','season','weather', 'year' ] dataTrain = dataTrain.drop(dropFeatures , axis=1) dataTest = dataTest.drop(dropFeatures , axis=1) 3.3.4、选择模型、训练模型 rfModel = RandomForestRegressor(n_estimators=1000 , random_state = 42) rfModel.fit(dataTrain , yLabels_log) preds = rfModel.predict( X = dataTrain) 3.3.5、预测测试集数据 predsTest= rfModel.predict(X = dataTest) submission=pd.DataFrame({'datetime':datetimecol , 'count':[max(0,x) for x in np.exp(predsTest)]}) submission.to_csv(r'D:\A\Data\ufo\/bike_predictions.csv',index=False) |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |