Negative network:
In [10]:
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.max_rows = 20
In [2]:
# read multigraph M
M = nx.read_gml('../output/network/u_Gc_negative2.gml')
# convert multigraph M to simple graph G (remove parallel edges)
G = nx.Graph()
for u,v,data in M.edges_iter(data=True):
w = data['weight'] if 'weight' in data else 1.0
if G.has_edge(u,v):
G[u][v]['weight'] += w
else:
G.add_edge(u, v, weight=w)
#print G.edges(data=True)
print nx.info(M), '\n'
print nx.info(G)
In [3]:
## jaccard coefficient
jc = nx.jaccard_coefficient(G)
jc = list(jc)
for u, v, p in jc:
'(%s, %s) -> %.4f' % (u, v, p)
In [11]:
df = pd.DataFrame(jc, columns=['u', 'v', 'jaccard'])
subset_df = df.ix[df['jaccard'] != 0, :]
subset_df.sort_values('jaccard', ascending=False)
Out[11]:
In [5]:
# save jaccard
#subset_df.to_csv('jaccard_negative.csv')
In [6]:
## adamic adar index
aa = nx.adamic_adar_index(G)
aa = list(aa)
for u, v, p in aa:
'(%s, %s) -> %.8f' % (u, v, p)
In [12]:
df = pd.DataFrame(aa, columns=['u', 'v', 'adamic_adar'])
subset_df = df.ix[df['adamic_adar'] != 0, :]
subset_df.sort_values('adamic_adar', ascending=False)
Out[12]:
In [8]:
# save adamic idar
#subset_df.to_csv('adamic_negative.csv')
In [13]:
# closeness vitality
# of a node is the change in the sum of distances between all node pairs when excluding that node
cv = nx.closeness_vitality(M)
cv_df = pd.DataFrame.from_dict(cv, orient = 'index')
cv_df.columns = ['closeness vitality']
cv_df.sort_values(by = ['closeness vitality'], ascending = False)
Out[13]:
In [15]:
# save closeness vitality
#cv_df.to_csv('out/cv_negative.csv')
In [14]:
# link analysis: page rank
# PageRank computes a ranking of nodes based on structure of incoming links
pr = nx.pagerank_numpy(M)
pr_df = pd.DataFrame.from_dict(pr, orient = 'index')
pr_df.columns = ['page rank']
pr_df.sort_values(by = ['page rank'], ascending = False)
Out[14]:
In [16]:
# save page rank
#pr_df.to_csv('out/pr_negative.csv')
In [ ]: