Python高級應用課程設計作業——自然災害大數據分析與可視化


一、選題背景

自然災害對人類生活構成嚴重威脅,因此開展災害數據分析與評估,了解特定地區、災害的發生規律,了解自然災害的致災因子對自然、社會、經濟和環境所造成的影響,以及短期和長期變化方式,並在此基礎上采取行動,降低自然災害風險,減少自然災害對社會經濟和人們生命財產所造成的損失,在自然災害頻發的當下,要對自然災害有更多的了解,自然災害數據的分析和可視化能呈現更直觀的災害信息。

二、大數據

1.分析的數據集

 

  • fema_declaration_string:用於唯一識別斯塔福德法案聲明的機構標准方法,串聯declaration_type,disaster_numberstate
  • disaster_number:用於指定宣布為災難的事件或事件的順序分配編號。
  • state:美國的州、地區或領地。
  • declaration_type:“DR”(= 重大災害)、“EM”(= 應急管理)或“FM”(=“火災管理”)之一
  • declaration_date: 宣布災難的日期。
  • fy_declared: 災難開始的年份。
  • incident_type:事件類型,例如“火災”、“洪水”或“颶風”。事件類型將影響可用的援助類型。
  • declaration_title: 災難的標題。這可能是一個有用的標識符,例如“卡特里娜颶風”或“Covid-19 大流行”。
  • ih_program_declared:二進制標志,指示是否為此災難宣布了“個人和家庭計划”。
  • ia_program_declared:二進制標志,指示是否為此災難宣布了“個人援助計划”。
  • pa_program_declared:二進制標志,指示是否為此災難宣布了“公共援助計划”。
  • hm_program_declared:指示是否為此災難宣布了“減災計划”的二進制標志。
  • incident_begin_date:事件本身開始的日期。
  • incident_end_date:事件本身結束的日期。
  • disaster_closeout_date:所有災害結束的日期。
  • fips:5 位 FIPS 縣代碼;用於標識美國、哥倫比亞特區、美國領土、美國邊遠地區和自由聯系州的縣。
  • place_code:FEMA 內部使用的唯一代碼系統來識別采用數字“99”+ 3 位縣 FIPS 代碼的位置。有一些聲明的位置沒有可識別的 FIPS 縣代碼,在這種情況下分配了唯一標識符。
  • designated_area:描述包含在聲明中的美國縣的名稱或短語。可以取值“全州”。
  • declaration_request_number:分配給請求災難聲明的唯一 ID。
  • hash: MD5 記錄的字段和值的哈希值。
  • last_refresh:FEMA 上次更新記錄的日期。
  • id:分配給記錄的唯一 ID。
  • 總計62715條數據

2.數據分析課程設計方案概述

1.對數據進行基本的分組和分析

2.用圖形來分析災害的頻率

3.依照所得的圖形得出相應的推論

三、數據分析步驟

1.數據源:https://www.kaggle.com/headsortails/us-natural-disaster-declarations

2.導入包,導入數據集。

 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 import seaborn as sns
 5 import warnings
 6 warnings.filterwarnings('ignore')
 7 from datetime import datetime
 8 #讀取數據集
 9 filename = 'D:/us_disaster_declarations.csv'
10 df = pd.read_csv(filename)

 

    數據清洗

  1.進行初步的數據清洗。

  (1)刪除與本次數據分析無關的列:
 
(2)空值處理
   
     (3)  重復數據刪除
     
      (4)空格處理
 1 #刪除與本次數據分析無關的列
 2 df.drop('hash',axis=1,inplace=True)#刪除記錄的字段和值的哈希值列,對本次實驗無用數據
 3 df.drop('last_refresh',axis=1,inplace=True)#刪除上次更新記錄的日期列,對本次實驗無用數據
 4 df.drop('id',axis=1,inplace=True)#刪除ID列,對本次實驗無用數據
 5 
 6 #空值處理
 7 df['disaster_number'].isnull().value_counts()#判斷關鍵字段是否存在空值
 8 df['incident_type'].isnull().value_counts()#判斷關鍵字段是否存在空值
 9 df['state'].isnull().value_counts()#判斷關鍵字段是否存在空值
10 df['ih_program_declared'].isnull().value_counts()#判斷關鍵字段是否存在空值
11 df['fy_declared'].isnull().value_counts()#判斷關鍵字段是否存在空值
12 df['disaster_closeout_date'].isnull().value_counts()#判斷關鍵字段是否存在空值
13 df['declaration_request_number'].isnull().value_counts()#判斷關鍵字段是否存在空值
14 df['fips'].isnull().value_counts()#判斷關鍵字段是否存在空值
15 df['incident_begin_date'].isnull().value_counts()#判斷關鍵字段是否存在空值
16 
17 #刪除重復數據
18 df = df.drop_duplicates()
19 
20 #空格處理
21 df['fema_declaration_string']=df['fema_declaration_string'].map(str.strip)
22 df['state']=df['state'].map(str.strip)
23 df['declaration_type']=df['declaration_type'].map(str.strip)
24 df['declaration_date']=df['declaration_date'].map(str.strip)
25 df['incident_type']=df['incident_type'].map(str.strip)
26 df['declaration_title']=df['declaration_title'].map(str.strip)
27 df['designated_area']=df['designated_area'].map(str.strip)
28 df['incident_begin_date']=df['incident_begin_date'].map(str.strip)
29 
30 df.head()

 

 2.進一步數據清理:通過describe()函數查詢異常值

1 #異常值處理
2 df.describe()

 未發現異常值

 

 三、數據可視化

 1.用不同圖形繪制不同災害發生頻率圖

1 #提取數據
2 E_df = df[df['incident_type'] == 'Earthquake']#提取出災難類型為地震的數據
3 T_df = df[df['incident_type'] == 'Tornado']#提取出災難類型為龍卷風的數據
4 F_df = df[df['incident_type'] == 'Flood']#提取出災難類型為洪水的數據

 1 E_df = df[df['incident_type'] == 'Earthquake']#提取出災難類型為地震的數據
 2 
 3 #提取數據
 4 a=np.array(E_df['fy_declared'])
 5 b=np.array(E_df['declaration_title'])
 6 
 7 #繪制地震散點圖
 8 plt.figure(figsize=(50,15))
 9 plt.xlabel("日期")
10 plt.ylabel("地震")
11 plt.plot(a,b,linewidth=5.0,c='y',label="地震")
12 plt.title('Number of earthquakes in each year')
13 plt.show()

 1 T_df = df[df['incident_type'] == 'Tornado']#提取出災難類型為龍卷風的數據
 2 
 3 #提取數據
 4 a=np.array(T_df['fy_declared'])
 5 b=np.array(T_df['declaration_title'])
 6 
 7 #繪制龍卷風的散點圖
 8 plt.figure(figsize=(30,15))
 9 plt.xlabel("日期")
10 plt.ylabel("龍卷風")
11 plt.scatter(a,b,linewidth=3.0,c='r',label="龍卷風")
12 plt.title('Number of Tornado in each year')
13 plt.show()

 1 F_df = df[df['incident_type'] == 'Flood']#提取出災難類型為洪水的數據
 2 
 3 #提取數據
 4 a=np.array(F_df['fy_declared'])
 5 b=np.array(F_df['declaration_title'])
 6 
 7 #繪制洪水的散點圖
 8 plt.figure(figsize=(50,30))
 9 plt.xlabel("日期")
10 plt.ylabel("洪水")
11 plt.plot(a,b,linewidth=5.0,c='b',label="洪水")
12 plt.title('Number of Flood in each year')
13 plt.show()

 

 

 

由圖我們可以看出,洪水的發生頻次遠大於地震與龍卷風,並且隨着年份的增加發生次數也有相應的增長。

 

1 #調取各州所對應的自然災害數據
2 m=df[['state','incident_type']].groupby('state').describe()
3 m['incident_type'].sort_values(by='count',ascending=False).head(10)

 

1 #事件數量
2 fig,axes=plt.subplots(2,2)
3 sns.distplot(df['incident_type'].value_counts(),ax=axes[0][0])
4 sns.distplot(df['incident_type'].value_counts(),kde=False,ax=axes[0][1])
5 sns.distplot(df['incident_type'].value_counts(),rug=True,ax=axes[1][0])
6 sns.distplot(df['incident_type'].value_counts(),vertical=True,ax=axes[1][1])

 

 

 

1 #各種自然災害發生的頻率
2 m=df[['fy_declared','incident_type']].groupby('fy_declared').describe()['incident_type'].reset_index()
3 plt.figure(figsize=(10,5))
4 plt.title('Frequency of various natural disasters')
5 sns.barplot(x='top',y='freq',data =m)

由圖我們可以直觀的看出不同災害的發生頻率,由於數據集中加入了生物災害,2020新冠爆發,所以生物災害發生頻率遠大於其他災害。

 

2.畫出年份與災害發生次數的回歸圖

1 #年份與自然災害發生次數的回歸圖
2 m=df['fy_declared'].value_counts().reset_index()
3 sns.lmplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'),
4            aspect=1.7, height=5,markers=['o'], scatter_kws={'s':20});
5 plt.title('Regression diagram of year and number of natural disasters')

由此回歸圖可以看出,從1960年到2020年美國每年發生災害的次數呈線性增加

 

3.接下來制作各個州受災所占比例的餅圖和柱狀圖

1.餅圖

1 #調取州所對應的數據
2 d=m.sort_values(by='state').reset_index().head(10)

1 #用餅圖來展現每個州的受自然災害影響的情況
2 d=m.sort_values(by='state').reset_index()
3 c=d['state']
4 a=d['index']
5 e=np.array(a)
6 plt.pie(c ,labels=e,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
7 plt.title('Disaster situation in each state')
8 plt.figure(figsize=(30,30))

 

2.柱狀圖

1 #調取州所對應的事件類型
2 S = df[['state','incident_type']].groupby('state').count().head(10)

1 #用柱狀圖來展現每個州的受自然災害影響的情況
2 S = df[['state','incident_type']].groupby('state').count()
3 S.reset_index(inplace=True)
4 S=S.sort_values(by='incident_type',ascending=False)
5 plt.figure(figsize=(30,10))
6 sns.barplot(x="state", y="incident_type", data=S,)
7 plt.title('Impact of natural disasters in each state')
8 total_width,n=0.8,3#設置間隔
9 plt.grid()#增加網格

 

3.熱力圖

1 #熱力圖來展現各州在不同年份受自然災害的影響情況
2 pt = df.pivot_table(values ='disaster_number', index = 'state', columns = 'fy_declared').fillna(0)
3 plt.figure(figsize=(10,8))
4 sns.heatmap(pt, cmap='coolwarm');
5 plt.savefig('heatmap.png')
6 plt.title('Disaster heat map')

結合圖像可以直觀的看出:TX(德克薩斯州)是美國受自然災害影響最大的州,PW是受自然災害影響最小的,GA(佐治亞州)的受災情況接近平均值。

 

4.下面我們就德克薩斯州和佐治亞州進行具體的分析

畫出德克薩斯州各個年份受自然災害影響情況

1 #德克薩斯州
2 m=df.query('state=="TX"')['fy_declared'].value_counts().reset_index()
3 plt.figure(figsize=(30,15))
4 sns.barplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'));
5 plt.title('Impact of natural disasters in Texas in each year')
6 plt.grid()

1 #佐治亞州
2 a=df.query('state=="GA"')['fy_declared'].value_counts().reset_index()
3 plt.figure(figsize=(30,15))
4 sns.barplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'));
5 plt.title('Impact of natural disasters in Georgia in each year')
6 plt.grid()

 1 #德克薩斯州年份與受災情況的回歸圖
 2 m=df.query('state=="TX"')['fy_declared'].value_counts().reset_index()
 3 sns.regplot(x='index',y='fy_declared', data=m.sort_values(by='fy_declared'),
 4             color='r',marker='+')
 5 
 6 #佐治亞州年份與受災情況的回歸圖
 7 a=df.query('state=="GA"')['fy_declared'].value_counts().reset_index()
 8 sns.regplot(x='index',y='fy_declared', data=a.sort_values(by='fy_declared'),
 9             color='g',marker='*')
10 
11 plt.title('Regression of years and disaster conditions in Texas and Georgia')

 

 從柱狀圖中可以看出德克薩斯州在2005年時受自然災害影響最大,每年自然災害的發生的次數有相應的降低,佐治亞州在2017年時受自然災害影響最大。

從回歸圖中可以看出在1975年以前佐治亞州每年自然災害發生次數大於德克薩斯州,1975年以后德克薩斯州每年自然災害發生次數大於佐治亞洲。

 

5. 用餅圖分析德克薩斯州和佐治亞州各種自然災害所占比例

1 #佐治亞州各種自然災害所占比例
2 a=df.query('state=="GA"')['incident_type'].value_counts().reset_index()
3 b=a['index']
4 c=np.array(b)
5 d=a['incident_type']
6 plt.pie(d,labels=b,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
7 plt.title('Proportion of natural disasters in Georgia')
8 plt.figure(figsize=(20,6.5))

1 #德克薩斯州各種自然災害所占比例
2 m=df.query('state=="TX"')['incident_type'].value_counts().reset_index()
3 b=m['index']
4 c=np.array(b)
5 d=m['incident_type']
6 plt.pie(d,labels=b,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
7 plt.title('Proportion of various natural disasters in Texas')
8 plt.figure(figsize=(20,6.5))

 

有圖可知,颶風和火災是德克薩斯州常發的自然災難,颶風和強冰暴是佐治亞州常發的自然災害

 

1 #查詢受自然災害影響最大的郡
2 df[['designated_area','incident_type']].groupby('designated_area').count().sort_values(by='incident_type',ascending=False).head(10)

華盛頓郡受自然災害影響最大。

 

6.自然災害的聲明類型有三種“DR" (重大災害), "EM" (緊急災難), or "FM" (= "火災")

1 #災害聲明類型統計
2 df['declaration_type'].value_counts()

 在所有數據中重大災害有42378條,緊急災害有18508條,火災有1828條

 

用盒圖和柱狀圖來展現災害聲明類型

1 #用盒圖來展現災害聲明類型
2 m=df[['fy_declared','declaration_type']]
3 plt.figure(figsize=(8,5))
4 sns.boxplot(x='declaration_type',y='fy_declared',data=m);
5 plt.title('Disaster declaration type')

1 #用柱狀圖來展現災害聲明類型
2 fig,axes=plt.subplots(1,2)
3 plt.figure(figsize=(10,5))
4 sns.barplot(x='index',y='declaration_type',data=df['declaration_type'].value_counts().reset_index(),ax=axes[0])
5 sns.barplot(x='declaration_type',y='index',data=df['declaration_type'].value_counts().reset_index(),ax=axes[1])
6 plt.title('Disaster declaration type')

 用盒圖和柱狀圖我們可以更直觀的看出災害聲明類型的數量分布

 

 7.德克薩斯州和佐治亞州公共援助計划占比

 

   pa_program_declared:二進制標志,指示是否為此災難宣布了“公共援助計划”。(1有, 0沒有)

  • ia_program_declared:二進制標志,指示是否為此災難宣布了“個人援助計划”。(1有, 0沒有) 
1 #德克薩斯州
2 #二進制標志,指示是否為此災難宣布了“公共援助計划"(1有 0沒有)
3 m=df.query('state=="TX"')['pa_program_declared'].value_counts().reset_index()
4 b=m['index']
5 c=np.array(b)
6 d=m['pa_program_declared']
7 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
8 plt.title('Did the state of Texas announce a public aid program for the disaster')
9 plt.figure(figsize=(20,6.5))

1 #佐治亞洲
2 #二進制標志,指示是否為此災難宣布了“公共援助計划"(1有 0沒有)
3 m=df.query('state=="GA"')['pa_program_declared'].value_counts().reset_index()
4 b=m['index']
5 c=np.array(b)
6 d=m['pa_program_declared']
7 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
8 plt.title('Did Georgia announce a public assistance program for the disaster')
9 plt.figure(figsize=(20,6.5))

1 #德克薩斯州
2 #二進制標志,指示是否為此災難宣布了“個人援助計划"(1有 0沒有)
3 m=df.query('state=="TX"')['ia_program_declared'].value_counts().reset_index()
4 b=m['index']
5 c=np.array(b)
6 d=m['ia_program_declared']
7 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
8 plt.title('Indicates whether a "personal assistance plan" has been announced for this disaster')
9 plt.figure(figsize=(20,6.5))

1 #佐治亞州
2 #二進制標志,指示是否為此災難宣布了“個人援助計划"(1有 0沒有)
3 m=df.query('state=="GA"')['ia_program_declared'].value_counts().reset_index()
4 b=m['index']
5 c=np.array(b)
6 d=m['ia_program_declared']
7 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
8 plt.title('Indicates whether a "personal assistance plan" has been announced for this disaster')
9 plt.figure(figsize=(20,6.5))

1 #年份與是否執行公共援助計划情況的回歸圖
2 m=df.query('pa_program_declared==1')['fy_declared'].value_counts().reset_index()
3 sns.regplot(x='index',y='fy_declared', data=m.sort_values(by='fy_declared'),
4             color='b',marker='+')
5 plt.title('And whether the public assistance program is implemented')

 

由餅圖可知佐治亞洲的公共援助服務比德克薩斯州好,公共援助率達到了95.99%

由回歸圖我們可以看出美國公共援助服務量不斷增加。

 

 四、完整代碼

 

  1 import numpy as np
  2 import pandas as pd
  3 import matplotlib.pyplot as plt
  4 import seaborn as sns
  5 import warnings
  6   warnings.filterwarnings('ignore')
  7 from datetime import datetime
  8   #讀取數據集
  9   filename = 'D:/us_disaster_declarations.csv'
 10  df = pd.read_csv(filename)
 11 
 12 #刪除與本次數據分析無關的列
 13   df.drop('hash',axis=1,inplace=True)#刪除記錄的字段和值的哈希值列,對本次實驗無用數據
 14   df.drop('last_refresh',axis=1,inplace=True)#刪除上次更新記錄的日期列,對本次實驗無用數據
 15  df.drop('id',axis=1,inplace=True)#刪除ID列,對本次實驗無用數據
 16  
 17   #空值處理
 18   df['disaster_number'].isnull().value_counts()#判斷關鍵字段是否存在空值
 19   df['incident_type'].isnull().value_counts()#判斷關鍵字段是否存在空值
 20   df['state'].isnull().value_counts()#判斷關鍵字段是否存在空值
 21  df['ih_program_declared'].isnull().value_counts()#判斷關鍵字段是否存在空值
 22  df['fy_declared'].isnull().value_counts()#判斷關鍵字段是否存在空值
 23  df['disaster_closeout_date'].isnull().value_counts()#判斷關鍵字段是否存在空值
 24  df['declaration_request_number'].isnull().value_counts()#判斷關鍵字段是否存在空值
 25  df['fips'].isnull().value_counts()#判斷關鍵字段是否存在空值
 26  df['incident_begin_date'].isnull().value_counts()#判斷關鍵字段是否存在空值
 27  
 28  #刪除重復數據
 29  df = df.drop_duplicates()
 30  
 31  #空格處理
 32  df['fema_declaration_string']=df['fema_declaration_string'].map(str.strip)
 33  df['state']=df['state'].map(str.strip)
 34  df['declaration_type']=df['declaration_type'].map(str.strip)
 35  df['declaration_date']=df['declaration_date'].map(str.strip)
 36  df['incident_type']=df['incident_type'].map(str.strip)
 37  df['declaration_title']=df['declaration_title'].map(str.strip)
 38  df['designated_area']=df['designated_area'].map(str.strip)
 39  df['incident_begin_date']=df['incident_begin_date'].map(str.strip)
 40  
 41  df.head()
 42 #異常值處理
 43 df.describe()
 44 
 45 E_df = df[df['incident_type'] == 'Earthquake'].head(5)#提取出災難類型為地震的數據
 46 T_df = df[df['incident_type'] == 'Tornado']#提取出災難類型為龍卷風的數據
 47 F_df = df[df['incident_type'] == 'Flood']#提取出災難類型為洪水的數據
 48 
 49 E_df = df[df['incident_type'] == 'Earthquake']#提取出災難類型為地震的數據
 50 
 51   #提取數據
 52  a=np.array(E_df['incident_begin_date'])
 53  b=np.array(E_df['declaration_title'])
 54   
 55   #繪制地震的折線圖
 56   plt.figure(figsize=(50,15))
 57  plt.xlabel("日期")
 58  plt.ylabel("地震")
 59  plt.plot(a,b,linewidth=5.0,c='y',label="地震")
 60  plt.title('Number of earthquakes in each year')
 61  plt.show()
 62 
 63 T_df = df[df['incident_type'] == 'Tornado']#提取出災難類型為龍卷風的數據
 64 
 65   #提取數據
 66   a=np.array(T_df['incident_begin_date'])
 67   b=np.array(T_df['declaration_title'])
 68  
 69   #繪制龍卷風的折線圖
 70   plt.figure(figsize=(50,15))
 71  plt.xlabel("日期")
 72  plt.ylabel("龍卷風")
 73  plt.plot(a,b,linewidth=5.0,c='r',label="龍卷風")
 74  plt.title('Number of Tornado in each year')
 75  plt.show()
 76 
 77 F_df = df[df['incident_type'] == 'Flood']#提取出災難類型為洪水的數據
 78   
 79   #提取數據
 80   a=np.array(F_df['incident_begin_date'])
 81   b=np.array(F_df['declaration_title'])
 82   
 83   #繪制洪水的折線圖
 84  plt.figure(figsize=(50,30))
 85  plt.xlabel("日期")
 86 plt.ylabel("洪水")
 87  plt.plot(a,b,linewidth=5.0,c='b',label="洪水")
 88  plt.title('Number of Flood in each year')
 89 plt.show()
 90 
 91 #調取各州所對應的自然災害數據
 92 m=df[['state','incident_type']].groupby('state').describe()
 93 m['incident_type'].sort_values(by='count',ascending=False).head(10)
 94 
 95 #事件數量
 96 fig,axes=plt.subplots(2,2)
 97  sns.distplot(df['incident_type'].value_counts(),ax=axes[0][0])
 98  sns.distplot(df['incident_type'].value_counts(),kde=False,ax=axes[0][1])
 99  sns.distplot(df['incident_type'].value_counts(),rug=True,ax=axes[1][0])
100  sns.distplot(df['incident_type'].value_counts(),vertical=True,ax=axes[1][1])
101 
102 #各種自然災害發生的頻率
103 m=df[['fy_declared','incident_type']].groupby('fy_declared').describe()['incident_type'].reset_index()
104 plt.figure(figsize=(10,5))
105 plt.title('Frequency of various natural disasters')
106 sns.barplot(x='top',y='freq',data =m)
107 
108 #年份與自然災害發生次數的回歸圖
109 m=df['fy_declared'].value_counts().reset_index()
110 sns.lmplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'),
111             aspect=1.7, height=5,markers=['o'], scatter_kws={'s':20});
112  plt.title('Regression diagram of year and number of natural disasters')
113 
114 #調取州所對應的數據
115 d=m.sort_values(by='state').reset_index().head(10)
116 
117 #用餅圖來展現每個州的受自然災害影響的情況
118 d=m.sort_values(by='state').reset_index()
119 c=d['state']
120 a=d['index']
121 e=np.array(a)
122 plt.pie(c ,labels=e,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
123 plt.title('Disaster situation in each state')
124 plt.figure(figsize=(30,30))
125 
126 #調取州所對應的事件類型
127 S = df[['state','incident_type']].groupby('state').count().head(10)
128 
129 #用柱狀圖來展現每個州的受自然災害影響的情況
130 S = df[['state','incident_type']].groupby('state').count()
131 S.reset_index(inplace=True)
132  S=S.sort_values(by='incident_type',ascending=False)
133 plt.figure(figsize=(30,10))
134 sns.barplot(x="state", y="incident_type", data=S,)
135  plt.title('Impact of natural disasters in each state')
136 total_width,n=0.8,3#設置間隔
137 plt.grid()#增加網格
138 
139 #熱力圖來展現各州在不同年份受自然災害的影響情況
140 pt = df.pivot_table(values ='disaster_number', index = 'state', columns = 'fy_declared').fillna(0)
141 plt.figure(figsize=(10,8))
142 sns.heatmap(pt, cmap='coolwarm');
143 plt.savefig('heatmap.png')
144 plt.title('Disaster heat map')
145 
146 #德克薩斯州
147 m=df.query('state=="TX"')['fy_declared'].value_counts().reset_index()
148 plt.figure(figsize=(30,15))
149 sns.barplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'));
150 plt.title('Impact of natural disasters in Texas in each year')
151 plt.grid()
152 
153 #佐治亞州
154 a=df.query('state=="GA"')['fy_declared'].value_counts().reset_index()
155 plt.figure(figsize=(30,15))
156 sns.barplot(x='index',y='fy_declared',data=m.sort_values(by='fy_declared'));
157 plt.title('Impact of natural disasters in Georgia in each year')
158 plt.grid()
159 
160 #德克薩斯州年份與受災情況的回歸圖
161 m=df.query('state=="TX"')['fy_declared'].value_counts().reset_index()
162 sns.regplot(x='index',y='fy_declared', data=m.sort_values(by='fy_declared'),
163             color='r',marker='+')
164  
165  #佐治亞州年份與受災情況的回歸圖
166  a=df.query('state=="GA"')['fy_declared'].value_counts().reset_index()
167  sns.regplot(x='index',y='fy_declared', data=a.sort_values(by='fy_declared'),
168              color='g',marker='*')
169  
170 plt.title('Regression of years and disaster conditions in Texas and Georgia')
171 
172 #佐治亞州各種自然災害所占比例
173 a=df.query('state=="GA"')['incident_type'].value_counts().reset_index()
174 b=a['index']
175 c=np.array(b)
176 d=a['incident_type']
177 plt.pie(d,labels=b,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
178 plt.title('Proportion of natural disasters in Georgia')
179 plt.figure(figsize=(20,6.5))
180 
181 #德克薩斯州各種自然災害所占比例
182 m=df.query('state=="TX"')['incident_type'].value_counts().reset_index()
183 b=m['index']
184 c=np.array(b)
185 d=m['incident_type']
186 plt.pie(d,labels=b,autopct='%1.2f%%',pctdistance=0.8) #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
187 plt.title('Proportion of various natural disasters in Texas')
188 plt.figure(figsize=(20,6.5))
189 
190 #查詢受自然災害影響最大的郡
191 df[['designated_area','incident_type']].groupby('designated_area').count().sort_values(by='incident_type',ascending=False).head(10)
192 
193 #災害聲明類型統計
194 df['declaration_type'].value_counts()
195 
196 #用盒圖來展現災害聲明類型
197 m=df[['fy_declared','declaration_type']]
198 plt.figure(figsize=(8,5))
199 sns.boxplot(x='declaration_type',y='fy_declared',data=m);
200 plt.title('Disaster declaration type')
201 
202 #用柱狀圖來展現災害聲明類型
203 fig,axes=plt.subplots(1,2)
204 plt.figure(figsize=(10,5))
205 sns.barplot(x='index',y='declaration_type',data=df['declaration_type'].value_counts().reset_index(),ax=axes[0])
206 sns.barplot(x='declaration_type',y='index',data=df['declaration_type'].value_counts().reset_index(),ax=axes[1])
207 plt.title('Disaster declaration type')
208 
209 #德克薩斯州
210 #二進制標志,指示是否為此災難宣布了“公共援助計划"(1有 0沒有)
211 m=df.query('state=="TX"')['pa_program_declared'].value_counts().reset_index()
212 b=m['index']
213 c=np.array(b)
214 d=m['pa_program_declared']
215 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
216 plt.title('Did the state of Texas announce a public aid program for the disaster')
217 plt.figure(figsize=(20,6.5))
218 
219 #佐治亞洲
220 #二進制標志,指示是否為此災難宣布了“公共援助計划"(1有 0沒有)
221 m=df.query('state=="GA"')['pa_program_declared'].value_counts().reset_index()
222 b=m['index']
223 c=np.array(b)
224 d=m['pa_program_declared']
225 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
226 plt.title('Did Georgia announce a public assistance program for the disaster')
227 plt.figure(figsize=(20,6.5))
228 
229 #德克薩斯州
230 #二進制標志,指示是否為此災難宣布了“個人援助計划"(1有 0沒有)
231 m=df.query('state=="TX"')['ia_program_declared'].value_counts().reset_index()
232 b=m['index']
233 c=np.array(b)
234 d=m['ia_program_declared']
235 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
236 plt.title('Indicates whether a "personal assistance plan" has been announced for this disaster')
237 plt.figure(figsize=(20,6.5))
238 
239  #佐治亞州
240 #二進制標志,指示是否為此災難宣布了“個人援助計划"(1有 0沒有)
241 m=df.query('state=="GA"')['ia_program_declared'].value_counts().reset_index()
242 b=m['index']
243 c=np.array(b)
244 d=m['ia_program_declared']
245 plt.pie(d,labels=b,autopct='%1.2f%%') #畫餅圖(數據,數據對應的標簽,百分數保留兩位小數點,調節距離)
246 plt.title('Indicates whether a "personal assistance plan" has been announced for this disaster')
247 plt.figure(figsize=(20,6.5))
248 
249 #年份與是否執行公共援助計划情況的回歸圖
250 m=df.query('pa_program_declared==1')['fy_declared'].value_counts().reset_index()
251 sns.regplot(x='index',y='fy_declared', data=m.sort_values(by='fy_declared'),color='b',marker='+')
252 plt.title('And whether the public assistance program is implemented')

 

 

五、總結

 1、通過這次對美國自然災害大數據分析與可視化,我了解了美國發生頻率最高的自然災害、美國每年受自然災害影響最大的州和郡、美國每年對待自然災害所提供的公共援助計划的實施率,並對受自然災害影響最大的州進行了相應的分析。

2、隨着對數據集的分析與可視化的漸漸深入,一些問題也體現了出來,美國每年自然災害發生次數不斷上升,這很大一部分原因來自人類活動,這應當引起人們的警覺。隨着人類對自然資源的加速開采和“空洞”效應的繼續擴大,人為的自然地質性災害將越來越頻繁地發生,地陷、火山爆發、地震、海嘯、山體滑坡等等重大破壞性因素將危及人類的繁衍生存,為此我們應當把環境保護放在推進現代化建設過程中的關鍵位置,我們應該認識到:保護和改善環境也是保護和發展生產力。

3.個人收獲:通過這次對數據集分析與可視化,我掌握了數據清洗的基本步驟,熟悉了直方圖、散點圖、回歸圖、餅圖等常見圖的畫法。加深了對大數據分析與可視化的理解。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM