kaggle實戰之海冰面積序列的數據分析:時間序列分析(三)

本文主要為筆者學習kaggle實戰項目“Daily sea ice exten data”時心得筆記,項目主要利用NSIDC提供的每日海冰面積(sea ice extent)數據進行數據分析,學習源代碼為Mathew Savage:visualisation of sea-ice data,僅供交流參考。

3 時間序列分析

3.1 海冰的逐日變化

因為數據直接為每日數據,因此無需進行數據處理。通過想x-y折線圖表現出逐日變化。

海冰的逐日變化.png
  • 主體figure plt.figure plt.plot
    通過plt.figure(figsize=(9,3))指定紙張大小,將兩副圖疊加繪制
plt.figure(figsize=(9,3))
plt.plot(north.index,north['Extent'],label="North Hemisphere")
plt.plot(south.index,south['Extent'],label="South Hemisphere")
  • 圖例legend plt.legend
#add plot legend and titles
plt.legend(bbox_to_anchor=(0.,-.363,1.,.102),loc=3,ncol=2,mode="expand",borderaxespad=0)

bbox_to_anchor=(0.,-.363,1.,.102)指定錨點 (x,y,width,height)一般只用x,y
loc=3表示圖標位于左下,也可以使用·loc=“lower left·”這里可以省略
ncol=2表示圖標有幾列,這里是兩列
mode=expand {"expand", None}水平填充滿坐標區域擺放
borderaxespad=0 邊界與坐標軸之間的距離

  • 標題和x/y軸標簽 title&label plt.title plt.xlabel plt.ylabel
plt.ylabel("Sea ice exten(10^6 sq km)")
plt.xlabel('Data')
plt.title('Daily sea ice exten')

3.2 海冰的逐年變化

海冰的逐年變化.png

3.2.1 時間序列的resample

重采樣指將時間序列從一個頻率轉換到另外一個頻率,包括downsampling(高頻到低頻)和upsampling(低頻到高頻)

resample的相關參數:
  • freq='12m','5min','Second(15)' 采樣頻率
  • how='mean','sum','max',‘min’,'fist','last','median' 采樣方式(‘ohlc’金融計算開盤收盤最高最低的采樣方式)
  • axis=0 采樣的軸
  • closed=‘right’,'left' 即時間哪一段是包含的
  • label=‘right’,‘left’ 時間哪一段是標記的9:30-9:35 默認right即為9:35標記
  • loffset=None/‘-1s’ 用于聚合標簽調早1秒
  • kind=None 聚合到時期‘period’或‘timestamp’,默認聚集到時間序列的索引類型
  • fill_method ffile或者bfill
  • limit=none 填充期數
    需要對數據求月平均,這里使用了north.resample即panda對象的resample方法來進行重采樣
例子

各區間哪邊是閉合的?如何標記哪個?
降采樣 -聚合 close、label
ts.resample('5min',how='sum')

圖片.png

groupby采樣:ts.groupby(lambda x:x.month).mean()
ts.groupby(lambda x:x.weekday).mean()
升采樣:插值!fill_method limit
df_daily=frame.resample('D',fill_method='ffill')

圖片.png

3.2.2 對海冰序列進行降頻處理

由‘D’轉為‘12M’采樣,采樣方式為求平均

#resample raw data into annual averages
northyear=north.resample('12M',how='mean')
southyear=south.resample('12M',how='mean')

默認右邊封閉,標記右邊。因為最初和最末的數據可能會不全,因此將其刪去。

#remove the initial and final itmes as they are averageed incoorrectly
northyear=northyear[1:-1]
southyear=southyear[1:-1]

3.2.2 繪圖

#plot
plt.figure(figsize=(9,3))
plt.plot(northyear.Year,northyear['Extent'],marker='.',label='North hemisphere')
plt.plot(southyear.Year,southyear['Extent'],marker='.',label='South Hemisphere')
#add plot legend and title
plt.xlabel('Year')
plt.ylabel('Sea ice exten(10^6 sq km)')
plt.title('Annual average sea ice')
plt.xlim(1977,2016)
  • 通過plt.xlim 對坐標進行限制

3.3 海冰的逐月變化

海冰的逐月變化.png
#difine date range to plot between
start=1978
end=dt.datetime.now().year+1

畫兩幅子圖使用plt.subplots,通過設置sharex共享x軸,返回f-畫布控制對象,axarr圖形控制對象。

#defien plot
f,axarr=plt.subplots(2,sharex=True,figsize=(9,6))

設置主坐標格標注格式axarr.xaxis.set_major_formatter(mdates.DateFormatter("%b"))
繪圖時的顏色循環繪圖,因此需要漸變色
axarr.set_pro_cycle(plt.cycler('color',plt.cm.winter(np.linspace(0,1,len(range(sater,end)))))

#orgnise plot axxes
month_fmt=mdates.DateFormatter("%b")
axarr[0].xaxis.set_major_formatter(month_fmt)
axarr[0].set_prop_cycle(plt.cycler('color',plt.cm.winter(np.linspace(0,1,len(range(start,end))))))
axarr[1].set_prop_cycle(plt.cycler('color',plt.cm.winter(np.linspace(0,1,len(range(start, end))))))

設置子圖的圖例和坐標,使用axarr.set_xlabel,axarr.set_ylabel,axarr.set_title設置坐標名和標題名
axarr.add_artist(AnchoredText())添加文本框,loc指文本框位置

#add legend and title
axarr[0].set_ylabel('Sea ice extent (10^6 sq km)')
axarr[1].set_ylabel('Sea ice extent (10^6 sq km)')
axarr[1].set_xlabel('Month')
axarr[0].set_title('Annual change in sea-ice extent');
axarr[0].add_artist(AnchoredText('Northern Hemisphere', loc=3))
axarr[1].add_artist(AnchoredText('Southern Hemisphere', loc=2))

作者繪圖并不是通過計算海冰月平均來展現每月的變化。而是通過循環繪制每年的海冰變化。因此這里需要在一張圖上循環繪圖。為了使得繪圖都在同一個坐標上,認為設定將‘Year’值都定位了1972年。不需要采樣,直接繪圖即可。

# loop for every year between the start year and current
for year in range(start, end):
    # create new dataframe for each year, 
    # and set the year to 1972 so all are plotted on the same axis
    nyeardf = north[['Extent', 'Day', 'Month']][north['Year'] == year]
    nyeardf['Year'] = 1972
    nyeardf['Date'] = pd.to_datetime(nyeardf[['Year','Month','Day']])
    nyeardf.index = nyeardf['Date'].values
    
    syeardf = south[['Extent', 'Day', 'Month']][south['Year'] == year]
    syeardf['Year'] = 1972
    syeardf['Date'] = pd.to_datetime(syeardf[['Year','Month','Day']])
    syeardf.index = syeardf['Date'].values
   # plot each year individually
    axarr[0].plot(nyeardf.index,nyeardf['Extent'], label = year)
    axarr[1].plot(syeardf.index,syeardf['Extent'])

3.4 小結

本章學習重點:時間序列數據的重采樣,x-y軸圖的繪制。

3.5 完整代碼

plt.figure(figsize=(9,3))
plt.plot(north.index,north['Extent'],label="North Hemisphere")
plt.plot(south.index,south['Extent'],label="South Hemisphere")

#add plot legend and titles
#plt.legend(bbox_to_anchor=(0.,-.363,1.,.102),loc=3,ncol=2,mode="expand",borderaxespad=0)
plt.legend(bbox_to_anchor=(0.1,-0.1,0.8,0),ncol=2,mode="expand",borderaxespad=0)

plt.ylabel("Sea ice exten(10^6 sq km)")
plt.xlabel('Data')
plt.title('Daily sea ice exten')

plt.figure(figsize=(9,3))
plt.plot(north.index,north['Extent'],label="North Hemisphere")
plt.plot(south.index,south['Extent'],label="South Hemisphere")

#add plot legend and titles
#plt.legend(bbox_to_anchor=(0.,-.363,1.,.102),loc=3,ncol=2,mode="expand",borderaxespad=0)
plt.legend(bbox_to_anchor=(0.1,-0.1,0.8,0),ncol=2,mode="expand",borderaxespad=0)

plt.ylabel("Sea ice exten(10^6 sq km)")
plt.xlabel('Data')
plt.title('Daily sea ice exten')

#difine date range to plot between
start=1978
end=dt.datetime.now().year+1

#defien plot
f,axarr=plt.subplots(2,sharex=True,figsize=(9,6))

#orgnise plot axxes
month_fmt=mdates.DateFormatter("%b")
axarr[0].xaxis.set_major_formatter(month_fmt)
axarr[0].set_prop_cycle(plt.cycler('color',plt.cm.winter(np.linspace(0,1,len(range(start,end))))))
axarr[1].set_prop_cycle(plt.cycler('color',plt.cm.winter(np.linspace(0,1,len(range(start, end))))))

#add legend and title
axarr[0].set_ylabel('Sea ice extent (10^6 sq km)')
axarr[1].set_ylabel('Sea ice extent (10^6 sq km)')
axarr[1].set_xlabel('Month')
axarr[0].set_title('Annual change in sea-ice extent');
axarr[0].add_artist(AnchoredText('Northern Hemisphere', loc=3))
axarr[1].add_artist(AnchoredText('Southern Hemisphere', loc=2))

# loop for every year between the start year and current
for year in range(start, end):
  # create new dataframe for each year, 
  # and set the year to 1972 so all are plotted on the same axis
  nyeardf = north[['Extent', 'Day', 'Month']][north['Year'] == year]
  nyeardf['Year'] = 1972
  nyeardf['Date'] = pd.to_datetime(nyeardf[['Year','Month','Day']])
  nyeardf.index = nyeardf['Date'].values
  
  syeardf = south[['Extent', 'Day', 'Month']][south['Year'] == year]
  syeardf['Year'] = 1972
  syeardf['Date'] = pd.to_datetime(syeardf[['Year','Month','Day']])
  syeardf.index = syeardf['Date'].values
  
  # plot each year individually
  axarr[0].plot(nyeardf.index,nyeardf['Extent'], label = year)
  axarr[1].plot(syeardf.index,syeardf['Extent'])
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容