In [72]:
import networkx as nx

Simple networkx usage

In [81]:
g = nx.read_edgelist("/Users/cazabetremy/Documents/GitHub/dynetx/dynetx/test/GOT/GoT_S01E01_000.edges")
In [82]:
print(list(g.nodes()))
['Gared', 'Waymar_Royce', 'Will', 'Cersei_Lannister', 'Jaime_Lannister', 'Theon_Greyjoy', 'Robb_Stark', 'Jon_Snow', 'Eddard_Stark', 'Brandon_Stark', 'Sansa_Stark', 'Mordane', 'Rodrik_Cassel', 'Catelyn_Stark']
In [83]:
print("degree of Jon: ",g.degree("Jon_Snow"))

print("neighb of Jon: ",g["Jon_Snow"])
degree of Jon:  4
neighb of Jon:  {'Theon_Greyjoy': {}, 'Robb_Stark': {}, 'Eddard_Stark': {}, 'Brandon_Stark': {}}
In [84]:
print("density: ",nx.density(g))
from networkx.algorithms.approximation import average_clustering
print("avg_clustering: ",average_clustering(g))
density:  0.17582417582417584
avg_clustering:  0.509

Example of community detection with networkx

In [85]:
from networkx.algorithms import community
connectedComp = list(nx.connected_components(g))
comsKcliques = list(community.k_clique_communities(g,k=3))
comsLabelPropa = list(community.asyn_lpa_communities(g))


print(len(connectedComp))
print(len(comsKcliques))
print(len(comsLabelPropa))
4
2
4
In [86]:
for c in comsKcliques:
    print(c)
    print("---")
frozenset({'Jon_Snow', 'Brandon_Stark', 'Theon_Greyjoy', 'Rodrik_Cassel', 'Eddard_Stark', 'Robb_Stark'})
---
frozenset({'Will', 'Waymar_Royce', 'Gared'})
---
In [87]:
for c in list(comsLabelPropa):
    print(c)
    print("---")
{'Will', 'Waymar_Royce', 'Gared'}
---
{'Cersei_Lannister', 'Jaime_Lannister'}
---
{'Brandon_Stark', 'Rodrik_Cassel', 'Robb_Stark', 'Jon_Snow', 'Catelyn_Stark', 'Theon_Greyjoy', 'Eddard_Stark'}
---
{'Sansa_Stark', 'Mordane'}
---
In [88]:
print("modularity CC",community.modularity(g,connectedComp))
print("modularity LP",community.modularity(g,comsLabelPropa))
modularity CC 0.484375
modularity LP 0.484375
In [ ]: