三、可視化分析
- 各個時間段單車的租賃變化
考慮到在所給的變量中,時間變量比較多,因此首先考慮各個時間段,單車的租賃情況。
首先看看,2011年和2012年,兩年中每個月平均使用情況
1 #首先看一下整體的租賃 2 fig = plt.subplots(figsize=(15,15)) 3 4 ax1 = plt.subplot2grid((4,2), (0,0), colspan=2) 5 df1 = train_2.groupby(['Month', 'Year']).mean().unstack()['count'] 6 #print(df1) 7 df1.plot(kind='area', ax=ax1, alpha=0.6,fontsize=15) 8 ax1.set_title('Monthly bicycle usage in 2011-2012',fontsize=15) 9 ax1.set_xlabel('month',fontsize=15) 10 ax1.legend(loc=0) 11 plt.show()

明顯能夠看到,在2012年,共享單車的使用情況明顯好於2011年,我們可以這么認為,共享單車在出現之初,使用量以及普及率沒有那么可觀,隨着共享單車的慢慢普及,租賃情況明顯有所上升。同時我們可以看到5-10月的使用情況最好,可能是由於天氣溫度等原因的影響。
接下來,看看這兩年中每個月的平均使用率。
1 #每月平均使用 2 fig,ax2=plt.subplots(figsize=(10,6)) 3 th=train_2.groupby('Month')['count'].mean() 4 #print(th) 5 sns.barplot(y=th.values,x=np.arange(1,13),ax=ax2) 6 ax2.set_xlabel('month',size=15) 7 ax2.set_ylabel('Avearage Count') 8 ax2.set_title('Average Count By Month') 9 plt.show()

接下來,以周為時間周期,看看每天各個時間段平均使用量
1 #一周每天的使用情況 2 fig,ax3=plt.subplots(figsize=(10,6)) 3 season_week=train_2.groupby(['Hour','Weekday']).mean().unstack()['count'] 4 #season_week.columns=['Spring','Summer','Fall','Winter'] 5 season_week.columns=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] 6 season_week.plot(ax=ax3,style='-.') 7 plt.show()
從上圖我們可以看出,周一到周五中,一天中早上8-9點和下午5-7點,使用量比較多,可能是早上上班時間和晚上下班時間導致的,同時包含外出吃飯的原因,而對於周末,時間比較集中,基本在11點到下午5點左右使用量比較多,這個時間估計是大家的休閑時間。
然后,我們看看各個季節,一天中各個時段,單車的使用情況
1 #每個季節一天平均使用量 2 fig,ax4=plt.subplots(figsize=(10,6)) 3 season_hour=train_1.groupby(['Hour','season']).mean().unstack()['count'] 4 season_hour.columns=['Spring','Summer','Fall','Winter'] 5 #print(season_hour) 6 season_hour.plot(ax=ax4, style='--.') 7 ax4.set_title('Average Hourly Use of Shared Bicycles in a U.S. City in 2011-2012') 8 ax4.set_xlabel('hour') 9 ax4.set_xticks(list(range(24))) 10 ax4.set_xticklabels(list(range(24))) 11 ax4.set_xlim(0,23) 12 plt.show()
從上圖我們可以看出,春天的使用量明顯偏少,可能是溫度較低的原因,同時,早上八點和晚上5點左右使用較多,與我們上面分析一樣。
最后展示一下在節假日時,單車的使用情況
1 #接着看看節假日單車使用情況 2 fig,ax5=plt.subplots(figsize=(10,6)) 3 #ax5 = plt.subplot2grid((4,2), (3,0), colspan=2) 4 df51 = train_2.groupby(['Hour', 'holiday']).mean().unstack()['count'].rename(columns={0:'non holiday',1:'holiday'}) 5 df52 = train_2.groupby(['Hour', 'workingday']).mean().unstack()['count'].rename(columns={0:'weekend',1:'workday'}) 6 df51.plot(ax=ax5, style='-.') 7 df52.plot(ax=ax5, style='-o') 8 ax5.set_title('Average Hourly Use of Shared Bicycles in a U.S. City in 2011-2012') 9 ax5.set_xlabel('hour') 10 ax5.set_xticks(list(range(24))) 11 ax5.set_xticklabels(list(range(24))) 12 ax5.set_xlim(0,23) 13 plt.savefig('hol.jpg') 14 plt.show()

明顯可以看到,周末和工作日,單車的使用主要集中在不同時間,而節假日和非節假日使用時間比較接近,但是節假日使用明顯高於非節假日。
- 天氣對單車租賃的影響
1 #天氣的影響 2 fig,ax6=plt.subplots(figsize=(8,5)) 3 weather_1=train_2.groupby('weather')['count'].sum() 4 weather_2=train_2.groupby('weather')['count'].mean() 5 weather_=pd.concat([weather_1,weather_2],axis=1).reset_index() 6 weather_.columns = ['weather', 'sum', 'mean'] 7 #weather_['sum'].plot(kind='bar', width=0.6, ax=ax6,color='green') 8 sns.barplot(x=weather_['weather'],y=weather_['sum'],data=weather_,edgecolor='black',linewidth=0.9)#edgecolor柱形邊緣顏色 9 weather_['mean'].plot(style='b--o', alpha=0.6, ax=ax6,secondary_y=True) 10 plt.grid() 11 plt.xlim(-1,4) 12 ax6.set_xlabel('weather') 13 ax6.set_ylabel('sum') 14 ax6.set_xticks(range(4)) 15 ax6.set_xticklabels(['sunny','cloudy','light snow','heavy snow']) 16 ax6.set_title('Shared bicycle usage in different weather in a city of the United States in 2011-2012') 17 plt.show()
1 fig,ax7=plt.subplots(figsize=(8,5)) 2 #ax8=ax7.twinx() 3 wind_1=train_2.groupby('windspeed').sum()['count'] 4 wind_2=train_2.groupby('windspeed').mean()['count'] 5 wind_=pd.concat([wind_1,wind_2],axis=1).reset_index() 6 wind_.columns = ['windspeed', 'sum', 'mean'] 7 wind_['sum'].plot(style='-o', ax=ax7, alpha=0.4,color='g') 8 wind_['mean'].plot(style='b--.', alpha=0.6, ax=ax7, secondary_y=True, label='平均值') 9 ax7.set_label('windspeed') 10 plt.xlim(0,30) 11 plt.grid() 12 ax7.set_xlabel('windspeed') 13 ax7.set_ylabel('count') 14 ax7.set_title('Shared bicycle usage at different windspeeds in a city of the United States in 2011-2012') 15 plt.show()
第一幅圖反映出天氣狀況對租車數量的影響,柱狀圖是租車總量,天氣越好租車總量越多,但天氣好的時候多毫無疑問也造成了這個結果。
折線圖反映的是平均值,目的就是消除各種天氣的天數不同的影響,然而反常的是大雨大雪大霧天氣情況下租車數量的平均值居然大幅提升,這一反常現象同樣出現在右圖中,當風速越高,租車總量趨近於0的時候,平均數反而最高,我們來查看一下原始數據:
1 train_1[train_1['weather']==4] 2 train_1[train_1['windspeed']>50]
通過查閱原始數據,我們發現這種極端天氣出現的次數很少,所以導致這種極端情況。
- 溫度及濕度對租賃的影響
1 #濕度月平均變化 2 humidity_m= train_2.groupby(['Year','Month']).mean()['humidity'] 3 fig,ax8=plt.subplots(figsize=(15,5)) 4 humidity_m.plot(marker='o',color='r') 5 plt.grid() 6 plt.legend() 7 ax8.set_title('Change trend of average humidity per day in 2011-2012') 8 plt.savefig('wen.jpg') 9 plt.show()
1 humidity_c = train_1.groupby(['humidity'], as_index=True).agg({'casual':'mean', 'registered':'mean', 'count':'mean'}) 2 humidity_c.plot(style='-.',figsize=(15,5),title='Average number of rentals initiated per hour in different humidity') 3 plt.grid() 4 plt.legend() 5 plt.savefig('wen2.jpg') 6 plt.show()
第一張圖顯示的是一年中濕度的變化,第二張圖展示的是不同濕度對租賃的影響,可以看到,在濕度為20的時候,達到租賃最高峰,隨着濕度增加,租賃的數量逐漸減小。
接下來,看看溫度的影響。
1 #溫度月平均變化 2 temp_m= train_2.groupby(['Year','Month']).mean()['temp'] 3 temp_m1 = train_2.groupby(['temp'], as_index=True).agg({'casual':'mean', 'registered':'mean', 'count':'mean'}) 4 temp_m.plot(marker='o',color='r',figsize=(15,3)) 5 plt.grid() 6 plt.legend() 7 temp_m1.plot(style='-.',figsize=(15,3),title='Average number of rentals initiated per hour in different temp') 8 plt.grid() 9 plt.legend() 10 plt.savefig('wen3.jpg') 11 plt.show()
第一張圖展示的是各個月份溫度的變化,第二張表明在不同溫度下,共享單車的租賃數量的變化,可以看到,當溫度較低時,租賃較小,隨着溫度慢慢上升,租賃有所升高,30-38度左右為最好。
最后我們嘗試一下多變量圖
1 #多變量 2 sns.pairplot(train_2[['temp','windspeed','humidity','count']]) 3 plt.show()