利用python分析權力的遊戲五王之戰資料

來源:互聯網
上載者:User

標籤:width   AC   很多   taf   name   sum   copy   row   color   

本身是個美劇迷,其中權力的遊戲是我最愛的美劇之一,所以我通過kaggle下載了資料集並進行分析。資料名稱解釋如下:

name: 戰爭的名稱,字元變數。year: 戰爭發生的年份,數值變數。battle_number: 本資料中的unique id,對應每一場獨立的戰役,數值變數。attacker_king: 攻擊方的國王,"/"表示了國王的更換。例如:"Joffrey/Tommen Baratheon"意味著Tomen Baratheon繼承了Joffrey的王位,分類變數。defender_king: 防守方的國王,分類變數。attacker_1: 攻擊方將領,字元變數。attacker_2: 攻擊方將領,字元變數。attacker_3: 攻擊方將領,字元變數。attacker_4: 攻擊方將領,字元變數。defender_1: 防守方將領,字元變數。defender_2: 防守方將領,字元變數。defender_3: 防守方將領,字元變數。defender_4: 防守方將領,字元變數。attacker_outcome: 從攻擊方角度來看的戰爭結果,分別有:win, loss, draw,分類變數。battle_type: 戰爭的類別。pitched_battle: 雙方軍隊在一個地點相遇並戰鬥,這也是最基本的戰爭類別;ambush: 以隱藏或詭計為主要攻擊手段的戰爭;siege: 陣地戰;razing: 對未設防位置的攻擊。分類變數。major_death: 是否有重要人物的死亡,二進位變數。major_capture: 是否有重要人物的被捕,二進位變數。attacker_size: 攻擊方力量的大小,並未對騎兵、步兵等士兵種類有所區分,數值變數。defender_size: 防守方力量的大小,並未對騎兵、步兵等士兵種類有所區分,數值變數。attacker_commander: 攻擊方的主要指揮官。指揮官的名字中並沒有包含頭銜,不同的指揮官名字用逗號隔開,字元變數。defender_commander: 防守方的主要指揮官。指揮官的名字中並沒有包含頭銜,不同的指揮官名字用逗號隔開,字元變數。summer: 戰爭是否發生於夏天,二進位變數。location: 戰爭發生的地點,字元變數。region: 戰爭發生的地區,包括:Beyond the Wall, The North, The Iron Islands, The Riverlands, The Vale of Arryn, The Westerlands, The Crownlands, The Reach, The Stormlands, Dorne,分類變數。note: 注釋,字元變數。

首先我們先提出問題:

1.每個國王攻擊方式

2.每年死亡或被俘的重要人物

3.每個地區死亡或被俘的重要人物人數

4.戰爭結果是否與兵力多少有關係

1 匯入包

# TO DO: load pacakges
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline

1 收集資料

# TO DO: load the datasetdf = pd.read_csv(‘battles.csv‘)df.columns

1.1 查看資料類型

df.info()

df.describe()

收集和查看完資料,我們要對資料進行整理了

二 資料整理

2.1 首先備份資料

# 備份
df1 = df.copy()

通過觀察資料我們發現以下錯誤:
品質錯誤:

1.defender3 and defender4 都是 NaN,並且他們的類型為float。

2.部分資料丟失,解決丟失問題。

3. year與battle_number 類型錯誤

# 去除‘attacker_2‘, ‘attacker_3‘, ‘attacker_4‘, ‘defender_2‘, ‘defender_3‘, ‘defender_4‘, ‘note‘列data_game_clean = data_game_clean.drop([‘attacker_2‘, ‘attacker_3‘, ‘attacker_4‘, ‘defender_2‘, ‘defender_3‘, ‘defender_4‘, ‘note‘], axis=1)
data_game_clean[‘attacker_king‘] = data_game_clean[‘attacker_king‘].astype(‘category‘)
data_game_clean[‘defender_king‘] = data_game_clean[‘defender_king‘].astype(‘category‘)
data_game_clean[‘battle_type‘] = data_game_clean[‘battle_type‘].astype(‘category‘)
data_game_clean[‘region‘] = data_game_clean[‘region‘].astype(‘category‘)
# year與battle_number 類型錯誤,需要把int轉換成objectdata_game_clean[‘year‘] = data_game_clean[‘year‘].astype(‘object‘)data_game_clean[‘battle_number‘] = data_game_clean[‘battle_number‘].astype(‘object‘)
# 檢查是否有重複值sum(data_game_clean.duplicated())

 查看結果

data_game_clean.attacker_outcome.head()

 

# 檢查是否有重複值sum(df1.duplicated())

 

 

資料探索分析每個國王攻擊次數
data_game_clean[‘attacker_king‘].value_counts().plot(kind=‘barh‘,rot=45)plt.show()

看出第一的是Joffrey/Tommen Baratheon作為進攻方進行了14場戰鬥,主要原因是各個領主不承認Joffrey的合法地位,因為Joffrey是瑟曦和他弟弟的孩子,身體中淌著蘭尼斯特家族的血,為了讓各領主承認其合法地位,所以進行了大量戰鬥。第二個是Robb Stark,因為其父親被害開始複仇之戰。

每個國王攻擊方式
sns.set(style="darkgrid")sns.countplot(y=‘battle_type‘, hue=‘attacker_king‘, data = df1)plt.legend(bbox_to_anchor=(1.05, 1))plt.show()

這場戰鬥分為四種類型,分別為遭遇戰、伏擊戰、圍城戰、razing(不懂這個意思),可以看出Joffrey/Tommen Baratheon的14場戰爭中有6場遭遇戰、3場伏擊戰和5場圍城戰,而史塔克是10場戰爭中有5場伏擊戰和3場遭遇戰和2場圍城戰,能夠看出Joffrey/Tommen Baratheon更喜歡遭遇戰,而史塔克更喜歡伏擊戰。還能夠看出只有Stannis Baratheon進行過razing。

每個地區死亡或被俘的重要人物人數
#去除major_death major_capture的空值df0 = data_game_clean.dropna(subset = [‘major_death‘, ‘major_capture‘])#給每個地區分組並計算major_death major_capture的和data = df0.groupby(‘region‘).sum()[[‘major_death‘, ‘major_capture‘]]data


#將region計數,並轉化為表格與data合并p = pd.concat([data, df0.region.value_counts().to_frame()], axis = 1)
#排序p = p.sort_values(‘region‘, ascending = False)

#作圖p.plot.barh()plt.xlabel(‘count‘)plt.title(‘attacker_outcome_size‘)

可以看出在The Riverlands發生的戰爭最多,死亡和被俘的人物也最多,而血色婚禮也發生在此,史塔克家族在這裡傷亡慘重。北境雖然發生的戰爭多,但死亡的重要人物少。

戰爭結果是否與兵力多少有關係
# 去除‘attacker_size‘, ‘defender_size‘,‘attacker_outcome‘3列空值df2 = data_game_clean.dropna(subset = [‘attacker_size‘, ‘defender_size‘,‘attacker_outcome‘])# 計算進攻與防禦的兵力差值df3 = df2.attacker_size - df2.defender_size#將其轉化為dataframedf3 = df3.to_frame(name=‘size‘)#將這一列合并至df1表格中result = pd.concat([df2,df3],join=‘outer‘,axis=1)result.info()

 

sns.lmplot(x = ‘attacker_size‘, y = ‘defender_size‘, hue=‘attacker_outcome‘, fit_reg=False,data = data_game)

戰爭結果是否與兵力多少沒有關係,只有2場戰爭是在兵力取得優勢的情況下勝利的,其他都是以少勝多,因為戰爭有很多不可預測性,不是人多就能打贏戰爭的。

 

attackers = df_data_game_clean.attacker_king.map(lambda x:str(x).split(","))empty_array = []for i in attackers:    empty_array = np.append(empty_array, i)

 

from wordcloud import WordCloudcloud = WordCloud(width=1440, height=1080, relative_scaling=0.5, stopwords=[‘battle‘]).generate(" ".join(empty_array))plt.figure(figsize=(20, 15))plt.imshow(cloud)plt.axis(‘off‘)plt.show()

 

 可以看到offrey/Tommen Baratheon被提及的更多,而Euron Greyjoy被提及的最少。

得出結論

結論:

1.每個國王攻擊方式:可以看出Joffrey/Tommen Baratheon的14場戰爭中有6場遭遇戰、3場伏擊戰和5場圍城戰,而史塔克是10場戰爭中有5場伏擊戰和3場遭遇戰和2場圍城戰,能夠看出Joffrey/Tommen Baratheon更喜歡遭遇戰,而史塔克更喜歡伏擊戰。還能夠看出只有Stannis Baratheon進行過razing。

2.每年死亡或被俘的重要人物:299年發生的戰爭被俘和死亡的重要人物最多,可能和發生的戰爭次數有關,因為299年發生的戰爭次數最多。

3.每個地區死亡或被俘的重要人物人數:可以看出在The Riverlands發生的戰爭最多,死亡和被俘的人物也最多,而血色婚禮也發生在此,史塔克家族在這裡傷亡慘重。北境雖然發生的戰爭多,但死亡的重要人物少。

4.戰爭結果是否與兵力多少有關係:戰爭結果是否與兵力多少沒有關係,只有2場戰爭是在兵力取得優勢的情況下勝利的,其他都是以少勝多,因為戰爭有很多不可預測性,不是人多就能打贏戰爭的。

5.可以看到offrey/Tommen Baratheon被提及的更多,而Euron Greyjoy被提及的最少。

 

利用python分析權力的遊戲五王之戰資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.