-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFINAL_BOOK.py
More file actions
169 lines (124 loc) · 5.15 KB
/
FINAL_BOOK.py
File metadata and controls
169 lines (124 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 7:11:46 2020
@author: prituldave
"""
from nxviz import MatrixPlot,ArcPlot,CircosPlot
from hiveplot import HivePlot
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
def ecdf(data):
return np.sort(data),np.arange(1,len(data)+1)/len(data)
for i in range(1,6):
df = pd.read_csv("/Users/prituldave/projects/networkx/GOT/book"+str(i)+".csv")
print(df.head())
G = nx.Graph()
'''G.add_nodes_from(df['Source'])
G.add_nodes_from(df['Target'])'''
for s,t,w in zip(df['Source'],df['Target'],df['weight']):
G.add_edge(s,t,weight=w)
print(G.nodes(data=True))
#pos = nx.spring_layout(G)
plt.axis('equal')
nx.draw(G,with_labels=True,font_size=5,node_size=8);plt.show()
#nx.draw_networkx_edge_labels(G,pos)
m = MatrixPlot(G)
m.draw();plt.show()
a = ArcPlot(G)
a.draw();plt.show()
c = CircosPlot(G)
c.draw()
#Top 5 import persons without weights#
persons_list = []
for p in G.nodes:
persons_list.append([p,len(list(G.neighbors(p)))])
vvip_list = sorted(persons_list, key = lambda x: x[1],reverse=True)
print("\n Top5 interactions in Book without weights \n"+str(i),vvip_list[:5])
#Visualizing degree of centrality#
fig = plt.figure(i+6)
neighbors = [len(list(G.neighbors(node))) for node in G.nodes()]
x, y = ecdf(neighbors)
plt.scatter(x, y)
plt.title('\nNumber of Neighbors');plt.show()
#Betweeness centrality#
print("\nBetweeness centraility without weights",sorted(nx.betweenness_centrality(G).items(), key=lambda x:x[1], reverse=True)[0:5])
#Top 5 important persons with weights#
#sorted(weighted_degree(G, 'weight').items(), key=lambda x:x[1], reverse=True)[0:10]
l = list(G.degree(weight='weight'))
vvip_list = sorted(l, key = lambda x: x[1],reverse=True)
print("\n Top5 interactions in Book with weight\n"+str(i),vvip_list[:5])
#Betweeness centraility#
print("\nBetweeness centraility with weights",sorted(nx.betweenness_centrality(G,weight='weight').items(), key=lambda x:x[1], reverse=True)[0:5])
'''
Conclusion:-
Eddard Stark is highly important person in book1
Information passing is highly done by Robert-Baratheon in book1
Tyrion-Lannister is highly important person in book2 and book3
Information passing is highly done by Jaime-Lannister in book2
Information passing is highly done by Joffrey-Baratheon in book3
Jaime-Lannister and Cersei-Lannister is highly important person in book4
Information passing is highly done by Stannis-Baratheon in book4 and book5
Jon-Snow is highly important person in book5
'''
#Evolution of character importance#
edd_stark=[]
Tyrion_Lannister=[]
Jaime_Lannister=[]
Jon_Snow=[]
for i in range(1,6):
df = pd.read_csv("/Users/prituldave/projects/networkx/GOT/book"+str(i)+".csv")
G = nx.Graph()
for s,t,w in zip(df['Source'],df['Target'],df['weight']):
G.add_edge(s,t,weight=w)
edd_stark.append(nx.degree_centrality(G).get('Eddard-Stark'))
Tyrion_Lannister.append(nx.degree_centrality(G).get('Tyrion-Lannister'))
Jaime_Lannister.append(nx.degree_centrality(G).get('Jaime-Lannister'))
Jon_Snow.append(nx.degree_centrality(G).get('Jon-Snow'))
plt.plot(edd_stark,label='Eddard-Stark')
plt.plot(Tyrion_Lannister,label='Tyrion-Lannister')
plt.plot(Jaime_Lannister,label='Jaime-Lannister')
plt.plot(Jon_Snow,label='Jon-Snow')
plt.legend(loc='best')
plt.show()
'''
Conclusion:-
Interaction of Jon snow increase by book 5
Interaction of Eddard-Stark completely decrease by book5 which indicating he dies off as book progress
'''
#Whats up with Stannis Baratheon#
for i in range(1,6):
df = pd.read_csv("/Users/prituldave/projects/networkx/GOT/book"+str(i)+".csv")
G = nx.Graph()
for s,t,w in zip(df['Source'],df['Target'],df['weight']):
G.add_edge(s,t,weight=w)
print(sorted(nx.betweenness_centrality(G,weight='weight').items(), key=lambda x:x[1], reverse=True)[0:5])
'''
Conclusion:-
Betweeness centrality of Stannis-Baratheon is high in Book4 and Book5.
It means he has done maximum interaction in Book4 and Book5
'''
#Page Rank#
temp = []
for i in range(1,6):
df = pd.read_csv("/Users/prituldave/projects/networkx/GOT/book"+str(i)+".csv")
G = nx.Graph()
for s,t,w in zip(df['Source'],df['Target'],df['weight']):
G.add_edge(s,t,weight=w)
print("\n Page rank for book ",i)
print(sorted(nx.pagerank_numpy(G, weight=None).items(), key=lambda x:x[1], reverse=True)[0:10])
#Correlation:- Combining all and analyzing#
for i in range(1,6):
df = pd.read_csv("/Users/prituldave/projects/networkx/GOT/book"+str(i)+".csv")
G = nx.Graph()
for s,t,w in zip(df['Source'],df['Target'],df['weight']):
G.add_edge(s,t,weight=w)
measures = [nx.pagerank(G),nx.betweenness_centrality(G, weight='weight'), nx.degree_centrality(G)]
cor = pd.DataFrame.from_records(measures)
#Final Conclusion#
'''
Eddard Stark is the most important person as it die by book5
because degree of centrality is high in Eddard Stark
'''