Creating and Drawing a Network¶

In [1]:
# Install the library networkx for network analysis
!pip install networkx
Requirement already satisfied: networkx in c:\users\colto\anaconda3\lib\site-packages (2.7.1)
In [2]:
# Load the libraries
import networkx
import pandas as pd
pd.set_option('display.max_rows', 200)
import matplotlib.pyplot as plt
In [3]:
# Load the data of the network of A Song of Ice and Fire
# The dataset was created by Andrew Beveridge (https://github.com/mathbeveridge/asoiaf/tree/master)
asoiaf_df = pd.read_csv('Data/asoiaf-book1-edges.csv')
asoiaf_df
Out[3]:
Source Target Type weight book
0 Addam-Marbrand Jaime-Lannister Undirected 3 1
1 Addam-Marbrand Tywin-Lannister Undirected 6 1
2 Aegon-I-Targaryen Daenerys-Targaryen Undirected 5 1
3 Aegon-I-Targaryen Eddard-Stark Undirected 4 1
4 Aemon-Targaryen-(Maester-Aemon) Alliser-Thorne Undirected 4 1
... ... ... ... ... ...
679 Tyrion-Lannister Willis-Wode Undirected 4 1
680 Tyrion-Lannister Yoren Undirected 10 1
681 Tywin-Lannister Varys Undirected 4 1
682 Tywin-Lannister Walder-Frey Undirected 8 1
683 Waymar-Royce Will-(prologue) Undirected 18 1

684 rows × 5 columns

These networks were created by connecting two characters whenever their names (or nicknames) appeared within 15 words of one another in one of the books in A Song of Ice and Fire. The edge weight corresponds to the number of interactions. For example, the following sentence counts as an “edge” or connection between Jon Snow and Sam Tarly:
“It was the bastard Jon Snow who had taken that from him, him and his fat friend Sam Tarly.”

In [4]:
# Create a Network from a Pandas dataframe
G = networkx.from_pandas_edgelist(asoiaf_df, 'Source', 'Target', 'weight')
G['Jon-Snow']
Out[4]:
AtlasView({'Aemon-Targaryen-(Maester-Aemon)': {'weight': 34}, 'Albett': {'weight': 5}, 'Alliser-Thorne': {'weight': 32}, 'Arya-Stark': {'weight': 37}, 'Benjen-Stark': {'weight': 41}, 'Bowen-Marsh': {'weight': 10}, 'Bran-Stark': {'weight': 56}, 'Catelyn-Stark': {'weight': 14}, 'Cersei-Lannister': {'weight': 4}, 'Chett': {'weight': 4}, 'Dareon': {'weight': 6}, 'Donal-Noye': {'weight': 7}, 'Dywen': {'weight': 4}, 'Eddard-Stark': {'weight': 38}, 'Grenn': {'weight': 31}, 'Halder': {'weight': 23}, 'Hobb': {'weight': 5}, 'Jaremy-Rykker': {'weight': 7}, 'Jeor-Mormont': {'weight': 81}, 'Joffrey-Baratheon': {'weight': 10}, 'Jory-Cassel': {'weight': 3}, 'Luwin': {'weight': 10}, 'Matthar': {'weight': 3}, 'Mordane': {'weight': 4}, 'Othor': {'weight': 5}, 'Pypar': {'weight': 45}, 'Rast': {'weight': 8}, 'Rickon-Stark': {'weight': 11}, 'Robb-Stark': {'weight': 53}, 'Robert-Baratheon': {'weight': 18}, 'Rodrik-Cassel': {'weight': 3}, 'Samwell-Tarly': {'weight': 81}, 'Sansa-Stark': {'weight': 8}, 'Theon-Greyjoy': {'weight': 8}, 'Todder': {'weight': 13}, 'Tyrion-Lannister': {'weight': 56}, 'Yoren': {'weight': 6}})
In [5]:
# Draw a simple network
networkx.draw_networkx(G, with_labels=False)
In [6]:
# Add labels and customize width, font, and figure size
plt.figure(figsize=(8,8))
networkx.draw_networkx(G, with_labels=True, node_color='skyblue', width=.3, font_size=8)

Calculate Degree and Weighted Degree¶

Degree centrality is defined as the number of links incident upon a node (i.e., the number of ties that a node has).

In [7]:
networkx.degree(G)
Out[7]:
DegreeView({'Addam-Marbrand': 2, 'Jaime-Lannister': 29, 'Tywin-Lannister': 22, 'Aegon-I-Targaryen': 2, 'Daenerys-Targaryen': 21, 'Eddard-Stark': 66, 'Aemon-Targaryen-(Maester-Aemon)': 7, 'Alliser-Thorne': 10, 'Bowen-Marsh': 5, 'Chett': 3, 'Clydas': 1, 'Jeor-Mormont': 13, 'Jon-Snow': 37, 'Samwell-Tarly': 12, 'Aerys-II-Targaryen': 6, 'Brandon-Stark': 6, 'Gerold-Hightower': 5, 'Jon-Arryn': 15, 'Robert-Baratheon': 50, 'Aggo': 6, 'Drogo': 19, 'Jhogo': 6, 'Jorah-Mormont': 13, 'Quaro': 5, 'Rakharo': 5, 'Albett': 3, 'Halder': 8, 'Rast': 6, 'Grenn': 7, 'Pypar': 7, 'Tyrion-Lannister': 46, 'Alyn': 5, 'Harwin': 4, 'Jory-Cassel': 21, 'Tomard': 7, 'Arthur-Dayne': 1, 'Arya-Stark': 27, 'Benjen-Stark': 14, 'Bran-Stark': 32, 'Catelyn-Stark': 43, 'Cersei-Lannister': 30, 'Desmond': 2, 'Ilyn-Payne': 9, 'Jeyne-Poole': 6, 'Joffrey-Baratheon': 30, 'Meryn-Trant': 9, 'Mordane': 11, 'Mycah': 5, 'Myrcella-Baratheon': 7, 'Petyr-Baelish': 26, 'Rickon-Stark': 9, 'Robb-Stark': 35, 'Rodrik-Cassel': 18, 'Sandor-Clegane': 16, 'Sansa-Stark': 35, 'Syrio-Forel': 3, 'Tommen-Baratheon': 8, 'Vayon-Poole': 5, 'Yoren': 7, 'Arys-Oakheart': 1, 'Balon-Greyjoy': 2, 'Balon-Swann': 2, 'Renly-Baratheon': 18, 'Barristan-Selmy': 14, 'Boros-Blount': 5, 'Pycelle': 14, 'Varys': 17, 'Jaremy-Rykker': 4, 'Luwin': 18, 'Mance-Rayder': 1, 'Theon-Greyjoy': 15, 'Waymar-Royce': 3, 'Beric-Dondarrion': 6, 'Gregor-Clegane': 12, 'Loras-Tyrell': 14, 'Thoros-of-Myr': 1, 'Hali': 1, 'Hallis-Mollen': 5, 'Hodor': 4, 'Hullen': 6, 'Joseth': 2, 'Nan': 4, 'Osha': 3, 'Rickard-Karstark': 10, 'Rickard-Stark': 2, 'Stiv': 2, 'Lyanna-Stark': 4, 'Bronn': 10, 'Chiggen': 3, 'Marillion': 5, 'Shae': 2, 'Shagga': 6, 'Vardis-Egen': 6, 'Willis-Wode': 6, 'Brynden-Tully': 11, 'Edmure-Tully': 11, 'Hoster-Tully': 5, 'Lysa-Arryn': 15, 'Nestor-Royce': 2, 'Walder-Frey': 7, 'Colemon': 2, 'Donnel-Waynwood': 1, 'Eon-Hunter': 3, 'Jon-Umber-(Greatjon)': 8, 'Masha-Heddle': 1, 'Moreo-Tumitis': 1, 'Mya-Stone': 1, 'Mychel-Redfort': 1, 'Robert-Arryn': 3, 'Stevron-Frey': 2, 'Tytos-Blackwood': 3, 'Wendel-Manderly': 2, 'Cayn': 3, 'Janos-Slynt': 7, 'Stannis-Baratheon': 12, 'Chella': 1, 'Clement-Piper': 1, 'Karyl-Vance': 3, 'Cohollo': 4, 'Haggo': 6, 'Qotho': 7, 'Conn': 3, 'Coratt': 1, 'Doreah': 6, 'Eroeh': 1, 'Illyrio-Mopatis': 6, 'Irri': 8, 'Jhiqui': 5, 'Mirri-Maz-Duur': 6, 'Rhaegar-Targaryen': 7, 'Viserys-Targaryen': 8, 'Danwell-Frey': 3, 'Hosteen-Frey': 2, 'Jared-Frey': 2, 'Dareon': 1, 'Daryn-Hornwood': 2, 'Torrhen-Karstark': 3, 'Dolf': 1, 'Donal-Noye': 1, 'Jommo': 2, 'Ogo': 3, 'Rhaego': 1, 'Dywen': 1, 'Galbart-Glover': 6, 'Gendry': 1, 'High-Septon-(fat_one)': 2, 'Howland-Reed': 1, 'Jacks': 1, 'Joss': 1, 'Marq-Piper': 5, 'Porther': 1, 'Raymun-Darry': 2, 'Tobho-Mott': 1, 'Tregar': 1, 'Varly': 1, 'Wyl-(guard)': 4, 'Wylla': 1, 'Fogo': 1, 'Roose-Bolton': 5, 'Gared': 3, 'Will-(prologue)': 2, 'Oswell-Whent': 1, 'Todder': 4, 'Gunthor-son-of-Gurn': 1, 'Harys-Swyft': 1, 'Heward': 2, 'Hobb': 1, 'Hugh': 1, 'Jafer-Flowers': 1, 'Kevan-Lannister': 3, 'Matthar': 1, 'Othor': 1, 'Maege-Mormont': 2, 'Jonos-Bracken': 1, 'Jyck': 2, 'Morrec': 2, 'Kurleket': 1, 'Lancel-Lannister': 1, 'Leo-Lefford': 1, 'Mace-Tyrell': 2, 'Lyn-Corbray': 1, 'Paxter-Redwyne': 1, 'Maegor-I-Targaryen': 1, 'Mord': 1, 'Randyll-Tarly': 1, 'Timett': 2, 'Ulf-son-of-Umar': 1})
In [8]:
# Make the degree values a dictionary, then add it as a network attribute
degrees = dict(networkx.degree(G))
networkx.set_node_attributes(G, name='degree', values=degrees)
In [9]:
# Make a Pandas dataframe from the degree data, then sort from highest to lowest
degree_df = pd.DataFrame(G.nodes(data='degree'), columns=['node', 'degree'])
degree_df = degree_df.sort_values(by='degree', ascending=False)
degree_df
Out[9]:
node degree
5 Eddard-Stark 66
18 Robert-Baratheon 50
30 Tyrion-Lannister 46
39 Catelyn-Stark 43
12 Jon-Snow 37
51 Robb-Stark 35
54 Sansa-Stark 35
38 Bran-Stark 32
40 Cersei-Lannister 30
44 Joffrey-Baratheon 30
1 Jaime-Lannister 29
36 Arya-Stark 27
49 Petyr-Baelish 26
2 Tywin-Lannister 22
4 Daenerys-Targaryen 21
33 Jory-Cassel 21
20 Drogo 19
52 Rodrik-Cassel 18
62 Renly-Baratheon 18
68 Luwin 18
66 Varys 17
53 Sandor-Clegane 16
70 Theon-Greyjoy 15
17 Jon-Arryn 15
97 Lysa-Arryn 15
74 Loras-Tyrell 14
37 Benjen-Stark 14
63 Barristan-Selmy 14
65 Pycelle 14
11 Jeor-Mormont 13
22 Jorah-Mormont 13
114 Stannis-Baratheon 12
73 Gregor-Clegane 12
13 Samwell-Tarly 12
95 Edmure-Tully 11
94 Brynden-Tully 11
46 Mordane 11
7 Alliser-Thorne 10
87 Bronn 10
83 Rickard-Karstark 10
42 Ilyn-Payne 9
45 Meryn-Trant 9
50 Rickon-Stark 9
103 Jon-Umber-(Greatjon) 8
130 Viserys-Targaryen 8
126 Irri 8
26 Halder 8
56 Tommen-Baratheon 8
113 Janos-Slynt 7
120 Qotho 7
6 Aemon-Targaryen-(Maester-Aemon) 7
99 Walder-Frey 7
58 Yoren 7
28 Grenn 7
34 Tomard 7
129 Rhaegar-Targaryen 7
48 Myrcella-Baratheon 7
29 Pypar 7
91 Shagga 6
79 Hullen 6
128 Mirri-Maz-Duur 6
92 Vardis-Egen 6
72 Beric-Dondarrion 6
143 Galbart-Glover 6
123 Doreah 6
119 Haggo 6
93 Willis-Wode 6
125 Illyrio-Mopatis 6
43 Jeyne-Poole 6
14 Aerys-II-Targaryen 6
15 Brandon-Stark 6
19 Aggo 6
21 Jhogo 6
27 Rast 6
127 Jhiqui 5
8 Bowen-Marsh 5
16 Gerold-Hightower 5
23 Quaro 5
24 Rakharo 5
64 Boros-Blount 5
31 Alyn 5
149 Marq-Piper 5
89 Marillion 5
96 Hoster-Tully 5
47 Mycah 5
57 Vayon-Poole 5
77 Hallis-Mollen 5
158 Roose-Bolton 5
155 Wyl-(guard) 4
32 Harwin 4
162 Todder 4
118 Cohollo 4
78 Hodor 4
67 Jaremy-Rykker 4
86 Lyanna-Stark 4
81 Nan 4
110 Tytos-Blackwood 3
108 Robert-Arryn 3
121 Conn 3
9 Chett 3
88 Chiggen 3
117 Karyl-Vance 3
71 Waymar-Royce 3
169 Kevan-Lannister 3
82 Osha 3
112 Cayn 3
131 Danwell-Frey 3
55 Syrio-Forel 3
136 Torrhen-Karstark 3
140 Ogo 3
159 Gared 3
25 Albett 3
102 Eon-Hunter 3
174 Jyck 2
185 Timett 2
165 Heward 2
145 High-Septon-(fat_one) 2
179 Mace-Tyrell 2
132 Hosteen-Frey 2
133 Jared-Frey 2
160 Will-(prologue) 2
172 Maege-Mormont 2
139 Jommo 2
151 Raymun-Darry 2
135 Daryn-Hornwood 2
175 Morrec 2
0 Addam-Marbrand 2
85 Stiv 2
3 Aegon-I-Targaryen 2
98 Nestor-Royce 2
111 Wendel-Manderly 2
109 Stevron-Frey 2
80 Joseth 2
90 Shae 2
61 Balon-Swann 2
84 Rickard-Stark 2
100 Colemon 2
41 Desmond 2
60 Balon-Greyjoy 2
124 Eroeh 1
166 Hobb 1
168 Jafer-Flowers 1
167 Hugh 1
171 Othor 1
164 Harys-Swyft 1
163 Gunthor-son-of-Gurn 1
170 Matthar 1
173 Jonos-Bracken 1
59 Arys-Oakheart 1
161 Oswell-Whent 1
35 Arthur-Dayne 1
176 Kurleket 1
177 Lancel-Lannister 1
178 Leo-Lefford 1
10 Clydas 1
180 Lyn-Corbray 1
181 Paxter-Redwyne 1
182 Maegor-I-Targaryen 1
183 Mord 1
184 Randyll-Tarly 1
69 Mance-Rayder 1
157 Fogo 1
75 Thoros-of-Myr 1
141 Rhaego 1
116 Clement-Piper 1
115 Chella 1
107 Mychel-Redfort 1
134 Dareon 1
106 Mya-Stone 1
105 Moreo-Tumitis 1
137 Dolf 1
138 Donal-Noye 1
104 Masha-Heddle 1
101 Donnel-Waynwood 1
142 Dywen 1
76 Hali 1
144 Gendry 1
146 Howland-Reed 1
147 Jacks 1
148 Joss 1
150 Porther 1
152 Tobho-Mott 1
153 Tregar 1
154 Varly 1
156 Wylla 1
122 Coratt 1
186 Ulf-son-of-Umar 1
In [10]:
# Plot the 10 nodes with the highest degree values
degree_df[:10].plot(kind='barh', x='node', title='Top 10 Characters with the Highest Degrees').invert_yaxis()

Weighted degree centrality is defined as the summation of the weights of all links incident upon a node (i.e., the summation of weights of all ties that a node has).

In [11]:
networkx.degree(G, weight='weight')
Out[11]:
DegreeView({'Addam-Marbrand': 9, 'Jaime-Lannister': 241, 'Tywin-Lannister': 181, 'Aegon-I-Targaryen': 9, 'Daenerys-Targaryen': 443, 'Eddard-Stark': 1284, 'Aemon-Targaryen-(Maester-Aemon)': 74, 'Alliser-Thorne': 80, 'Bowen-Marsh': 28, 'Chett': 19, 'Clydas': 5, 'Jeor-Mormont': 181, 'Jon-Snow': 784, 'Samwell-Tarly': 148, 'Aerys-II-Targaryen': 37, 'Brandon-Stark': 41, 'Gerold-Hightower': 17, 'Jon-Arryn': 169, 'Robert-Baratheon': 941, 'Aggo': 36, 'Drogo': 256, 'Jhogo': 43, 'Jorah-Mormont': 154, 'Quaro': 21, 'Rakharo': 24, 'Albett': 11, 'Halder': 56, 'Rast': 26, 'Grenn': 85, 'Pypar': 107, 'Tyrion-Lannister': 650, 'Alyn': 32, 'Harwin': 18, 'Jory-Cassel': 152, 'Tomard': 42, 'Arthur-Dayne': 4, 'Arya-Stark': 430, 'Benjen-Stark': 104, 'Bran-Stark': 531, 'Catelyn-Stark': 520, 'Cersei-Lannister': 424, 'Desmond': 7, 'Ilyn-Payne': 43, 'Jeyne-Poole': 54, 'Joffrey-Baratheon': 422, 'Meryn-Trant': 36, 'Mordane': 141, 'Mycah': 50, 'Myrcella-Baratheon': 54, 'Petyr-Baelish': 313, 'Rickon-Stark': 98, 'Robb-Stark': 516, 'Rodrik-Cassel': 124, 'Sandor-Clegane': 135, 'Sansa-Stark': 545, 'Syrio-Forel': 31, 'Tommen-Baratheon': 37, 'Vayon-Poole': 28, 'Yoren': 33, 'Arys-Oakheart': 3, 'Balon-Greyjoy': 7, 'Balon-Swann': 6, 'Renly-Baratheon': 186, 'Barristan-Selmy': 143, 'Boros-Blount': 26, 'Pycelle': 150, 'Varys': 231, 'Jaremy-Rykker': 24, 'Luwin': 210, 'Mance-Rayder': 3, 'Theon-Greyjoy': 133, 'Waymar-Royce': 34, 'Beric-Dondarrion': 28, 'Gregor-Clegane': 74, 'Loras-Tyrell': 78, 'Thoros-of-Myr': 5, 'Hali': 3, 'Hallis-Mollen': 37, 'Hodor': 52, 'Hullen': 20, 'Joseth': 7, 'Nan': 33, 'Osha': 31, 'Rickard-Karstark': 46, 'Rickard-Stark': 7, 'Stiv': 7, 'Lyanna-Stark': 36, 'Bronn': 109, 'Chiggen': 16, 'Marillion': 29, 'Shae': 15, 'Shagga': 44, 'Vardis-Egen': 37, 'Willis-Wode': 28, 'Brynden-Tully': 69, 'Edmure-Tully': 48, 'Hoster-Tully': 24, 'Lysa-Arryn': 138, 'Nestor-Royce': 6, 'Walder-Frey': 41, 'Colemon': 7, 'Donnel-Waynwood': 3, 'Eon-Hunter': 9, 'Jon-Umber-(Greatjon)': 39, 'Masha-Heddle': 5, 'Moreo-Tumitis': 3, 'Mya-Stone': 9, 'Mychel-Redfort': 3, 'Robert-Arryn': 20, 'Stevron-Frey': 8, 'Tytos-Blackwood': 10, 'Wendel-Manderly': 6, 'Cayn': 16, 'Janos-Slynt': 25, 'Stannis-Baratheon': 105, 'Chella': 5, 'Clement-Piper': 4, 'Karyl-Vance': 11, 'Cohollo': 29, 'Haggo': 45, 'Qotho': 68, 'Conn': 20, 'Coratt': 5, 'Doreah': 51, 'Eroeh': 5, 'Illyrio-Mopatis': 70, 'Irri': 81, 'Jhiqui': 60, 'Mirri-Maz-Duur': 63, 'Rhaegar-Targaryen': 53, 'Viserys-Targaryen': 105, 'Danwell-Frey': 9, 'Hosteen-Frey': 6, 'Jared-Frey': 6, 'Dareon': 6, 'Daryn-Hornwood': 6, 'Torrhen-Karstark': 11, 'Dolf': 7, 'Donal-Noye': 7, 'Jommo': 6, 'Ogo': 18, 'Rhaego': 5, 'Dywen': 4, 'Galbart-Glover': 23, 'Gendry': 3, 'High-Septon-(fat_one)': 8, 'Howland-Reed': 3, 'Jacks': 4, 'Joss': 5, 'Marq-Piper': 18, 'Porther': 4, 'Raymun-Darry': 12, 'Tobho-Mott': 7, 'Tregar': 4, 'Varly': 4, 'Wyl-(guard)': 15, 'Wylla': 3, 'Fogo': 4, 'Roose-Bolton': 19, 'Gared': 29, 'Will-(prologue)': 30, 'Oswell-Whent': 3, 'Todder': 31, 'Gunthor-son-of-Gurn': 7, 'Harys-Swyft': 3, 'Heward': 8, 'Hobb': 5, 'Hugh': 3, 'Jafer-Flowers': 3, 'Kevan-Lannister': 28, 'Matthar': 3, 'Othor': 5, 'Maege-Mormont': 8, 'Jonos-Bracken': 3, 'Jyck': 11, 'Morrec': 10, 'Kurleket': 5, 'Lancel-Lannister': 3, 'Leo-Lefford': 4, 'Mace-Tyrell': 6, 'Lyn-Corbray': 3, 'Paxter-Redwyne': 3, 'Maegor-I-Targaryen': 4, 'Mord': 20, 'Randyll-Tarly': 5, 'Timett': 7, 'Ulf-son-of-Umar': 4})

Task 1¶

In [12]:
# First, make the weighted degree values a dictionary, and add it as a network attribute
# Then make a Pandas dataframe from the weighted degree data, and sort from highest to lowest
# Finally, plot the 15 (instead of 10) nodes with the highest weighted degree values, invert the y axis, 
# set color as red, and add reasonable title, xlabel, and ylabel

degrees = dict(networkx.degree(G, weight='weight'))
networkx.set_node_attributes(G, name='weighted_degree', values=degrees)
degree_df = pd.DataFrame(G.nodes(data='weighted_degree'), columns=['node', 'weighted_degree'])
degree_df = degree_df.sort_values(by='weighted_degree', ascending=False)
degree_df[:15].plot(kind='barh', x='node', title='Top 15 Characters with the Highest Weighted Degrees', color = 'red', xlabel = 'characters', ylabel='weighted_degrees').invert_yaxis()

Calculate Different Centrality Scores¶

Eigenvector centrality is a measure of the influence of a node in a network. High eigenvector score means that a node is connected to many nodes who themselves have high scores.

In [13]:
networkx.eigenvector_centrality(G)
Out[13]:
{'Addam-Marbrand': 0.016906622875341313,
 'Jaime-Lannister': 0.1949579857603636,
 'Tywin-Lannister': 0.13939046584052586,
 'Aegon-I-Targaryen': 0.01738468087953347,
 'Daenerys-Targaryen': 0.047389349606780984,
 'Eddard-Stark': 0.29640893871843443,
 'Aemon-Targaryen-(Maester-Aemon)': 0.015728915590424563,
 'Alliser-Thorne': 0.0288464406191708,
 'Bowen-Marsh': 0.015169602126328249,
 'Chett': 0.010824124243907147,
 'Clydas': 0.0007954157740942034,
 'Jeor-Mormont': 0.05708614464400135,
 'Jon-Snow': 0.1704604493682857,
 'Samwell-Tarly': 0.02786296605781483,
 'Aerys-II-Targaryen': 0.048582634087231305,
 'Brandon-Stark': 0.04401847371679745,
 'Gerold-Hightower': 0.031231471653479868,
 'Jon-Arryn': 0.12467394372126446,
 'Robert-Baratheon': 0.2694880017758137,
 'Aggo': 0.005481130715564632,
 'Drogo': 0.02259083571954163,
 'Jhogo': 0.005712048672841408,
 'Jorah-Mormont': 0.024102672246267692,
 'Quaro': 0.004319920160802584,
 'Rakharo': 0.004263929104536286,
 'Albett': 0.010048162713271911,
 'Halder': 0.014781007284671957,
 'Rast': 0.013466110590970989,
 'Grenn': 0.01639690491836453,
 'Pypar': 0.014297337660772631,
 'Tyrion-Lannister': 0.22520388145217876,
 'Alyn': 0.04012382043745944,
 'Harwin': 0.026369147283276637,
 'Jory-Cassel': 0.14468019419020758,
 'Tomard': 0.05654806881254954,
 'Arthur-Dayne': 0.0015792463861233257,
 'Arya-Stark': 0.17848320231945494,
 'Benjen-Stark': 0.09067422893446861,
 'Bran-Stark': 0.19377206822851203,
 'Catelyn-Stark': 0.21255000233989124,
 'Cersei-Lannister': 0.21643331139694155,
 'Desmond': 0.024013374348794234,
 'Ilyn-Payne': 0.08701709456214834,
 'Jeyne-Poole': 0.04944286706830396,
 'Joffrey-Baratheon': 0.22075394589376895,
 'Meryn-Trant': 0.07310608185356994,
 'Mordane': 0.10338831776958497,
 'Mycah': 0.05368981465529889,
 'Myrcella-Baratheon': 0.06545937981560086,
 'Petyr-Baelish': 0.201464044325535,
 'Rickon-Stark': 0.0911971493584744,
 'Robb-Stark': 0.19253612354629224,
 'Rodrik-Cassel': 0.1363957353262088,
 'Sandor-Clegane': 0.13458575177273222,
 'Sansa-Stark': 0.23155019835665283,
 'Syrio-Forel': 0.027710001448224882,
 'Tommen-Baratheon': 0.07444060512260162,
 'Vayon-Poole': 0.05590033474448936,
 'Yoren': 0.05603875714669781,
 'Arys-Oakheart': 0.010944095989904618,
 'Balon-Greyjoy': 0.02861514635331536,
 'Balon-Swann': 0.014553468136728889,
 'Renly-Baratheon': 0.1532287242439995,
 'Barristan-Selmy': 0.12302718877294898,
 'Boros-Blount': 0.04373268037151271,
 'Pycelle': 0.12219169081693486,
 'Varys': 0.15044258583494022,
 'Jaremy-Rykker': 0.016132796236034825,
 'Luwin': 0.10255033886172546,
 'Mance-Rayder': 0.004585129661056267,
 'Theon-Greyjoy': 0.10563256306460303,
 'Waymar-Royce': 0.0047645563593988124,
 'Beric-Dondarrion': 0.05103495200514704,
 'Gregor-Clegane': 0.10138445097297553,
 'Loras-Tyrell': 0.10786588808016156,
 'Thoros-of-Myr': 0.002580606209997588,
 'Hali': 0.009798379367785801,
 'Hallis-Mollen': 0.04080938950934968,
 'Hodor': 0.02603658498719331,
 'Hullen': 0.04026631802680754,
 'Joseth': 0.01498406302298033,
 'Nan': 0.02603658498719331,
 'Osha': 0.024719976850451925,
 'Rickard-Karstark': 0.06590114105036478,
 'Rickard-Stark': 0.01202420245434714,
 'Stiv': 0.019534293195257398,
 'Lyanna-Stark': 0.04254945728318318,
 'Bronn': 0.04320727044390087,
 'Chiggen': 0.015274406249032446,
 'Marillion': 0.03291934444006452,
 'Shae': 0.01357259808079795,
 'Shagga': 0.01487803069139531,
 'Vardis-Egen': 0.030626468252802973,
 'Willis-Wode': 0.033654547957501856,
 'Brynden-Tully': 0.06678578692261303,
 'Edmure-Tully': 0.06806757359530645,
 'Hoster-Tully': 0.03601261025834175,
 'Lysa-Arryn': 0.09529568210346988,
 'Nestor-Royce': 0.014125023204130267,
 'Walder-Frey': 0.036984031709819946,
 'Colemon': 0.012296569610271198,
 'Donnel-Waynwood': 0.010747888745467586,
 'Eon-Hunter': 0.017115289146141632,
 'Jon-Umber-(Greatjon)': 0.0397814156722378,
 'Masha-Heddle': 0.010747888745467586,
 'Moreo-Tumitis': 0.010747888745467586,
 'Mya-Stone': 0.010747888745467586,
 'Mychel-Redfort': 0.010747888745467586,
 'Robert-Arryn': 0.02695434980977946,
 'Stevron-Frey': 0.012618064751098574,
 'Tytos-Blackwood': 0.01422617756719362,
 'Wendel-Manderly': 0.020483802572939185,
 'Cayn': 0.02559566501193552,
 'Janos-Slynt': 0.07879610821922742,
 'Stannis-Baratheon': 0.10945155967262629,
 'Chella': 0.011387741528441438,
 'Clement-Piper': 0.0002535943343961503,
 'Karyl-Vance': 0.005015125987307613,
 'Cohollo': 0.004056089872611762,
 'Haggo': 0.004529286288515295,
 'Qotho': 0.005692241579735824,
 'Conn': 0.012171201129333934,
 'Coratt': 0.0006154589629808151,
 'Doreah': 0.006581729076365154,
 'Eroeh': 0.0023964898635822603,
 'Illyrio-Mopatis': 0.019931169633803564,
 'Irri': 0.007816169006182781,
 'Jhiqui': 0.005486256953771574,
 'Mirri-Maz-Duur': 0.005563979727871628,
 'Rhaegar-Targaryen': 0.05213837687676225,
 'Viserys-Targaryen': 0.022757626238469306,
 'Danwell-Frey': 0.0018803053819896208,
 'Hosteen-Frey': 0.00010014875657557114,
 'Jared-Frey': 0.00010014875657557114,
 'Dareon': 0.008619680042012707,
 'Daryn-Hornwood': 0.01628002604941205,
 'Torrhen-Karstark': 0.025547317380754688,
 'Dolf': 0.000752337246122054,
 'Donal-Noye': 0.008619680042012707,
 'Jommo': 0.001203573779451314,
 'Ogo': 0.0012065144544696012,
 'Rhaego': 0.0011425368414569482,
 'Dywen': 0.008619680042012707,
 'Galbart-Glover': 0.04115052322670415,
 'Gendry': 0.014988191015951207,
 'High-Septon-(fat_one)': 0.02615076698505184,
 'Howland-Reed': 0.014988191015951207,
 'Jacks': 0.014988191015951207,
 'Joss': 0.014988191015951207,
 'Marq-Piper': 0.030858678967445173,
 'Porther': 0.014988191015951207,
 'Raymun-Darry': 0.02861514635331536,
 'Tobho-Mott': 0.014988191015951207,
 'Tregar': 0.014988191015951207,
 'Varly': 0.014988191015951207,
 'Wyl-(guard)': 0.03261562506545171,
 'Wylla': 0.014988191015951207,
 'Fogo': 6.1036937994366e-05,
 'Roose-Bolton': 0.024209210342201973,
 'Gared': 0.0031479020740754747,
 'Will-(prologue)': 0.0004001277877619065,
 'Oswell-Whent': 0.0015792463861233257,
 'Todder': 0.010919406010291672,
 'Gunthor-son-of-Gurn': 0.011387741528441438,
 'Harys-Swyft': 0.009858194405182258,
 'Heward': 0.008965140352499127,
 'Hobb': 0.008619680042012707,
 'Hugh': 0.006304223856049019,
 'Jafer-Flowers': 0.0008158136301284558,
 'Kevan-Lannister': 0.028294364403782756,
 'Matthar': 0.008619680042012707,
 'Othor': 0.008619680042012707,
 'Maege-Mormont': 0.005344035151065859,
 'Jonos-Bracken': 0.0007193703105539877,
 'Jyck': 0.011994252167140448,
 'Morrec': 0.011994252167140448,
 'Kurleket': 0.011387741528441438,
 'Lancel-Lannister': 0.013626955337364152,
 'Leo-Lefford': 0.011387741528441438,
 'Mace-Tyrell': 0.005468257486430045,
 'Lyn-Corbray': 0.004818719535870433,
 'Paxter-Redwyne': 0.00027649966971300823,
 'Maegor-I-Targaryen': 0.011708487843306485,
 'Mord': 0.011387741528441438,
 'Randyll-Tarly': 0.0014090284278002363,
 'Timett': 0.012140078774563494,
 'Ulf-son-of-Umar': 0.000752337246122054}
In [14]:
# Add eigenvector_centrality (which is already a dictionary) as a network attribute
eigenvector_centrality = networkx.eigenvector_centrality(G)
networkx.set_node_attributes(G, name='eigenvector', values=eigenvector_centrality)
In [15]:
# Make a Pandas dataframe from the eigenvector centrality, then sort from highest to lowest
eigenvector_df = pd.DataFrame(G.nodes(data='eigenvector'), columns=['node', 'eigenvector'])
eigenvector_df = eigenvector_df.sort_values(by='eigenvector', ascending=False)
eigenvector_df
Out[15]:
node eigenvector
5 Eddard-Stark 0.296409
18 Robert-Baratheon 0.269488
54 Sansa-Stark 0.231550
30 Tyrion-Lannister 0.225204
44 Joffrey-Baratheon 0.220754
40 Cersei-Lannister 0.216433
39 Catelyn-Stark 0.212550
49 Petyr-Baelish 0.201464
1 Jaime-Lannister 0.194958
38 Bran-Stark 0.193772
51 Robb-Stark 0.192536
36 Arya-Stark 0.178483
12 Jon-Snow 0.170460
62 Renly-Baratheon 0.153229
66 Varys 0.150443
33 Jory-Cassel 0.144680
2 Tywin-Lannister 0.139390
52 Rodrik-Cassel 0.136396
53 Sandor-Clegane 0.134586
17 Jon-Arryn 0.124674
63 Barristan-Selmy 0.123027
65 Pycelle 0.122192
114 Stannis-Baratheon 0.109452
74 Loras-Tyrell 0.107866
70 Theon-Greyjoy 0.105633
46 Mordane 0.103388
68 Luwin 0.102550
73 Gregor-Clegane 0.101384
97 Lysa-Arryn 0.095296
50 Rickon-Stark 0.091197
37 Benjen-Stark 0.090674
42 Ilyn-Payne 0.087017
113 Janos-Slynt 0.078796
56 Tommen-Baratheon 0.074441
45 Meryn-Trant 0.073106
95 Edmure-Tully 0.068068
94 Brynden-Tully 0.066786
83 Rickard-Karstark 0.065901
48 Myrcella-Baratheon 0.065459
11 Jeor-Mormont 0.057086
34 Tomard 0.056548
58 Yoren 0.056039
57 Vayon-Poole 0.055900
47 Mycah 0.053690
129 Rhaegar-Targaryen 0.052138
72 Beric-Dondarrion 0.051035
43 Jeyne-Poole 0.049443
14 Aerys-II-Targaryen 0.048583
4 Daenerys-Targaryen 0.047389
15 Brandon-Stark 0.044018
64 Boros-Blount 0.043733
87 Bronn 0.043207
86 Lyanna-Stark 0.042549
143 Galbart-Glover 0.041151
77 Hallis-Mollen 0.040809
79 Hullen 0.040266
31 Alyn 0.040124
103 Jon-Umber-(Greatjon) 0.039781
99 Walder-Frey 0.036984
96 Hoster-Tully 0.036013
93 Willis-Wode 0.033655
89 Marillion 0.032919
155 Wyl-(guard) 0.032616
16 Gerold-Hightower 0.031231
149 Marq-Piper 0.030859
92 Vardis-Egen 0.030626
7 Alliser-Thorne 0.028846
60 Balon-Greyjoy 0.028615
151 Raymun-Darry 0.028615
169 Kevan-Lannister 0.028294
13 Samwell-Tarly 0.027863
55 Syrio-Forel 0.027710
108 Robert-Arryn 0.026954
32 Harwin 0.026369
145 High-Septon-(fat_one) 0.026151
81 Nan 0.026037
78 Hodor 0.026037
112 Cayn 0.025596
136 Torrhen-Karstark 0.025547
82 Osha 0.024720
158 Roose-Bolton 0.024209
22 Jorah-Mormont 0.024103
41 Desmond 0.024013
130 Viserys-Targaryen 0.022758
20 Drogo 0.022591
111 Wendel-Manderly 0.020484
125 Illyrio-Mopatis 0.019931
85 Stiv 0.019534
3 Aegon-I-Targaryen 0.017385
102 Eon-Hunter 0.017115
0 Addam-Marbrand 0.016907
28 Grenn 0.016397
135 Daryn-Hornwood 0.016280
67 Jaremy-Rykker 0.016133
6 Aemon-Targaryen-(Maester-Aemon) 0.015729
88 Chiggen 0.015274
8 Bowen-Marsh 0.015170
148 Joss 0.014988
147 Jacks 0.014988
152 Tobho-Mott 0.014988
153 Tregar 0.014988
154 Varly 0.014988
156 Wylla 0.014988
150 Porther 0.014988
146 Howland-Reed 0.014988
144 Gendry 0.014988
80 Joseth 0.014984
91 Shagga 0.014878
26 Halder 0.014781
61 Balon-Swann 0.014553
29 Pypar 0.014297
110 Tytos-Blackwood 0.014226
98 Nestor-Royce 0.014125
177 Lancel-Lannister 0.013627
90 Shae 0.013573
27 Rast 0.013466
109 Stevron-Frey 0.012618
100 Colemon 0.012297
121 Conn 0.012171
185 Timett 0.012140
84 Rickard-Stark 0.012024
175 Morrec 0.011994
174 Jyck 0.011994
182 Maegor-I-Targaryen 0.011708
178 Leo-Lefford 0.011388
176 Kurleket 0.011388
163 Gunthor-son-of-Gurn 0.011388
183 Mord 0.011388
115 Chella 0.011388
59 Arys-Oakheart 0.010944
162 Todder 0.010919
9 Chett 0.010824
106 Mya-Stone 0.010748
105 Moreo-Tumitis 0.010748
104 Masha-Heddle 0.010748
101 Donnel-Waynwood 0.010748
107 Mychel-Redfort 0.010748
25 Albett 0.010048
164 Harys-Swyft 0.009858
76 Hali 0.009798
165 Heward 0.008965
138 Donal-Noye 0.008620
166 Hobb 0.008620
142 Dywen 0.008620
134 Dareon 0.008620
170 Matthar 0.008620
171 Othor 0.008620
126 Irri 0.007816
123 Doreah 0.006582
167 Hugh 0.006304
21 Jhogo 0.005712
120 Qotho 0.005692
128 Mirri-Maz-Duur 0.005564
127 Jhiqui 0.005486
19 Aggo 0.005481
179 Mace-Tyrell 0.005468
172 Maege-Mormont 0.005344
117 Karyl-Vance 0.005015
180 Lyn-Corbray 0.004819
71 Waymar-Royce 0.004765
69 Mance-Rayder 0.004585
119 Haggo 0.004529
23 Quaro 0.004320
24 Rakharo 0.004264
118 Cohollo 0.004056
159 Gared 0.003148
75 Thoros-of-Myr 0.002581
124 Eroeh 0.002396
131 Danwell-Frey 0.001880
161 Oswell-Whent 0.001579
35 Arthur-Dayne 0.001579
184 Randyll-Tarly 0.001409
140 Ogo 0.001207
139 Jommo 0.001204
141 Rhaego 0.001143
168 Jafer-Flowers 0.000816
10 Clydas 0.000795
137 Dolf 0.000752
186 Ulf-son-of-Umar 0.000752
173 Jonos-Bracken 0.000719
122 Coratt 0.000615
160 Will-(prologue) 0.000400
181 Paxter-Redwyne 0.000276
116 Clement-Piper 0.000254
133 Jared-Frey 0.000100
132 Hosteen-Frey 0.000100
157 Fogo 0.000061

Betweenness centrality is a measure of centrality in a graph based on shortest paths. The betweenness centrality for each node is the number of these shortest paths that pass through the node.

In [16]:
networkx.betweenness_centrality(G)
Out[16]:
{'Addam-Marbrand': 0.0,
 'Jaime-Lannister': 0.0320095542681723,
 'Tywin-Lannister': 0.026190475931508066,
 'Aegon-I-Targaryen': 0.0,
 'Daenerys-Targaryen': 0.08627015537511595,
 'Eddard-Stark': 0.2696038913836117,
 'Aemon-Targaryen-(Maester-Aemon)': 0.010844715683425361,
 'Alliser-Thorne': 0.005026701469957172,
 'Bowen-Marsh': 0.0,
 'Chett': 0.0,
 'Clydas': 0.0,
 'Jeor-Mormont': 0.028345621007255038,
 'Jon-Snow': 0.17158135899829566,
 'Samwell-Tarly': 0.014449634885820021,
 'Aerys-II-Targaryen': 0.00048173749786653005,
 'Brandon-Stark': 0.002080261918971597,
 'Gerold-Hightower': 0.02144725370531822,
 'Jon-Arryn': 0.012525043642810938,
 'Robert-Baratheon': 0.21403028397371796,
 'Aggo': 7.265329845975008e-05,
 'Drogo': 0.06481224290874964,
 'Jhogo': 4.359197907585004e-05,
 'Jorah-Mormont': 0.012611106728154204,
 'Quaro': 2.906131938390003e-05,
 'Rakharo': 3.390487261455003e-05,
 'Albett': 0.0,
 'Halder': 0.0002102102102102102,
 'Rast': 7.749685169040008e-05,
 'Grenn': 0.0002024605250411702,
 'Pypar': 7.459071975201007e-05,
 'Tyrion-Lannister': 0.1902124972697492,
 'Alyn': 0.0003145710264731398,
 'Harwin': 0.0003166711322941555,
 'Jory-Cassel': 0.01752453849363612,
 'Tomard': 0.0002966978204169855,
 'Arthur-Dayne': 0.0,
 'Arya-Stark': 0.017495289470929955,
 'Benjen-Stark': 0.03293306050027417,
 'Bran-Stark': 0.05579958811784442,
 'Catelyn-Stark': 0.1513952715347627,
 'Cersei-Lannister': 0.026434677939526103,
 'Desmond': 0.0,
 'Ilyn-Payne': 0.0003199575146182485,
 'Jeyne-Poole': 6.109796432377078e-05,
 'Joffrey-Baratheon': 0.018949350296839806,
 'Meryn-Trant': 0.0006984119660127081,
 'Mordane': 0.0010561655570225142,
 'Mycah': 0.0,
 'Myrcella-Baratheon': 2.8230995972931453e-05,
 'Petyr-Baelish': 0.009436743202985242,
 'Rickon-Stark': 8.984632696126533e-05,
 'Robb-Stark': 0.07298399629664767,
 'Rodrik-Cassel': 0.00963397409313393,
 'Sandor-Clegane': 0.006237666841098885,
 'Sansa-Stark': 0.03714483664326785,
 'Syrio-Forel': 0.00012300558547174553,
 'Tommen-Baratheon': 0.000554548184780281,
 'Vayon-Poole': 0.0,
 'Yoren': 0.00021138387522088702,
 'Arys-Oakheart': 0.0,
 'Balon-Greyjoy': 0.0,
 'Balon-Swann': 0.0,
 'Renly-Baratheon': 0.009635188708026638,
 'Barristan-Selmy': 0.0012358894598099792,
 'Boros-Blount': 0.0,
 'Pycelle': 0.0003210706351837933,
 'Varys': 0.010794552272183802,
 'Jaremy-Rykker': 0.01075268817204301,
 'Luwin': 0.013182650228125651,
 'Mance-Rayder': 0.0,
 'Theon-Greyjoy': 0.00980833131274694,
 'Waymar-Royce': 0.006708358729333187,
 'Beric-Dondarrion': 0.01075268817204301,
 'Gregor-Clegane': 0.002923862955280991,
 'Loras-Tyrell': 0.022409577164998796,
 'Thoros-of-Myr': 0.0,
 'Hali': 0.0,
 'Hallis-Mollen': 0.0,
 'Hodor': 0.0,
 'Hullen': 0.0004513970364915859,
 'Joseth': 0.0,
 'Nan': 0.0,
 'Osha': 0.0,
 'Rickard-Karstark': 0.009473682089536549,
 'Rickard-Stark': 8.86701693153306e-05,
 'Stiv': 0.0,
 'Lyanna-Stark': 3.321293643874289e-05,
 'Bronn': 0.004124576359910305,
 'Chiggen': 0.0,
 'Marillion': 0.0,
 'Shae': 0.0,
 'Shagga': 0.02159256030223772,
 'Vardis-Egen': 0.0011877067734453138,
 'Willis-Wode': 0.00038763506505441997,
 'Brynden-Tully': 0.00383234320543726,
 'Edmure-Tully': 0.01963307983379491,
 'Hoster-Tully': 0.0003909562718491462,
 'Lysa-Arryn': 0.01821323157123013,
 'Nestor-Royce': 0.0,
 'Walder-Frey': 0.032338618467650716,
 'Colemon': 0.0,
 'Donnel-Waynwood': 0.0,
 'Eon-Hunter': 0.0,
 'Jon-Umber-(Greatjon)': 0.0033242714657788115,
 'Masha-Heddle': 0.0,
 'Moreo-Tumitis': 0.0,
 'Mya-Stone': 0.0,
 'Mychel-Redfort': 0.0,
 'Robert-Arryn': 0.0,
 'Stevron-Frey': 0.0,
 'Tytos-Blackwood': 0.01075268817204301,
 'Wendel-Manderly': 0.0,
 'Cayn': 2.179598953792502e-05,
 'Janos-Slynt': 0.0,
 'Stannis-Baratheon': 0.0004430380168891369,
 'Chella': 0.0,
 'Clement-Piper': 0.0,
 'Karyl-Vance': 0.01075268817204301,
 'Cohollo': 0.0,
 'Haggo': 6.780974522910006e-05,
 'Qotho': 0.00011624527753560009,
 'Conn': 0.01075268817204301,
 'Coratt': 0.0,
 'Doreah': 1.1624527753560013e-05,
 'Eroeh': 0.0,
 'Illyrio-Mopatis': 0.0011992863048539452,
 'Irri': 9.590235396687009e-05,
 'Jhiqui': 0.0,
 'Mirri-Maz-Duur': 4.8435532306500046e-05,
 'Rhaegar-Targaryen': 0.0028204821920682827,
 'Viserys-Targaryen': 0.002857503691485831,
 'Danwell-Frey': 0.02138913106655042,
 'Hosteen-Frey': 0.0,
 'Jared-Frey': 0.0,
 'Dareon': 0.0,
 'Daryn-Hornwood': 0.0,
 'Torrhen-Karstark': 0.00027229994971930455,
 'Dolf': 0.0,
 'Donal-Noye': 0.0,
 'Jommo': 0.0,
 'Ogo': 0.01075268817204301,
 'Rhaego': 0.0,
 'Dywen': 0.0,
 'Galbart-Glover': 0.0008706693487442282,
 'Gendry': 0.0,
 'High-Septon-(fat_one)': 0.0,
 'Howland-Reed': 0.0,
 'Jacks': 0.0,
 'Joss': 0.0,
 'Marq-Piper': 0.0062369436762998,
 'Porther': 0.0,
 'Raymun-Darry': 0.0,
 'Tobho-Mott': 0.0,
 'Tregar': 0.0,
 'Varly': 0.0,
 'Wyl-(guard)': 0.0016301901690895994,
 'Wylla': 0.0,
 'Fogo': 0.0,
 'Roose-Bolton': 2.4944299137847526e-05,
 'Gared': 0.004328212613689769,
 'Will-(prologue)': 0.0,
 'Oswell-Whent': 0.0,
 'Todder': 0.0,
 'Gunthor-son-of-Gurn': 0.0,
 'Harys-Swyft': 0.0,
 'Heward': 0.0,
 'Hobb': 0.0,
 'Hugh': 0.0,
 'Jafer-Flowers': 0.0,
 'Kevan-Lannister': 0.0,
 'Matthar': 0.0,
 'Othor': 0.0,
 'Maege-Mormont': 0.0,
 'Jonos-Bracken': 0.0,
 'Jyck': 0.0,
 'Morrec': 0.0,
 'Kurleket': 0.0,
 'Lancel-Lannister': 0.0,
 'Leo-Lefford': 0.0,
 'Mace-Tyrell': 0.01075268817204301,
 'Lyn-Corbray': 0.0,
 'Paxter-Redwyne': 0.0,
 'Maegor-I-Targaryen': 0.0,
 'Mord': 0.0,
 'Randyll-Tarly': 0.0,
 'Timett': 0.0,
 'Ulf-son-of-Umar': 0.0}
In [17]:
betweenness_centrality = networkx.betweenness_centrality(G)
networkx.set_node_attributes(G, name='betweenness', values=betweenness_centrality)
betweenness_df = pd.DataFrame(G.nodes(data='betweenness'), columns=['node', 'betweenness'])
betweenness_df = betweenness_df.sort_values(by='betweenness', ascending=False)
betweenness_df
Out[17]:
node betweenness
5 Eddard-Stark 0.269604
18 Robert-Baratheon 0.214030
30 Tyrion-Lannister 0.190212
12 Jon-Snow 0.171581
39 Catelyn-Stark 0.151395
4 Daenerys-Targaryen 0.086270
51 Robb-Stark 0.072984
20 Drogo 0.064812
38 Bran-Stark 0.055800
54 Sansa-Stark 0.037145
37 Benjen-Stark 0.032933
99 Walder-Frey 0.032339
1 Jaime-Lannister 0.032010
11 Jeor-Mormont 0.028346
40 Cersei-Lannister 0.026435
2 Tywin-Lannister 0.026190
74 Loras-Tyrell 0.022410
91 Shagga 0.021593
16 Gerold-Hightower 0.021447
131 Danwell-Frey 0.021389
95 Edmure-Tully 0.019633
44 Joffrey-Baratheon 0.018949
97 Lysa-Arryn 0.018213
33 Jory-Cassel 0.017525
36 Arya-Stark 0.017495
13 Samwell-Tarly 0.014450
68 Luwin 0.013183
22 Jorah-Mormont 0.012611
17 Jon-Arryn 0.012525
6 Aemon-Targaryen-(Maester-Aemon) 0.010845
66 Varys 0.010795
121 Conn 0.010753
72 Beric-Dondarrion 0.010753
140 Ogo 0.010753
179 Mace-Tyrell 0.010753
110 Tytos-Blackwood 0.010753
67 Jaremy-Rykker 0.010753
117 Karyl-Vance 0.010753
70 Theon-Greyjoy 0.009808
62 Renly-Baratheon 0.009635
52 Rodrik-Cassel 0.009634
83 Rickard-Karstark 0.009474
49 Petyr-Baelish 0.009437
71 Waymar-Royce 0.006708
53 Sandor-Clegane 0.006238
149 Marq-Piper 0.006237
7 Alliser-Thorne 0.005027
159 Gared 0.004328
87 Bronn 0.004125
94 Brynden-Tully 0.003832
103 Jon-Umber-(Greatjon) 0.003324
73 Gregor-Clegane 0.002924
130 Viserys-Targaryen 0.002858
129 Rhaegar-Targaryen 0.002820
15 Brandon-Stark 0.002080
155 Wyl-(guard) 0.001630
63 Barristan-Selmy 0.001236
125 Illyrio-Mopatis 0.001199
92 Vardis-Egen 0.001188
46 Mordane 0.001056
143 Galbart-Glover 0.000871
45 Meryn-Trant 0.000698
56 Tommen-Baratheon 0.000555
14 Aerys-II-Targaryen 0.000482
79 Hullen 0.000451
114 Stannis-Baratheon 0.000443
96 Hoster-Tully 0.000391
93 Willis-Wode 0.000388
65 Pycelle 0.000321
42 Ilyn-Payne 0.000320
32 Harwin 0.000317
31 Alyn 0.000315
34 Tomard 0.000297
136 Torrhen-Karstark 0.000272
58 Yoren 0.000211
26 Halder 0.000210
28 Grenn 0.000202
55 Syrio-Forel 0.000123
120 Qotho 0.000116
126 Irri 0.000096
50 Rickon-Stark 0.000090
84 Rickard-Stark 0.000089
27 Rast 0.000077
29 Pypar 0.000075
19 Aggo 0.000073
119 Haggo 0.000068
43 Jeyne-Poole 0.000061
128 Mirri-Maz-Duur 0.000048
21 Jhogo 0.000044
24 Rakharo 0.000034
86 Lyanna-Stark 0.000033
23 Quaro 0.000029
48 Myrcella-Baratheon 0.000028
158 Roose-Bolton 0.000025
112 Cayn 0.000022
123 Doreah 0.000012
144 Gendry 0.000000
146 Howland-Reed 0.000000
182 Maegor-I-Targaryen 0.000000
183 Mord 0.000000
184 Randyll-Tarly 0.000000
142 Dywen 0.000000
145 High-Septon-(fat_one) 0.000000
147 Jacks 0.000000
141 Rhaego 0.000000
148 Joss 0.000000
185 Timett 0.000000
181 Paxter-Redwyne 0.000000
139 Jommo 0.000000
150 Porther 0.000000
138 Donal-Noye 0.000000
151 Raymun-Darry 0.000000
152 Tobho-Mott 0.000000
173 Jonos-Bracken 0.000000
174 Jyck 0.000000
175 Morrec 0.000000
176 Kurleket 0.000000
171 Othor 0.000000
170 Matthar 0.000000
169 Kevan-Lannister 0.000000
168 Jafer-Flowers 0.000000
167 Hugh 0.000000
166 Hobb 0.000000
165 Heward 0.000000
177 Lancel-Lannister 0.000000
164 Harys-Swyft 0.000000
163 Gunthor-son-of-Gurn 0.000000
162 Todder 0.000000
161 Oswell-Whent 0.000000
178 Leo-Lefford 0.000000
180 Lyn-Corbray 0.000000
160 Will-(prologue) 0.000000
172 Maege-Mormont 0.000000
156 Wylla 0.000000
154 Varly 0.000000
153 Tregar 0.000000
157 Fogo 0.000000
0 Addam-Marbrand 0.000000
137 Dolf 0.000000
88 Chiggen 0.000000
82 Osha 0.000000
81 Nan 0.000000
80 Joseth 0.000000
78 Hodor 0.000000
77 Hallis-Mollen 0.000000
76 Hali 0.000000
75 Thoros-of-Myr 0.000000
69 Mance-Rayder 0.000000
64 Boros-Blount 0.000000
61 Balon-Swann 0.000000
60 Balon-Greyjoy 0.000000
59 Arys-Oakheart 0.000000
57 Vayon-Poole 0.000000
47 Mycah 0.000000
41 Desmond 0.000000
35 Arthur-Dayne 0.000000
25 Albett 0.000000
10 Clydas 0.000000
9 Chett 0.000000
8 Bowen-Marsh 0.000000
3 Aegon-I-Targaryen 0.000000
85 Stiv 0.000000
89 Marillion 0.000000
135 Daryn-Hornwood 0.000000
90 Shae 0.000000
134 Dareon 0.000000
133 Jared-Frey 0.000000
132 Hosteen-Frey 0.000000
127 Jhiqui 0.000000
124 Eroeh 0.000000
122 Coratt 0.000000
118 Cohollo 0.000000
116 Clement-Piper 0.000000
115 Chella 0.000000
113 Janos-Slynt 0.000000
111 Wendel-Manderly 0.000000
109 Stevron-Frey 0.000000
108 Robert-Arryn 0.000000
107 Mychel-Redfort 0.000000
106 Mya-Stone 0.000000
105 Moreo-Tumitis 0.000000
104 Masha-Heddle 0.000000
102 Eon-Hunter 0.000000
101 Donnel-Waynwood 0.000000
100 Colemon 0.000000
98 Nestor-Royce 0.000000
186 Ulf-son-of-Umar 0.000000

Closeness centrality is calculated as the reciprocal of the sum of the length of the shortest paths between the node and all other nodes in the graph. Thus, the more central a node is, the closer it is to all other nodes.

In [18]:
networkx.closeness_centrality(G)
Out[18]:
{'Addam-Marbrand': 0.3234782608695652,
 'Jaime-Lannister': 0.46153846153846156,
 'Tywin-Lannister': 0.4492753623188406,
 'Aegon-I-Targaryen': 0.3765182186234818,
 'Daenerys-Targaryen': 0.40522875816993464,
 'Eddard-Stark': 0.5636363636363636,
 'Aemon-Targaryen-(Maester-Aemon)': 0.33695652173913043,
 'Alliser-Thorne': 0.36328125,
 'Bowen-Marsh': 0.33574007220216606,
 'Chett': 0.3333333333333333,
 'Clydas': 0.25237449118046135,
 'Jeor-Mormont': 0.4161073825503356,
 'Jon-Snow': 0.493368700265252,
 'Samwell-Tarly': 0.3576923076923077,
 'Aerys-II-Targaryen': 0.38589211618257263,
 'Brandon-Stark': 0.3803680981595092,
 'Gerold-Hightower': 0.3803680981595092,
 'Jon-Arryn': 0.4376470588235294,
 'Robert-Baratheon': 0.5454545454545454,
 'Aggo': 0.2929133858267717,
 'Drogo': 0.3795918367346939,
 'Jhogo': 0.2929133858267717,
 'Jorah-Mormont': 0.37424547283702214,
 'Quaro': 0.29245283018867924,
 'Rakharo': 0.29245283018867924,
 'Albett': 0.33214285714285713,
 'Halder': 0.33574007220216606,
 'Rast': 0.3345323741007194,
 'Grenn': 0.33634719710669075,
 'Pypar': 0.33513513513513515,
 'Tyrion-Lannister': 0.510989010989011,
 'Alyn': 0.3803680981595092,
 'Harwin': 0.36470588235294116,
 'Jory-Cassel': 0.43661971830985913,
 'Tomard': 0.375,
 'Arthur-Dayne': 0.27596439169139464,
 'Arya-Stark': 0.458128078817734,
 'Benjen-Stark': 0.4325581395348837,
 'Bran-Stark': 0.4869109947643979,
 'Catelyn-Stark': 0.5054347826086957,
 'Cersei-Lannister': 0.484375,
 'Desmond': 0.3683168316831683,
 'Ilyn-Payne': 0.3866943866943867,
 'Jeyne-Poole': 0.36470588235294116,
 'Joffrey-Baratheon': 0.4806201550387597,
 'Meryn-Trant': 0.3675889328063241,
 'Mordane': 0.40611353711790393,
 'Mycah': 0.3727454909819639,
 'Myrcella-Baratheon': 0.3661417322834646,
 'Petyr-Baelish': 0.457002457002457,
 'Rickon-Stark': 0.4397163120567376,
 'Robb-Stark': 0.49732620320855614,
 'Rodrik-Cassel': 0.45365853658536587,
 'Sandor-Clegane': 0.41517857142857145,
 'Sansa-Stark': 0.48947368421052634,
 'Syrio-Forel': 0.3697813121272366,
 'Tommen-Baratheon': 0.39323467230443976,
 'Vayon-Poole': 0.38271604938271603,
 'Yoren': 0.38509316770186336,
 'Arys-Oakheart': 0.3268892794376098,
 'Balon-Greyjoy': 0.3765182186234818,
 'Balon-Swann': 0.3059210526315789,
 'Renly-Baratheon': 0.43457943925233644,
 'Barristan-Selmy': 0.39323467230443976,
 'Boros-Blount': 0.33941605839416056,
 'Pycelle': 0.39828693790149894,
 'Varys': 0.4460431654676259,
 'Jaremy-Rykker': 0.33574007220216606,
 'Luwin': 0.4492753623188406,
 'Mance-Rayder': 0.3024390243902439,
 'Theon-Greyjoy': 0.45036319612590797,
 'Waymar-Royce': 0.3044189852700491,
 'Beric-Dondarrion': 0.3835051546391753,
 'Gregor-Clegane': 0.4189189189189189,
 'Loras-Tyrell': 0.39828693790149894,
 'Thoros-of-Myr': 0.27761194029850744,
 'Hali': 0.328042328042328,
 'Hallis-Mollen': 0.3549618320610687,
 'Hodor': 0.3412844036697248,
 'Hullen': 0.3516068052930057,
 'Joseth': 0.3286219081272085,
 'Nan': 0.3412844036697248,
 'Osha': 0.34065934065934067,
 'Rickard-Karstark': 0.40611353711790393,
 'Rickard-Stark': 0.33037300177619894,
 'Stiv': 0.340036563071298,
 'Lyanna-Stark': 0.3803680981595092,
 'Bronn': 0.3712574850299401,
 'Chiggen': 0.340036563071298,
 'Marillion': 0.36186770428015563,
 'Shae': 0.33941605839416056,
 'Shagga': 0.3438077634011091,
 'Vardis-Egen': 0.3597678916827853,
 'Willis-Wode': 0.36257309941520466,
 'Brynden-Tully': 0.384297520661157,
 'Edmure-Tully': 0.40522875816993464,
 'Hoster-Tully': 0.3835051546391753,
 'Lysa-Arryn': 0.4295612009237875,
 'Nestor-Royce': 0.33695652173913043,
 'Walder-Frey': 0.36046511627906974,
 'Colemon': 0.33695652173913043,
 'Donnel-Waynwood': 0.33634719710669075,
 'Eon-Hunter': 0.33879781420765026,
 'Jon-Umber-(Greatjon)': 0.3583815028901734,
 'Masha-Heddle': 0.33634719710669075,
 'Moreo-Tumitis': 0.33634719710669075,
 'Mya-Stone': 0.33634719710669075,
 'Mychel-Redfort': 0.33634719710669075,
 'Robert-Arryn': 0.3576923076923077,
 'Stevron-Frey': 0.33879781420765026,
 'Tytos-Blackwood': 0.340036563071298,
 'Wendel-Manderly': 0.34896810506566606,
 'Cayn': 0.36328125,
 'Janos-Slynt': 0.38271604938271603,
 'Stannis-Baratheon': 0.4017278617710583,
 'Chella': 0.33879781420765026,
 'Clement-Piper': 0.22572815533980584,
 'Karyl-Vance': 0.29107981220657275,
 'Cohollo': 0.29199372056514916,
 'Haggo': 0.2929133858267717,
 'Qotho': 0.29337539432176657,
 'Conn': 0.34191176470588236,
 'Coratt': 0.2551440329218107,
 'Doreah': 0.2929133858267717,
 'Eroeh': 0.2888198757763975,
 'Illyrio-Mopatis': 0.36904761904761907,
 'Irri': 0.2938388625592417,
 'Jhiqui': 0.29245283018867924,
 'Mirri-Maz-Duur': 0.2929133858267717,
 'Rhaegar-Targaryen': 0.4,
 'Viserys-Targaryen': 0.3705179282868526,
 'Danwell-Frey': 0.266857962697274,
 'Hosteen-Frey': 0.21112372304199772,
 'Jared-Frey': 0.21112372304199772,
 'Dareon': 0.3309608540925267,
 'Daryn-Hornwood': 0.36186770428015563,
 'Torrhen-Karstark': 0.3780487804878049,
 'Dolf': 0.256198347107438,
 'Donal-Noye': 0.3309608540925267,
 'Jommo': 0.2763744427934621,
 'Ogo': 0.2767857142857143,
 'Rhaego': 0.27555555555555555,
 'Dywen': 0.3309608540925267,
 'Galbart-Glover': 0.3835051546391753,
 'Gendry': 0.3611650485436893,
 'High-Septon-(fat_one)': 0.36470588235294116,
 'Howland-Reed': 0.3611650485436893,
 'Jacks': 0.3611650485436893,
 'Joss': 0.3611650485436893,
 'Marq-Piper': 0.37575757575757573,
 'Porther': 0.3611650485436893,
 'Raymun-Darry': 0.3765182186234818,
 'Tobho-Mott': 0.3611650485436893,
 'Tregar': 0.3611650485436893,
 'Varly': 0.3611650485436893,
 'Wyl-(guard)': 0.3675889328063241,
 'Wylla': 0.3611650485436893,
 'Fogo': 0.21703617269544925,
 'Roose-Bolton': 0.3425414364640884,
 'Gared': 0.2961783439490446,
 'Will-(prologue)': 0.2363405336721728,
 'Oswell-Whent': 0.27596439169139464,
 'Todder': 0.33273703041144903,
 'Gunthor-son-of-Gurn': 0.33879781420765026,
 'Harys-Swyft': 0.3163265306122449,
 'Heward': 0.30491803278688523,
 'Hobb': 0.3309608540925267,
 'Hugh': 0.30491803278688523,
 'Jafer-Flowers': 0.2516914749661705,
 'Kevan-Lannister': 0.3570057581573896,
 'Matthar': 0.3309608540925267,
 'Othor': 0.3309608540925267,
 'Maege-Mormont': 0.290625,
 'Jonos-Bracken': 0.2540983606557377,
 'Jyck': 0.33941605839416056,
 'Morrec': 0.33941605839416056,
 'Kurleket': 0.33879781420765026,
 'Lancel-Lannister': 0.35361216730038025,
 'Leo-Lefford': 0.33879781420765026,
 'Mace-Tyrell': 0.28615384615384615,
 'Lyn-Corbray': 0.30097087378640774,
 'Paxter-Redwyne': 0.22275449101796407,
 'Maegor-I-Targaryen': 0.3292035398230089,
 'Mord': 0.33879781420765026,
 'Randyll-Tarly': 0.26382978723404255,
 'Timett': 0.34065934065934067,
 'Ulf-son-of-Umar': 0.256198347107438}
In [29]:
closeness_centrality = networkx.closeness_centrality(G)
networkx.set_node_attributes(G, name='closeness', values=closeness_centrality)
closeness_df = pd.DataFrame(G.nodes(data='closeness'), columns=['node', 'closeness'])
closeness_df = closeness_df.sort_values(by='closeness', ascending=False)
closeness_df
Out[29]:
node closeness
5 Eddard-Stark 0.563636
18 Robert-Baratheon 0.545455
30 Tyrion-Lannister 0.510989
39 Catelyn-Stark 0.505435
51 Robb-Stark 0.497326
12 Jon-Snow 0.493369
54 Sansa-Stark 0.489474
38 Bran-Stark 0.486911
40 Cersei-Lannister 0.484375
44 Joffrey-Baratheon 0.480620
1 Jaime-Lannister 0.461538
36 Arya-Stark 0.458128
49 Petyr-Baelish 0.457002
52 Rodrik-Cassel 0.453659
70 Theon-Greyjoy 0.450363
2 Tywin-Lannister 0.449275
68 Luwin 0.449275
66 Varys 0.446043
50 Rickon-Stark 0.439716
17 Jon-Arryn 0.437647
33 Jory-Cassel 0.436620
62 Renly-Baratheon 0.434579
37 Benjen-Stark 0.432558
97 Lysa-Arryn 0.429561
73 Gregor-Clegane 0.418919
11 Jeor-Mormont 0.416107
53 Sandor-Clegane 0.415179
46 Mordane 0.406114
83 Rickard-Karstark 0.406114
4 Daenerys-Targaryen 0.405229
95 Edmure-Tully 0.405229
114 Stannis-Baratheon 0.401728
129 Rhaegar-Targaryen 0.400000
74 Loras-Tyrell 0.398287
65 Pycelle 0.398287
63 Barristan-Selmy 0.393235
56 Tommen-Baratheon 0.393235
42 Ilyn-Payne 0.386694
14 Aerys-II-Targaryen 0.385892
58 Yoren 0.385093
94 Brynden-Tully 0.384298
143 Galbart-Glover 0.383505
72 Beric-Dondarrion 0.383505
96 Hoster-Tully 0.383505
113 Janos-Slynt 0.382716
57 Vayon-Poole 0.382716
31 Alyn 0.380368
15 Brandon-Stark 0.380368
16 Gerold-Hightower 0.380368
86 Lyanna-Stark 0.380368
20 Drogo 0.379592
136 Torrhen-Karstark 0.378049
151 Raymun-Darry 0.376518
60 Balon-Greyjoy 0.376518
3 Aegon-I-Targaryen 0.376518
149 Marq-Piper 0.375758
34 Tomard 0.375000
22 Jorah-Mormont 0.374245
47 Mycah 0.372745
87 Bronn 0.371257
130 Viserys-Targaryen 0.370518
55 Syrio-Forel 0.369781
125 Illyrio-Mopatis 0.369048
41 Desmond 0.368317
45 Meryn-Trant 0.367589
155 Wyl-(guard) 0.367589
48 Myrcella-Baratheon 0.366142
145 High-Septon-(fat_one) 0.364706
43 Jeyne-Poole 0.364706
32 Harwin 0.364706
112 Cayn 0.363281
7 Alliser-Thorne 0.363281
93 Willis-Wode 0.362573
89 Marillion 0.361868
135 Daryn-Hornwood 0.361868
150 Porther 0.361165
152 Tobho-Mott 0.361165
153 Tregar 0.361165
156 Wylla 0.361165
154 Varly 0.361165
148 Joss 0.361165
147 Jacks 0.361165
144 Gendry 0.361165
146 Howland-Reed 0.361165
99 Walder-Frey 0.360465
92 Vardis-Egen 0.359768
103 Jon-Umber-(Greatjon) 0.358382
13 Samwell-Tarly 0.357692
108 Robert-Arryn 0.357692
169 Kevan-Lannister 0.357006
77 Hallis-Mollen 0.354962
177 Lancel-Lannister 0.353612
79 Hullen 0.351607
111 Wendel-Manderly 0.348968
91 Shagga 0.343808
158 Roose-Bolton 0.342541
121 Conn 0.341912
81 Nan 0.341284
78 Hodor 0.341284
185 Timett 0.340659
82 Osha 0.340659
88 Chiggen 0.340037
85 Stiv 0.340037
110 Tytos-Blackwood 0.340037
64 Boros-Blount 0.339416
174 Jyck 0.339416
90 Shae 0.339416
175 Morrec 0.339416
115 Chella 0.338798
163 Gunthor-son-of-Gurn 0.338798
176 Kurleket 0.338798
109 Stevron-Frey 0.338798
102 Eon-Hunter 0.338798
178 Leo-Lefford 0.338798
183 Mord 0.338798
6 Aemon-Targaryen-(Maester-Aemon) 0.336957
98 Nestor-Royce 0.336957
100 Colemon 0.336957
107 Mychel-Redfort 0.336347
28 Grenn 0.336347
106 Mya-Stone 0.336347
105 Moreo-Tumitis 0.336347
104 Masha-Heddle 0.336347
101 Donnel-Waynwood 0.336347
26 Halder 0.335740
8 Bowen-Marsh 0.335740
67 Jaremy-Rykker 0.335740
29 Pypar 0.335135
27 Rast 0.334532
9 Chett 0.333333
162 Todder 0.332737
25 Albett 0.332143
134 Dareon 0.330961
138 Donal-Noye 0.330961
171 Othor 0.330961
166 Hobb 0.330961
170 Matthar 0.330961
142 Dywen 0.330961
84 Rickard-Stark 0.330373
182 Maegor-I-Targaryen 0.329204
80 Joseth 0.328622
76 Hali 0.328042
59 Arys-Oakheart 0.326889
0 Addam-Marbrand 0.323478
164 Harys-Swyft 0.316327
61 Balon-Swann 0.305921
165 Heward 0.304918
167 Hugh 0.304918
71 Waymar-Royce 0.304419
69 Mance-Rayder 0.302439
180 Lyn-Corbray 0.300971
159 Gared 0.296178
126 Irri 0.293839
120 Qotho 0.293375
128 Mirri-Maz-Duur 0.292913
19 Aggo 0.292913
119 Haggo 0.292913
123 Doreah 0.292913
21 Jhogo 0.292913
23 Quaro 0.292453
24 Rakharo 0.292453
127 Jhiqui 0.292453
118 Cohollo 0.291994
117 Karyl-Vance 0.291080
172 Maege-Mormont 0.290625
124 Eroeh 0.288820
179 Mace-Tyrell 0.286154
75 Thoros-of-Myr 0.277612
140 Ogo 0.276786
139 Jommo 0.276374
161 Oswell-Whent 0.275964
35 Arthur-Dayne 0.275964
141 Rhaego 0.275556
131 Danwell-Frey 0.266858
184 Randyll-Tarly 0.263830
137 Dolf 0.256198
186 Ulf-son-of-Umar 0.256198
122 Coratt 0.255144
173 Jonos-Bracken 0.254098
10 Clydas 0.252374
168 Jafer-Flowers 0.251691
160 Will-(prologue) 0.236341
116 Clement-Piper 0.225728
181 Paxter-Redwyne 0.222754
157 Fogo 0.217036
132 Hosteen-Frey 0.211124
133 Jared-Frey 0.211124
In [20]:
# Adding rank to the degree df
degree_df['degree_rank'] = range(1, len(degree_df) + 1)
degree_df
Out[20]:
node weighted_degree degree_rank
5 Eddard-Stark 1284 1
18 Robert-Baratheon 941 2
12 Jon-Snow 784 3
30 Tyrion-Lannister 650 4
54 Sansa-Stark 545 5
38 Bran-Stark 531 6
39 Catelyn-Stark 520 7
51 Robb-Stark 516 8
4 Daenerys-Targaryen 443 9
36 Arya-Stark 430 10
40 Cersei-Lannister 424 11
44 Joffrey-Baratheon 422 12
49 Petyr-Baelish 313 13
20 Drogo 256 14
1 Jaime-Lannister 241 15
66 Varys 231 16
68 Luwin 210 17
62 Renly-Baratheon 186 18
2 Tywin-Lannister 181 19
11 Jeor-Mormont 181 20
17 Jon-Arryn 169 21
22 Jorah-Mormont 154 22
33 Jory-Cassel 152 23
65 Pycelle 150 24
13 Samwell-Tarly 148 25
63 Barristan-Selmy 143 26
46 Mordane 141 27
97 Lysa-Arryn 138 28
53 Sandor-Clegane 135 29
70 Theon-Greyjoy 133 30
52 Rodrik-Cassel 124 31
87 Bronn 109 32
29 Pypar 107 33
114 Stannis-Baratheon 105 34
130 Viserys-Targaryen 105 35
37 Benjen-Stark 104 36
50 Rickon-Stark 98 37
28 Grenn 85 38
126 Irri 81 39
7 Alliser-Thorne 80 40
74 Loras-Tyrell 78 41
73 Gregor-Clegane 74 42
6 Aemon-Targaryen-(Maester-Aemon) 74 43
125 Illyrio-Mopatis 70 44
94 Brynden-Tully 69 45
120 Qotho 68 46
128 Mirri-Maz-Duur 63 47
127 Jhiqui 60 48
26 Halder 56 49
48 Myrcella-Baratheon 54 50
43 Jeyne-Poole 54 51
129 Rhaegar-Targaryen 53 52
78 Hodor 52 53
123 Doreah 51 54
47 Mycah 50 55
95 Edmure-Tully 48 56
83 Rickard-Karstark 46 57
119 Haggo 45 58
91 Shagga 44 59
42 Ilyn-Payne 43 60
21 Jhogo 43 61
34 Tomard 42 62
99 Walder-Frey 41 63
15 Brandon-Stark 41 64
103 Jon-Umber-(Greatjon) 39 65
56 Tommen-Baratheon 37 66
14 Aerys-II-Targaryen 37 67
77 Hallis-Mollen 37 68
92 Vardis-Egen 37 69
19 Aggo 36 70
86 Lyanna-Stark 36 71
45 Meryn-Trant 36 72
71 Waymar-Royce 34 73
58 Yoren 33 74
81 Nan 33 75
31 Alyn 32 76
162 Todder 31 77
55 Syrio-Forel 31 78
82 Osha 31 79
160 Will-(prologue) 30 80
89 Marillion 29 81
159 Gared 29 82
118 Cohollo 29 83
93 Willis-Wode 28 84
57 Vayon-Poole 28 85
8 Bowen-Marsh 28 86
169 Kevan-Lannister 28 87
72 Beric-Dondarrion 28 88
27 Rast 26 89
64 Boros-Blount 26 90
113 Janos-Slynt 25 91
67 Jaremy-Rykker 24 92
24 Rakharo 24 93
96 Hoster-Tully 24 94
143 Galbart-Glover 23 95
23 Quaro 21 96
121 Conn 20 97
183 Mord 20 98
79 Hullen 20 99
108 Robert-Arryn 20 100
158 Roose-Bolton 19 101
9 Chett 19 102
140 Ogo 18 103
32 Harwin 18 104
149 Marq-Piper 18 105
16 Gerold-Hightower 17 106
88 Chiggen 16 107
112 Cayn 16 108
155 Wyl-(guard) 15 109
90 Shae 15 110
151 Raymun-Darry 12 111
174 Jyck 11 112
25 Albett 11 113
136 Torrhen-Karstark 11 114
117 Karyl-Vance 11 115
110 Tytos-Blackwood 10 116
175 Morrec 10 117
131 Danwell-Frey 9 118
0 Addam-Marbrand 9 119
106 Mya-Stone 9 120
102 Eon-Hunter 9 121
3 Aegon-I-Targaryen 9 122
165 Heward 8 123
109 Stevron-Frey 8 124
172 Maege-Mormont 8 125
145 High-Septon-(fat_one) 8 126
137 Dolf 7 127
60 Balon-Greyjoy 7 128
41 Desmond 7 129
152 Tobho-Mott 7 130
80 Joseth 7 131
84 Rickard-Stark 7 132
163 Gunthor-son-of-Gurn 7 133
138 Donal-Noye 7 134
85 Stiv 7 135
100 Colemon 7 136
185 Timett 7 137
111 Wendel-Manderly 6 138
98 Nestor-Royce 6 139
179 Mace-Tyrell 6 140
61 Balon-Swann 6 141
135 Daryn-Hornwood 6 142
132 Hosteen-Frey 6 143
139 Jommo 6 144
133 Jared-Frey 6 145
134 Dareon 6 146
166 Hobb 5 147
122 Coratt 5 148
171 Othor 5 149
75 Thoros-of-Myr 5 150
124 Eroeh 5 151
115 Chella 5 152
148 Joss 5 153
10 Clydas 5 154
184 Randyll-Tarly 5 155
176 Kurleket 5 156
104 Masha-Heddle 5 157
141 Rhaego 5 158
35 Arthur-Dayne 4 159
178 Leo-Lefford 4 160
182 Maegor-I-Targaryen 4 161
116 Clement-Piper 4 162
153 Tregar 4 163
150 Porther 4 164
157 Fogo 4 165
142 Dywen 4 166
147 Jacks 4 167
154 Varly 4 168
186 Ulf-son-of-Umar 4 169
168 Jafer-Flowers 3 170
170 Matthar 3 171
107 Mychel-Redfort 3 172
105 Moreo-Tumitis 3 173
101 Donnel-Waynwood 3 174
161 Oswell-Whent 3 175
181 Paxter-Redwyne 3 176
180 Lyn-Corbray 3 177
144 Gendry 3 178
146 Howland-Reed 3 179
177 Lancel-Lannister 3 180
69 Mance-Rayder 3 181
164 Harys-Swyft 3 182
76 Hali 3 183
173 Jonos-Bracken 3 184
167 Hugh 3 185
156 Wylla 3 186
59 Arys-Oakheart 3 187

Task 2¶

In [50]:
# Referring to what we have done to the degree_df, add "eigenvector_rank" column to eigenvector_df, "betweenness_rank" column 
# to betweenness_df, and "closeness_rank" to closeness_df
# Then create an "all_rank_df" by merging degree_df, eigenvector_df, betweenness_df, and closeness_df
# (this can be done by merging two dataframes at a time)
# Finally, slice the columns so that the final ouput should only have 5 columns: node and four different ranks
pd.set_option('display.width', 1000) #Reference since I was having issues having all the columns being displayed at once https://pandas.pydata.org/docs/user_guide/options.html

eigenvector_df['eigenvector_rank'] = range(1, len(eigenvector_df) + 1)
betweenness_df['betweenness_rank'] = range(1, len(betweenness_df) + 1)
closeness_df['closeness_rank'] = range(1, len(closeness_df) + 1)

merge_df = pd.merge(degree_df, eigenvector_df)

merge_df = pd.merge(merge_df, betweenness_df)

merge_df = pd.merge(merge_df, closeness_df)

all_rank_df = merge_df[['node', 'degree_rank', 'betweenness_rank', 'eigenvector_rank', 'closeness_rank']]

print(all_rank_df)
                                node  degree_rank  betweenness_rank  eigenvector_rank  closeness_rank
0                       Eddard-Stark            1                 1                 1               1
1                   Robert-Baratheon            2                 2                 2               2
2                           Jon-Snow            3                 4                13               6
3                   Tyrion-Lannister            4                 3                 4               3
4                        Sansa-Stark            5                10                 3               7
5                         Bran-Stark            6                 9                10               8
6                      Catelyn-Stark            7                 5                 7               4
7                         Robb-Stark            8                 7                11               5
8                 Daenerys-Targaryen            9                 6                49              30
9                         Arya-Stark           10                25                12              12
10                  Cersei-Lannister           11                15                 6               9
11                 Joffrey-Baratheon           12                22                 5              10
12                     Petyr-Baelish           13                43                 8              13
13                             Drogo           14                 8                85              51
14                   Jaime-Lannister           15                13                 9              11
15                             Varys           16                31                15              18
16                             Luwin           17                27                27              17
17                   Renly-Baratheon           18                40                14              22
18                   Tywin-Lannister           19                16                17              16
19                      Jeor-Mormont           20                14                40              26
20                         Jon-Arryn           21                29                20              20
21                     Jorah-Mormont           22                28                82              58
22                       Jory-Cassel           23                24                16              21
23                           Pycelle           24                69                22              35
24                     Samwell-Tarly           25                26                71              88
25                   Barristan-Selmy           26                57                21              36
26                           Mordane           27                60                26              28
27                        Lysa-Arryn           28                23                29              24
28                    Sandor-Clegane           29                45                19              27
29                     Theon-Greyjoy           30                39                25              15
30                     Rodrik-Cassel           31                41                18              14
31                             Bronn           32                49                52              60
32                             Pypar           33                84               111             128
33                 Stannis-Baratheon           34                66                23              32
34                 Viserys-Targaryen           35                53                84              61
35                      Benjen-Stark           36                11                31              23
36                      Rickon-Stark           37                81                30              19
37                             Grenn           38                77                92             120
38                              Irri           39                80               148             153
39                    Alliser-Thorne           40                47                67              72
40                      Loras-Tyrell           41                17                24              34
41                    Gregor-Clegane           42                52                28              25
42   Aemon-Targaryen-(Maester-Aemon)           43                30                95             116
43                   Illyrio-Mopatis           44                58                87              63
44                     Brynden-Tully           45                50                37              41
45                             Qotho           46                79               152             154
46                    Mirri-Maz-Duur           47                88               153             155
47                            Jhiqui           48               169               154             162
48                            Halder           49                76               109             125
49                Myrcella-Baratheon           50                93                39              67
50                       Jeyne-Poole           51                87                47              69
51                 Rhaegar-Targaryen           52                54                45              33
52                             Hodor           53               144                77              99
53                            Doreah           54                96               149             158
54                             Mycah           55               154                44              59
55                      Edmure-Tully           56                21                36              31
56                  Rickard-Karstark           57                42                38              29
57                             Haggo           58                86               162             157
58                            Shagga           59                18               108              95
59                        Ilyn-Payne           60                70                32              38
60                             Jhogo           61                89               151             159
61                            Tomard           62                73                41              57
62                       Walder-Frey           63                12                59              85
63                     Brandon-Stark           64                55                50              48
64              Jon-Umber-(Greatjon)           65                51                58              87
65                  Tommen-Baratheon           66                63                34              37
66                Aerys-II-Targaryen           67                64                48              39
67                     Hallis-Mollen           68               145                55              91
68                       Vardis-Egen           69                59                66              86
69                              Aggo           70                85               155             156
70                      Lyanna-Stark           71                91                53              50
71                       Meryn-Trant           72                62                35              65
72                      Waymar-Royce           73                44               160             149
73                             Yoren           74                75                42              40
74                               Nan           75               142                76              98
75                              Alyn           76                72                57              47
76                            Todder           77               128               131             131
77                       Syrio-Forel           78                78                72              62
78                              Osha           79               141                80             101
79                   Will-(prologue)           80               132               182             182
80                         Marillion           81               163                62              74
81                             Gared           82                48               166             152
82                           Cohollo           83               172               165             163
83                       Willis-Wode           84                68                61              73
84                       Vayon-Poole           85               153                43              46
85                       Bowen-Marsh           86               160                97             126
86                   Kevan-Lannister           87               120                70              90
87                  Beric-Dondarrion           88                33                46              43
88                              Rast           89                83               116             129
89                      Boros-Blount           90               149                51             105
90                       Janos-Slynt           91               175                33              45
91                     Jaremy-Rykker           92                37                94             127
92                           Rakharo           93                90               164             161
93                      Hoster-Tully           94                67                60              44
94                    Galbart-Glover           95                61                54              42
95                             Quaro           96                92               163             160
96                              Conn           97                32               119              97
97                              Mord           98               100               128             115
98                            Hullen           99                65                56              93
99                      Robert-Arryn          100               178                73              89
100                     Roose-Bolton          101                94                81              96
101                            Chett          102               159               132             130
102                              Ogo          103                34               173             169
103                           Harwin          104                71                74              70
104                       Marq-Piper          105                46                65              56
105                 Gerold-Hightower          106                19                64              49
106                          Chiggen          107               140                96             102
107                             Cayn          108                95                78              71
108                      Wyl-(guard)          109                56                63              66
109                             Shae          110               165               115             107
110                     Raymun-Darry          111               112                69              53
111                             Jyck          112               115               123             106
112                           Albett          113               157               138             132
113                 Torrhen-Karstark          114                74                79              52
114                      Karyl-Vance          115                38               158             164
115                  Tytos-Blackwood          116                36               112             104
116                           Morrec          117               116               122             108
117                     Danwell-Frey          118                20               169             174
118                   Addam-Marbrand          119               138                91             144
119                        Mya-Stone          120               180               133             121
120                       Eon-Hunter          121               183                90             113
121                Aegon-I-Targaryen          122               161                89              55
122                           Heward          123               124               141             147
123                     Stevron-Frey          124               177               117             112
124                    Maege-Mormont          125               133               157             165
125            High-Septon-(fat_one)          126               103                75              68
126                             Dolf          127               139               178             176
127                    Balon-Greyjoy          128               151                68              54
128                          Desmond          129               155                83              64
129                       Tobho-Mott          130               113               100              77
130                           Joseth          131               143               107             141
131                    Rickard-Stark          132                82               121             139
132              Gunthor-son-of-Gurn          133               127               127             110
133                       Donal-Noye          134               111               142             134
134                             Stiv          135               162                88             103
135                          Colemon          136               185               118             118
136                           Timett          137               107               120             100
137                  Wendel-Manderly          138               176                86              94
138                     Nestor-Royce          139               186               113             117
139                      Mace-Tyrell          140                35               156             167
140                      Balon-Swann          141               150               110             146
141                   Daryn-Hornwood          142               164                93              75
142                     Hosteen-Frey          143               168               186             186
143                            Jommo          144               109               174             170
144                       Jared-Frey          145               167               185             187
145                           Dareon          146               166               145             133
146                             Hobb          147               123               143             136
147                           Coratt          148               171               181             178
148                            Othor          149               118               147             135
149                    Thoros-of-Myr          150               147               167             168
150                            Eroeh          151               170               168             166
151                           Chella          152               174               129             109
152                             Joss          153               106                98              81
153                           Clydas          154               158               177             180
154                    Randyll-Tarly          155               101               172             175
155                         Kurleket          156               117               126             111
156                     Masha-Heddle          157               182               135             123
157                           Rhaego          158               105               175             173
158                     Arthur-Dayne          159               156               171             172
159                      Leo-Lefford          160               130               125             114
160               Maegor-I-Targaryen          161                99               124             140
161                    Clement-Piper          162               173               184             183
162                           Tregar          163               136               101              78
163                          Porther          164               110               104              76
164                             Fogo          165               137               187             185
165                            Dywen          166               102               144             138
166                            Jacks          167               104                99              82
167                            Varly          168               135               102              80
168                  Ulf-son-of-Umar          169               187               179             177
169                    Jafer-Flowers          170               121               176             181
170                          Matthar          171               119               146             137
171                   Mychel-Redfort          172               179               137             119
172                    Moreo-Tumitis          173               181               134             122
173                  Donnel-Waynwood          174               184               136             124
174                     Oswell-Whent          175               129               170             171
175                   Paxter-Redwyne          176               108               183             184
176                      Lyn-Corbray          177               131               159             151
177                           Gendry          178                97               106              83
178                     Howland-Reed          179                98               105              84
179                 Lancel-Lannister          180               125               114              92
180                     Mance-Rayder          181               148               161             150
181                      Harys-Swyft          182               126               139             145
182                             Hali          183               146               140             142
183                    Jonos-Bracken          184               114               180             179
184                             Hugh          185               122               150             148
185                            Wylla          186               134               103              79
186                    Arys-Oakheart          187               152               130             143

Task 3¶

Discuss the results of task 2 in the next cell. You have to discuss at least two characters based on their ranks in different centralities. For instance, character A ranks high in all types of centralities; this may be attributed to reason XXX. On the other hand, character B ranks high in centrality C but low in centrality D, which could be a result of reason YYY. To provide context, please refer to the definitions of each centrality (included above) to clarify their implications. If you are familiar with A Song of Ice and Fire (or the television series Game of Thrones), you can attempt to interpret these results by exploring how a character's ranks in these centralities relate to the plot. If you haven't, you can just speculate on potential reasons, such as this character might have close association with key figures, leading to a high eigenvector centrality rank, etc.

Answer:As someone who is vaguely knowledgable of Game of Thrones, I am familiar with Eddard Stark in the early seasons of the show/book. This can be attributed to him having the highest ranking in all relative stats. He is a central figure in his family and influential over the area he lives in, idk if its from being a king, or a warrior or just a nice guy but from what I remember he was well know and well liked. This shows in each one of the rankings as eigenvector shows he knew many other important figures, and betweeness shows that he was central in many interactions. Also his closeness ranking shows the closeness between different nodes in a system so when there is a connection it would make sense that it would be through Eddard Stark. A figure not as influential as Eddard Stark is the individual that ranks 101 Roose Bolton. What makes him not as central is that his key importance from my brief google search is only being the head of his house. Though this gives him more influence in the story over other minor characters it keeps him locked from say having a more central role in the story as many of his interactions and connections are locked into a niche of the story within his own family. He does know and interact with individuals outside of his family but only in limited forms unlike Eddard Stark. This makes sense as Bolton's highest rank is in Eigenvector as the head of his family he would also mingle and meet with other heads of other families. But his other rankings are lacking as Bolton isn't a figure you need to go through to know others which hurts his betweeness and closeness rankings.

Communities¶

In [51]:
# Import the "community" module for network community detection
from networkx.algorithms import community
In [52]:
# Calculate communities with greedy_modularity_communities function, 
# which uses Clauset-Newman-Moore greedy modularity maximization to find the community partition with the largest modularity
communities = community.greedy_modularity_communities(G)
communities
Out[52]:
[frozenset({'Addam-Marbrand',
            'Bran-Stark',
            'Bronn',
            'Brynden-Tully',
            'Catelyn-Stark',
            'Chella',
            'Chiggen',
            'Colemon',
            'Donnel-Waynwood',
            'Edmure-Tully',
            'Eon-Hunter',
            'Galbart-Glover',
            'Gunthor-son-of-Gurn',
            'Hali',
            'Hallis-Mollen',
            'Harys-Swyft',
            'Heward',
            'Hodor',
            'Hoster-Tully',
            'Hullen',
            'Jaime-Lannister',
            'Jon-Umber-(Greatjon)',
            'Jonos-Bracken',
            'Jory-Cassel',
            'Joseth',
            'Jyck',
            'Kevan-Lannister',
            'Kurleket',
            'Leo-Lefford',
            'Luwin',
            'Lyn-Corbray',
            'Lysa-Arryn',
            'Maege-Mormont',
            'Marillion',
            'Masha-Heddle',
            'Mord',
            'Moreo-Tumitis',
            'Morrec',
            'Mya-Stone',
            'Mychel-Redfort',
            'Nan',
            'Nestor-Royce',
            'Osha',
            'Rickard-Karstark',
            'Rickon-Stark',
            'Robb-Stark',
            'Robert-Arryn',
            'Rodrik-Cassel',
            'Roose-Bolton',
            'Shae',
            'Stevron-Frey',
            'Stiv',
            'Theon-Greyjoy',
            'Tyrion-Lannister',
            'Tytos-Blackwood',
            'Tywin-Lannister',
            'Vardis-Egen',
            'Walder-Frey',
            'Wendel-Manderly',
            'Willis-Wode',
            'Wyl-(guard)'}),
 frozenset({'Arya-Stark',
            'Arys-Oakheart',
            'Balon-Swann',
            'Barristan-Selmy',
            'Beric-Dondarrion',
            'Boros-Blount',
            'Cersei-Lannister',
            'Gregor-Clegane',
            'Hugh',
            'Ilyn-Payne',
            'Janos-Slynt',
            'Jeyne-Poole',
            'Joffrey-Baratheon',
            'Jon-Arryn',
            'Lancel-Lannister',
            'Loras-Tyrell',
            'Mace-Tyrell',
            'Maegor-I-Targaryen',
            'Meryn-Trant',
            'Mordane',
            'Mycah',
            'Myrcella-Baratheon',
            'Paxter-Redwyne',
            'Petyr-Baelish',
            'Pycelle',
            'Renly-Baratheon',
            'Robert-Baratheon',
            'Sandor-Clegane',
            'Sansa-Stark',
            'Stannis-Baratheon',
            'Syrio-Forel',
            'Thoros-of-Myr',
            'Tommen-Baratheon',
            'Varys',
            'Vayon-Poole'}),
 frozenset({'Aegon-I-Targaryen',
            'Aerys-II-Targaryen',
            'Alyn',
            'Arthur-Dayne',
            'Balon-Greyjoy',
            'Brandon-Stark',
            'Cayn',
            'Clement-Piper',
            'Daryn-Hornwood',
            'Desmond',
            'Eddard-Stark',
            'Gendry',
            'Gerold-Hightower',
            'Harwin',
            'High-Septon-(fat_one)',
            'Howland-Reed',
            'Jacks',
            'Joss',
            'Karyl-Vance',
            'Lyanna-Stark',
            'Marq-Piper',
            'Oswell-Whent',
            'Porther',
            'Raymun-Darry',
            'Rickard-Stark',
            'Tobho-Mott',
            'Tomard',
            'Torrhen-Karstark',
            'Tregar',
            'Varly',
            'Wylla'}),
 frozenset({'Aemon-Targaryen-(Maester-Aemon)',
            'Albett',
            'Alliser-Thorne',
            'Benjen-Stark',
            'Bowen-Marsh',
            'Chett',
            'Clydas',
            'Dareon',
            'Donal-Noye',
            'Dywen',
            'Gared',
            'Grenn',
            'Halder',
            'Hobb',
            'Jafer-Flowers',
            'Jaremy-Rykker',
            'Jeor-Mormont',
            'Jon-Snow',
            'Mance-Rayder',
            'Matthar',
            'Othor',
            'Pypar',
            'Randyll-Tarly',
            'Rast',
            'Samwell-Tarly',
            'Todder',
            'Waymar-Royce',
            'Will-(prologue)',
            'Yoren'}),
 frozenset({'Aggo',
            'Cohollo',
            'Daenerys-Targaryen',
            'Doreah',
            'Drogo',
            'Eroeh',
            'Fogo',
            'Haggo',
            'Illyrio-Mopatis',
            'Irri',
            'Jhiqui',
            'Jhogo',
            'Jommo',
            'Jorah-Mormont',
            'Mirri-Maz-Duur',
            'Ogo',
            'Qotho',
            'Quaro',
            'Rakharo',
            'Rhaegar-Targaryen',
            'Rhaego',
            'Viserys-Targaryen'}),
 frozenset({'Conn', 'Coratt', 'Dolf', 'Shagga', 'Timett', 'Ulf-son-of-Umar'}),
 frozenset({'Danwell-Frey', 'Hosteen-Frey', 'Jared-Frey'})]
In [53]:
# Make a dictionary by looping through the communities and, for each member of the community, adding their community number
modularity_class = {} # Create empty dictionary
count = 0
#Loop through each community in the network
for community in communities:
    #For each member of the community, add their community number
    for name in community:
        modularity_class[name] = count
    count += 1
modularity_class
Out[53]:
{'Shae': 0,
 'Nan': 0,
 'Nestor-Royce': 0,
 'Rodrik-Cassel': 0,
 'Catelyn-Stark': 0,
 'Harys-Swyft': 0,
 'Jonos-Bracken': 0,
 'Morrec': 0,
 'Tyrion-Lannister': 0,
 'Eon-Hunter': 0,
 'Jyck': 0,
 'Maege-Mormont': 0,
 'Theon-Greyjoy': 0,
 'Mord': 0,
 'Tytos-Blackwood': 0,
 'Lyn-Corbray': 0,
 'Kevan-Lannister': 0,
 'Hodor': 0,
 'Walder-Frey': 0,
 'Stevron-Frey': 0,
 'Rickard-Karstark': 0,
 'Leo-Lefford': 0,
 'Roose-Bolton': 0,
 'Marillion': 0,
 'Osha': 0,
 'Luwin': 0,
 'Jon-Umber-(Greatjon)': 0,
 'Hoster-Tully': 0,
 'Bronn': 0,
 'Vardis-Egen': 0,
 'Robb-Stark': 0,
 'Jory-Cassel': 0,
 'Edmure-Tully': 0,
 'Galbart-Glover': 0,
 'Bran-Stark': 0,
 'Wendel-Manderly': 0,
 'Gunthor-son-of-Gurn': 0,
 'Heward': 0,
 'Kurleket': 0,
 'Rickon-Stark': 0,
 'Jaime-Lannister': 0,
 'Lysa-Arryn': 0,
 'Donnel-Waynwood': 0,
 'Wyl-(guard)': 0,
 'Robert-Arryn': 0,
 'Chiggen': 0,
 'Mya-Stone': 0,
 'Hullen': 0,
 'Brynden-Tully': 0,
 'Addam-Marbrand': 0,
 'Moreo-Tumitis': 0,
 'Hali': 0,
 'Stiv': 0,
 'Hallis-Mollen': 0,
 'Chella': 0,
 'Joseth': 0,
 'Masha-Heddle': 0,
 'Tywin-Lannister': 0,
 'Willis-Wode': 0,
 'Colemon': 0,
 'Mychel-Redfort': 0,
 'Maegor-I-Targaryen': 1,
 'Arya-Stark': 1,
 'Myrcella-Baratheon': 1,
 'Boros-Blount': 1,
 'Hugh': 1,
 'Petyr-Baelish': 1,
 'Janos-Slynt': 1,
 'Loras-Tyrell': 1,
 'Tommen-Baratheon': 1,
 'Lancel-Lannister': 1,
 'Vayon-Poole': 1,
 'Mycah': 1,
 'Sansa-Stark': 1,
 'Barristan-Selmy': 1,
 'Robert-Baratheon': 1,
 'Mace-Tyrell': 1,
 'Syrio-Forel': 1,
 'Balon-Swann': 1,
 'Pycelle': 1,
 'Stannis-Baratheon': 1,
 'Mordane': 1,
 'Beric-Dondarrion': 1,
 'Gregor-Clegane': 1,
 'Ilyn-Payne': 1,
 'Thoros-of-Myr': 1,
 'Varys': 1,
 'Jeyne-Poole': 1,
 'Cersei-Lannister': 1,
 'Sandor-Clegane': 1,
 'Meryn-Trant': 1,
 'Renly-Baratheon': 1,
 'Paxter-Redwyne': 1,
 'Arys-Oakheart': 1,
 'Jon-Arryn': 1,
 'Joffrey-Baratheon': 1,
 'Arthur-Dayne': 2,
 'Daryn-Hornwood': 2,
 'Rickard-Stark': 2,
 'Marq-Piper': 2,
 'Clement-Piper': 2,
 'Joss': 2,
 'Tobho-Mott': 2,
 'Torrhen-Karstark': 2,
 'Cayn': 2,
 'Oswell-Whent': 2,
 'Karyl-Vance': 2,
 'Howland-Reed': 2,
 'Aegon-I-Targaryen': 2,
 'Wylla': 2,
 'High-Septon-(fat_one)': 2,
 'Tomard': 2,
 'Porther': 2,
 'Raymun-Darry': 2,
 'Aerys-II-Targaryen': 2,
 'Balon-Greyjoy': 2,
 'Jacks': 2,
 'Harwin': 2,
 'Varly': 2,
 'Lyanna-Stark': 2,
 'Tregar': 2,
 'Gendry': 2,
 'Desmond': 2,
 'Gerold-Hightower': 2,
 'Eddard-Stark': 2,
 'Brandon-Stark': 2,
 'Alyn': 2,
 'Mance-Rayder': 3,
 'Clydas': 3,
 'Donal-Noye': 3,
 'Randyll-Tarly': 3,
 'Bowen-Marsh': 3,
 'Gared': 3,
 'Yoren': 3,
 'Alliser-Thorne': 3,
 'Rast': 3,
 'Jeor-Mormont': 3,
 'Todder': 3,
 'Jon-Snow': 3,
 'Will-(prologue)': 3,
 'Halder': 3,
 'Hobb': 3,
 'Dywen': 3,
 'Samwell-Tarly': 3,
 'Jafer-Flowers': 3,
 'Pypar': 3,
 'Chett': 3,
 'Grenn': 3,
 'Dareon': 3,
 'Waymar-Royce': 3,
 'Matthar': 3,
 'Benjen-Stark': 3,
 'Jaremy-Rykker': 3,
 'Othor': 3,
 'Albett': 3,
 'Aemon-Targaryen-(Maester-Aemon)': 3,
 'Qotho': 4,
 'Cohollo': 4,
 'Drogo': 4,
 'Fogo': 4,
 'Mirri-Maz-Duur': 4,
 'Jhiqui': 4,
 'Irri': 4,
 'Rhaegar-Targaryen': 4,
 'Daenerys-Targaryen': 4,
 'Eroeh': 4,
 'Quaro': 4,
 'Viserys-Targaryen': 4,
 'Jhogo': 4,
 'Ogo': 4,
 'Haggo': 4,
 'Aggo': 4,
 'Illyrio-Mopatis': 4,
 'Doreah': 4,
 'Rakharo': 4,
 'Rhaego': 4,
 'Jommo': 4,
 'Jorah-Mormont': 4,
 'Ulf-son-of-Umar': 5,
 'Shagga': 5,
 'Timett': 5,
 'Dolf': 5,
 'Conn': 5,
 'Coratt': 5,
 'Jared-Frey': 6,
 'Hosteen-Frey': 6,
 'Danwell-Frey': 6}
In [54]:
# Add modularity class to the network as an attribute
networkx.set_node_attributes(G, modularity_class, 'modularity_class_b1')
In [55]:
# Make a Pandas dataframe from modularity class data
communities_df = pd.DataFrame(G.nodes(data='modularity_class_b1'), columns=['node', 'modularity_class_b1'])
communities_df = communities_df.sort_values(by='modularity_class_b1')
communities_df
Out[55]:
node modularity_class_b1
0 Addam-Marbrand 0
100 Colemon 0
99 Walder-Frey 0
98 Nestor-Royce 0
97 Lysa-Arryn 0
96 Hoster-Tully 0
95 Edmure-Tully 0
94 Brynden-Tully 0
92 Vardis-Egen 0
90 Shae 0
89 Marillion 0
101 Donnel-Waynwood 0
88 Chiggen 0
85 Stiv 0
83 Rickard-Karstark 0
82 Osha 0
81 Nan 0
80 Joseth 0
79 Hullen 0
78 Hodor 0
77 Hallis-Mollen 0
76 Hali 0
70 Theon-Greyjoy 0
87 Bronn 0
102 Eon-Hunter 0
103 Jon-Umber-(Greatjon) 0
104 Masha-Heddle 0
183 Mord 0
180 Lyn-Corbray 0
178 Leo-Lefford 0
176 Kurleket 0
175 Morrec 0
174 Jyck 0
173 Jonos-Bracken 0
172 Maege-Mormont 0
169 Kevan-Lannister 0
165 Heward 0
164 Harys-Swyft 0
163 Gunthor-son-of-Gurn 0
158 Roose-Bolton 0
155 Wyl-(guard) 0
143 Galbart-Glover 0
115 Chella 0
111 Wendel-Manderly 0
110 Tytos-Blackwood 0
109 Stevron-Frey 0
108 Robert-Arryn 0
107 Mychel-Redfort 0
106 Mya-Stone 0
105 Moreo-Tumitis 0
68 Luwin 0
52 Rodrik-Cassel 0
93 Willis-Wode 0
30 Tyrion-Lannister 0
51 Robb-Stark 0
39 Catelyn-Stark 0
38 Bran-Stark 0
50 Rickon-Stark 0
2 Tywin-Lannister 0
1 Jaime-Lannister 0
33 Jory-Cassel 0
44 Joffrey-Baratheon 1
43 Jeyne-Poole 1
42 Ilyn-Payne 1
40 Cersei-Lannister 1
36 Arya-Stark 1
53 Sandor-Clegane 1
181 Paxter-Redwyne 1
179 Mace-Tyrell 1
177 Lancel-Lannister 1
18 Robert-Baratheon 1
17 Jon-Arryn 1
113 Janos-Slynt 1
114 Stannis-Baratheon 1
167 Hugh 1
45 Meryn-Trant 1
46 Mordane 1
182 Maegor-I-Targaryen 1
48 Myrcella-Baratheon 1
59 Arys-Oakheart 1
47 Mycah 1
61 Balon-Swann 1
62 Renly-Baratheon 1
63 Barristan-Selmy 1
55 Syrio-Forel 1
54 Sansa-Stark 1
64 Boros-Blount 1
56 Tommen-Baratheon 1
66 Varys 1
65 Pycelle 1
57 Vayon-Poole 1
75 Thoros-of-Myr 1
74 Loras-Tyrell 1
49 Petyr-Baelish 1
73 Gregor-Clegane 1
72 Beric-Dondarrion 1
16 Gerold-Hightower 2
116 Clement-Piper 2
154 Varly 2
153 Tregar 2
5 Eddard-Stark 2
152 Tobho-Mott 2
148 Joss 2
136 Torrhen-Karstark 2
161 Oswell-Whent 2
151 Raymun-Darry 2
15 Brandon-Stark 2
144 Gendry 2
145 High-Septon-(fat_one) 2
150 Porther 2
146 Howland-Reed 2
147 Jacks 2
149 Marq-Piper 2
135 Daryn-Hornwood 2
117 Karyl-Vance 2
156 Wylla 2
60 Balon-Greyjoy 2
3 Aegon-I-Targaryen 2
41 Desmond 2
14 Aerys-II-Targaryen 2
84 Rickard-Stark 2
86 Lyanna-Stark 2
112 Cayn 2
35 Arthur-Dayne 2
34 Tomard 2
32 Harwin 2
31 Alyn 2
160 Will-(prologue) 3
159 Gared 3
13 Samwell-Tarly 3
142 Dywen 3
67 Jaremy-Rykker 3
37 Benjen-Stark 3
69 Mance-Rayder 3
71 Waymar-Royce 3
184 Randyll-Tarly 3
138 Donal-Noye 3
58 Yoren 3
28 Grenn 3
25 Albett 3
6 Aemon-Targaryen-(Maester-Aemon) 3
7 Alliser-Thorne 3
8 Bowen-Marsh 3
171 Othor 3
170 Matthar 3
29 Pypar 3
9 Chett 3
26 Halder 3
166 Hobb 3
10 Clydas 3
11 Jeor-Mormont 3
12 Jon-Snow 3
162 Todder 3
134 Dareon 3
168 Jafer-Flowers 3
27 Rast 3
4 Daenerys-Targaryen 4
119 Haggo 4
141 Rhaego 4
24 Rakharo 4
23 Quaro 4
22 Jorah-Mormont 4
21 Jhogo 4
20 Drogo 4
19 Aggo 4
118 Cohollo 4
120 Qotho 4
157 Fogo 4
124 Eroeh 4
125 Illyrio-Mopatis 4
123 Doreah 4
127 Jhiqui 4
128 Mirri-Maz-Duur 4
129 Rhaegar-Targaryen 4
130 Viserys-Targaryen 4
139 Jommo 4
140 Ogo 4
126 Irri 4
122 Coratt 5
121 Conn 5
137 Dolf 5
185 Timett 5
91 Shagga 5
186 Ulf-son-of-Umar 5
131 Danwell-Frey 6
132 Hosteen-Frey 6
133 Jared-Frey 6
In [56]:
# Visualization: plot all characters with their modularity class indicated by a star in a seaborn stripplot
!pip install seaborn
import seaborn as sns
plt.figure(figsize=(4,35))

ax =sns.stripplot(x='modularity_class_b1', y='node', data=communities_df,
              hue='modularity_class_b1', marker='*',size=15)

ax.legend(loc='upper right',bbox_to_anchor=(1.5, 1), title='Modularity Class')
ax.set_title("Characters By Modularity Class")
plt.show()
Requirement already satisfied: seaborn in c:\users\colto\anaconda3\lib\site-packages (0.11.2)
Requirement already satisfied: scipy>=1.0 in c:\users\colto\anaconda3\lib\site-packages (from seaborn) (1.7.3)
Requirement already satisfied: matplotlib>=2.2 in c:\users\colto\anaconda3\lib\site-packages (from seaborn) (3.5.1)
Requirement already satisfied: numpy>=1.15 in c:\users\colto\anaconda3\lib\site-packages (from seaborn) (1.21.5)
Requirement already satisfied: pandas>=0.23 in c:\users\colto\anaconda3\lib\site-packages (from seaborn) (1.4.2)
Requirement already satisfied: pillow>=6.2.0 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (9.0.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (4.25.0)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (2.8.2)
Requirement already satisfied: packaging>=20.0 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (21.3)
Requirement already satisfied: cycler>=0.10 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (0.11.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (1.3.2)
Requirement already satisfied: pyparsing>=2.2.1 in c:\users\colto\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (3.0.4)
Requirement already satisfied: pytz>=2020.1 in c:\users\colto\anaconda3\lib\site-packages (from pandas>=0.23->seaborn) (2021.3)
Requirement already satisfied: six>=1.5 in c:\users\colto\anaconda3\lib\site-packages (from python-dateutil>=2.7->matplotlib>=2.2->seaborn) (1.16.0)

Task 4¶

In [147]:
# Use the network data for the second book of A Song of Ice and Fire, and duplicate the process for creating the modularity
# of the first book and merge the results with the results of the first book
# First, you should create a network and use community.greedy_modularity_communities to generate the communities
# Then make a dictionary by looping through the communities and, for each member of the community, adding their community number
# Next, add modularity class to the network as an attribute and make a Pandas dataframe from modularity class data
# (remember to change column names to distinguish the modularity class of Book 2 and Book 1)
# Finally, merge the newly created dataframe for Book 2 and the communities_df for Book 1, and display the merged dataframe

from networkx.algorithms import community

asoiaf_b2_df = pd.read_csv('Data/asoiaf-book2-edges.csv')
G_b2 = nx.from_pandas_edgelist(asoiaf_b2_df, 'Source', 'Target', 'weight')

communities_b2 = community.greedy_modularity_communities(G_b2)

modularity_class_2 = {}
count = 0
for community in communities_b2:
    for name in community:
        modularity_class_2[name] = count
    count += 1

Book2 = pd.DataFrame(list(modularity_class_2.items()), columns=['Character', 'Modularity Class Book 2'])

Book1 = pd.read_csv('Data/asoiaf-book1-edges.csv')
Book1.rename(columns={'book': 'Modularity Class Book 1'}, inplace=True)
Book1.rename(columns={'Source': 'Character'}, inplace=True)

merged_df = pd.merge(Book1, Book2, on='Character', how='outer')


print(merged_df.head(150))
                           Character              Target        Type  weight  Modularity Class Book 1  Modularity Class Book 2
0                     Addam-Marbrand     Jaime-Lannister  Undirected     3.0                      1.0                      NaN
1                     Addam-Marbrand     Tywin-Lannister  Undirected     6.0                      1.0                      NaN
2                  Aegon-I-Targaryen  Daenerys-Targaryen  Undirected     5.0                      1.0                      6.0
3                  Aegon-I-Targaryen        Eddard-Stark  Undirected     4.0                      1.0                      6.0
4    Aemon-Targaryen-(Maester-Aemon)      Alliser-Thorne  Undirected     4.0                      1.0                      3.0
5    Aemon-Targaryen-(Maester-Aemon)         Bowen-Marsh  Undirected     4.0                      1.0                      3.0
6    Aemon-Targaryen-(Maester-Aemon)               Chett  Undirected     9.0                      1.0                      3.0
7    Aemon-Targaryen-(Maester-Aemon)              Clydas  Undirected     5.0                      1.0                      3.0
8    Aemon-Targaryen-(Maester-Aemon)        Jeor-Mormont  Undirected    13.0                      1.0                      3.0
9    Aemon-Targaryen-(Maester-Aemon)            Jon-Snow  Undirected    34.0                      1.0                      3.0
10   Aemon-Targaryen-(Maester-Aemon)       Samwell-Tarly  Undirected     5.0                      1.0                      3.0
11                Aerys-II-Targaryen       Brandon-Stark  Undirected     4.0                      1.0                      0.0
..                               ...                 ...         ...     ...                      ...                      ...
138                       Bran-Stark                Stiv  Undirected     4.0                      1.0                      1.0
139                       Bran-Stark       Theon-Greyjoy  Undirected    23.0                      1.0                      1.0
140                       Bran-Stark    Tommen-Baratheon  Undirected     5.0                      1.0                      1.0
141                       Bran-Stark    Tyrion-Lannister  Undirected    31.0                      1.0                      1.0
142                       Bran-Stark               Yoren  Undirected     3.0                      1.0                      1.0
143                    Brandon-Stark        Eddard-Stark  Undirected    11.0                      1.0                      NaN
144                    Brandon-Stark        Lyanna-Stark  Undirected     4.0                      1.0                      NaN
145                    Brandon-Stark       Petyr-Baelish  Undirected    14.0                      1.0                      NaN
146                    Brandon-Stark       Rickard-Stark  Undirected     3.0                      1.0                      NaN
147                    Brandon-Stark    Robert-Baratheon  Undirected     5.0                      1.0                      NaN
148                            Bronn       Catelyn-Stark  Undirected     6.0                      1.0                      0.0
149                            Bronn             Chiggen  Undirected     8.0                      1.0                      0.0

[150 rows x 6 columns]

Task 5¶

Discuss the results of task 4 in the next cell. Compare the modularity classes between Book 1 and Book 2. It's important to note that the actual number of modularity classes itself may not hold significant meaning. For instance, if characters A and B are the only members of modularity class 0 in Book 1 and they are also the only members of modularity class 1 in Book 2, there is no substantive difference despite the differing class numbers.
You should discuss at least two pairs of characters. The first pair should belong to the same modularity class in both books, while the second pair should belong to the same modularity class in one book but belong to different modularity classes in the other (e.g., character A and B both belong to modularity class 0 in Book 1 and modularity class 1 in Book 2; whereas character C and D both belong to modularity class 2 in Book 1, but in Book 2, C is in modularity class 2 while D is in modularity class 3). If you are familiar with A Song of Ice and Fire (or the television series Game of Thrones), you can attempt to interpret these results by exploring how the consistency or inconsistency in characters' modularity classes relates to the plot. If you haven't, you can just speculate on potential reasons for the consistency or inconsistency.

In task 4 an example of a pair of individuals that had a change in modularity between books would be Aegon-I-Targaryen and Daenerys_Targaryan going from a 1 -> 6 between books. This may be explained by individuals from their families or individuals they both know being mentioned and introduced in the second book strengthening their modularity. An example of modularity staying the same between books would be Bran Stark and Stiv. This could be from the characters each one of these individuals know related to the plot hasn't changed much. To the point it hasn't effected their modularity class.