In [2]:
import networkx
import numpy
from matplotlib import pyplot
import pandas
In [46]:
got_s1 = pandas.read_csv("got-s1-edges.csv")
got_s1 = got_s1[ got_s1["Weight"] > 20 ]
got_s1.head()
Out[46]:
Source Target Weight Season
0 NED ROBERT 192 1
1 DAENERYS JORAH 154 1
2 JON SAM 121 1
3 LITTLEFINGER NED 107 1
4 NED VARYS 96 1
In [47]:
got_s1_network = networkx.convert_matrix.from_pandas_edgelist(got_s1, "Source", "Target")
In [8]:
got_s1_network.is_directed()
Out[8]:
False
In [78]:
networkx.degree_histogram(got_s1_network)
Out[78]:
[0, 22, 10, 4, 3, 3, 5, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1]
In [79]:
degrees = numpy.array(networkx.degree_histogram(got_s1_network))
print(degrees.mean(), degrees.var())
pyplot.hist(degrees, bins=range(20))
pyplot.show()
3.0588235294117645 28.761245674740483
In [76]:
er_graph = networkx.erdos_renyi_graph(190, 0.05)
degrees = numpy.array([degree for node, degree in er_graph.degree()])
print(degrees.mean(), degrees.var())
pyplot.hist(degrees, bins=range(20))
pyplot.show()
9.25263157894737 8.557229916897507
In [75]:
small_graph = networkx.erdos_renyi_graph(10, 0.15)
networkx.draw(small_graph, with_labels=True)
pyplot.show()
networkx.to_numpy_array(small_graph)
Out[75]:
array([[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 1., 0., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 1., 0., 1., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 1.],
       [0., 1., 1., 0., 0., 0., 0., 0., 1., 0.]])
In [77]:
networkx.algorithms.betweenness_centrality(small_graph)
Out[77]:
{0: 0.0,
 1: 0.6388888888888888,
 2: 0.0,
 3: 0.0,
 4: 0.2222222222222222,
 5: 0.41666666666666663,
 6: 0.0,
 7: 0.0,
 8: 0.38888888888888884,
 9: 0.6388888888888888}
In [48]:
list(networkx.connected_components(got_s1_network))
Out[48]:
[{'ARYA',
  'BARRISTAN',
  'BENJEN',
  'BRAN',
  'BRONN',
  'CATELYN',
  'CERSEI',
  'GREATJON_UMBER',
  'GRENN',
  'JAIME',
  'JEOR',
  'JOFFREY',
  'JON',
  'JORY_CASSEL',
  'LITTLEFINGER',
  'LORAS',
  'LYSA',
  'MAESTER_AEMON',
  'MAESTER_LUWIN',
  'MORD',
  'NED',
  'OSHA',
  'PYCELLE',
  'PYP',
  'RENLY',
  'ROBB',
  'ROBERT',
  'ROBIN_ARRYN',
  'RODRIK_CASSEL',
  'ROS',
  'SAM',
  'SANSA',
  'SEPTA_MORDANE',
  'SHAE',
  'SHAGGA',
  'SYRIO_FOREL',
  'THEON',
  'TYRION',
  'TYWIN',
  'VARYS',
  'WALDER',
  'YOREN'},
 {'DAENERYS',
  'DOREAH',
  'DROGO',
  'ILLYRIO',
  'IRRI',
  'JORAH',
  'MIRRI_MAZ_DUUR',
  'QOTHO',
  'RAKHARO',
  'VISERYS'}]
In [45]:
networkx.shortest_path(got_s1_network, "SYRIO_FOREL", "BENJEN")
Out[45]:
['SYRIO_FOREL', 'ARYA', 'NED', 'BENJEN']
In [12]:
networkx.algorithms.centrality.eigenvector_centrality(got_s1_network)
Out[12]:
{'NED': 0.31498447895647114,
 'ROBERT': 0.24812123326232996,
 'DAENERYS': 0.07371709508806808,
 'JORAH': 0.042841550277667555,
 'JON': 0.1682492867126591,
 'SAM': 0.02760977289176597,
 'LITTLEFINGER': 0.2046629857551136,
 'VARYS': 0.17613900521317602,
 'DROGO': 0.025853812077304944,
 'ARYA': 0.20713039957330018,
 'CATELYN': 0.2374853669378773,
 'ROBB': 0.20707831129215076,
 'BRONN': 0.05346305577454019,
 'TYRION': 0.22972317989624752,
 'CERSEI': 0.2391948327808419,
 'SHAE': 0.022537259350262,
 'JOFFREY': 0.21947138769356012,
 'SANSA': 0.20258680392370262,
 'PYCELLE': 0.14385857482735104,
 'VISERYS': 0.02463241971074581,
 'SEPTA_MORDANE': 0.06621675759241473,
 'TYWIN': 0.1379288688100952,
 'JEOR': 0.09508865706845071,
 'THEON': 0.11738244917233683,
 'BRAN': 0.14944325869899366,
 'MAESTER_LUWIN': 0.09859108953759023,
 'JAIME': 0.19839992334776915,
 'LORAS': 0.07225792209147677,
 'RENLY': 0.13582506891244311,
 'MIRRI_MAZ_DUUR': 0.008664380155954938,
 'SYRIO_FOREL': 0.03236609884036915,
 'JORY_CASSEL': 0.07353590741387099,
 'PYP': 0.05625901664235569,
 'ROS': 0.0997873104273685,
 'GRENN': 0.03518365150427437,
 'WALDER': 0.03647705540471962,
 'DOREAH': 0.010186417042174442,
 'BENJEN': 0.10098044788554295,
 'IRRI': 0.010334131096659718,
 'GREATJON_UMBER': 0.08641441718477576,
 'MORD': 0.03933859958504836,
 'LYSA': 0.0393267606761192,
 'RAKHARO': 0.01081477729662621,
 'ROBIN_ARRYN': 0.0393267606761192,
 'OSHA': 0.03386917956269805,
 'RODRIK_CASSEL': 0.10154558021725314,
 'QOTHO': 0.008664380155954938,
 'MAESTER_AEMON': 0.03865786234320893,
 'BARRISTAN': 0.10837891255474891,
 'ILLYRIO': 0.029451481015388956,
 'SHAGGA': 0.024452846674840295,
 'YOREN': 0.14693833207255952,
 'ALLISER_THORNE': 0.02525266425944283,
 'WINE_MERCHANT': 0.007362131258890152,
 'HOUND': 0.17235860845959394,
 'LANCEL': 0.05600575134497693,
 'RICKON': 0.020892828947391195,
 'MARILLION': 0.05733300988329415,
 'KEVAN': 0.03579299097199367,
 'RHAEGO': 0.00894011251410292,
 'JANOS_SLYNT': 0.052203989364818376,
 'GENDRY': 0.04703077868780298,
 'HOT_PIE': 0.014235891533212038,
 'LOMMY_GREENHANDS': 0.011846822728373622,
 'MERYN_TRANT': 0.08266722018661106,
 'JON_ARRYN': 0.10131518668888438,
 'HODOR': 0.03946931249458833,
 'STANNIS': 0.08925405012433499,
 'VARDIS_EGEN': 0.012709646532337712,
 'MAGO': 0.005328933272053133,
 'MYCAH': 0.050529222950586095,
 'TOBHO_MOTT': 0.01937399608506775,
 'RAST': 0.030353081971419102,
 'OLD_NAN': 0.025362309672493897,
 'MHAEGEN': 0.031745349712321395,
 'AERYS': 0.08847019375229728,
 'BAELOR': 0.09342920242883482,
 'DAREON': 0.02485656367065147,
 'SER_VARDIS': 0.03418011985618143,
 'STABLE_BOY': 0.011084972434160517,
 'HUGH_OF_THE_VALE': 0.05163675362060137,
 'OTHELL_YARWYCK': 0.02042630963144132,
 'OTHOR': 0.018582572563304624,
 'MOUNTAIN': 0.06778489109197917,
 'TOMARD': 0.028902516637858706,
 'GARED': 0.002960310655048671,
 'ROYCE': 0.002960310655048671,
 'JONOS_BRACKEN': 0.011957163569349751,
 'MASHA_HEDDLE': 0.025004022222853466,
 'RICKARD_STARK': 0.03480154649928482,
 'ILYN_PAYNE': 0.09926836989392746,
 'WILL': 0.052353853032359884,
 'LYANNA': 0.04293682398469464,
 'RYGER_RIVERS': 0.0019521832855265192,
 'STEVRON_FREY': 0.014661829817864232,
 'WAYMAR_ROYCE': 0.002801877721997989,
 'ADDAM_MARBRAND': 0.010086580865166563,
 'LEO_LEFFORD': 0.014749778992445077,
 'RHAEGAR': 0.01801343034988839,
 'ASSASSIN': 0.031789848348150226,
 'BALON': 0.01857648354719713,
 'BERIC': 0.030135834107332553,
 'BRANDON_STARK': 0.04843161660804866,
 'HOSTER': 0.014661829817864232,
 'COHOLLO': 0.0013837176309660516,
 'JHIQUI': 0.005043557132778068,
 'MYRCELLA': 0.012294375690515754,
 'AEGON': 0.006803720312012545,
 'BORCAS': 0.004341555614830151,
 'BOWEN_MARSH': 0.004341555614830151,
 'LUKE': 0.004341555614830151,
 'IROGENIA': 0.0005452166350282167,
 'GALBART_GLOVER': 0.016346918194711207,
 'RICKARD_KARSTARK': 0.021989106056193898,
 'HIGH_SEPTON': 0.016857074748548917,
 'TYSHA': 0.02291220371355869,
 'JAREMY_RYKKER': 0.00919351901146345,
 'JOANNA': 0.012294375690515754,
 'TOMMEN': 0.02502419771981764,
 'LITTLE_BIRD': 0.002292844784862147,
 'JOYEUSE': 0.0019521832855265192,
 'MACE': 0.0038669729233149416,
 'MELESSA': 0.0014778353757368187,
 'ROOSE_BOLTON': 0.016857074748548917,
 'STEFFON': 0.016857074748548917,
 'VARLY': 0.016857074748548917,
 'RANDYLL': 0.0014778353757368187}
In [54]:
pyplot.figure(figsize=(7,7))
networkx.draw(got_s1_network, with_labels=True)
pyplot.show()