本文整理汇总了Python中networkx.closeness_centrality方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.closeness_centrality方法的具体用法?Python networkx.closeness_centrality怎么用?Python networkx.closeness_centrality使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.closeness_centrality方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _calc_graph_func
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def _calc_graph_func(p):
con, times_chunk, graph_func = p
vals = []
now = time.time()
for run, t in enumerate(times_chunk):
utils.time_to_go(now, run, len(times_chunk), 10)
con_t = con[:, :, t]
g = nx.from_numpy_matrix(con_t)
if graph_func == 'closeness_centrality':
x = nx.closeness_centrality(g)
elif graph_func == 'degree_centrality':
x = nx.degree_centrality(g)
elif graph_func == 'eigenvector_centrality':
x = nx.eigenvector_centrality(g, max_iter=10000)
elif graph_func == 'katz_centrality':
x = nx.katz_centrality(g, max_iter=100000)
else:
raise Exception('Wrong graph func!')
vals.append([x[k] for k in range(len(x))])
vals = np.array(vals)
return vals, times_chunk
示例2: test_florentine_families_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_florentine_families_closeness(self):
c=nx.closeness_centrality(self.F)
d={'Acciaiuoli': 0.368,
'Albizzi': 0.483,
'Barbadori': 0.4375,
'Bischeri': 0.400,
'Castellani': 0.389,
'Ginori': 0.333,
'Guadagni': 0.467,
'Lamberteschi': 0.326,
'Medici': 0.560,
'Pazzi': 0.286,
'Peruzzi': 0.368,
'Ridolfi': 0.500,
'Salviati': 0.389,
'Strozzi': 0.4375,
'Tornabuoni': 0.483}
for n in sorted(self.F):
assert_almost_equal(c[n],d[n],places=3)
示例3: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def __init__(self, method='degree', analyzer=NltkNormalizer().split_and_normalize):
self.analyze = analyzer
self.method = method
self.methods_on_digraph = {'hits', 'pagerank', 'katz'}
self._get_scores = {'degree': nx.degree, 'betweenness': nx.betweenness_centrality,
'pagerank': nx.pagerank_scipy, 'hits': self._hits, 'closeness': nx.closeness_centrality,
'katz': nx.katz_centrality}[method]
# Add a new value when a new vocabulary item is seen
self.vocabulary = defaultdict()
self.vocabulary.default_factory = self.vocabulary.__len__
示例4: test_k5_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_k5_closeness(self):
c=nx.closeness_centrality(self.K5)
d={0: 1.000,
1: 1.000,
2: 1.000,
3: 1.000,
4: 1.000}
for n in sorted(self.K5):
assert_almost_equal(c[n],d[n],places=3)
示例5: test_p3_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_p3_closeness(self):
c=nx.closeness_centrality(self.P3)
d={0: 0.667,
1: 1.000,
2: 0.667}
for n in sorted(self.P3):
assert_almost_equal(c[n],d[n],places=3)
示例6: test_krackhardt_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_krackhardt_closeness(self):
c=nx.closeness_centrality(self.K)
d={0: 0.529,
1: 0.529,
2: 0.500,
3: 0.600,
4: 0.500,
5: 0.643,
6: 0.643,
7: 0.600,
8: 0.429,
9: 0.310}
for n in sorted(self.K):
assert_almost_equal(c[n],d[n],places=3)
示例7: test_weighted_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_weighted_closeness(self):
XG=nx.Graph()
XG.add_weighted_edges_from([('s','u',10), ('s','x',5), ('u','v',1),
('u','x',2), ('v','y',1), ('x','u',3),
('x','v',5), ('x','y',2), ('y','s',7),
('y','v',6)])
c=nx.closeness_centrality(XG,distance='weight')
d={'y': 0.200,
'x': 0.286,
's': 0.138,
'u': 0.235,
'v': 0.200}
for n in sorted(XG):
assert_almost_equal(c[n],d[n],places=3)
示例8: calc_closeness_centrality
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def calc_closeness_centrality(p):
con, times_chunk = p
vals = []
now = time.time()
for run, t in enumerate(times_chunk):
utils.time_to_go(now, run, len(times_chunk), 10)
con_t = con[:, :, t]
g = nx.from_numpy_matrix(con_t)
# x = nx.closeness_centrality(g)
x = nx.degree_centrality(g)
vals.append([x[k] for k in range(len(x))])
vals = np.array(vals)
return vals, times_chunk
示例9: test_wf_improved
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_wf_improved(self):
G = nx.union(self.P4, nx.path_graph([4, 5, 6]))
c = nx.closeness_centrality(G)
cwf = nx.closeness_centrality(G, wf_improved=False)
res = {0: 0.25, 1: 0.375, 2: 0.375, 3: 0.25,
4: 0.222, 5: 0.333, 6: 0.222}
wf_res = {0: 0.5, 1: 0.75, 2: 0.75, 3: 0.5,
4: 0.667, 5: 1.0, 6: 0.667}
for n in G:
assert_almost_equal(c[n], res[n], places=3)
assert_almost_equal(cwf[n], wf_res[n], places=3)
示例10: test_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_digraph(self):
G = nx.path_graph(3, create_using=nx.DiGraph())
c = nx.closeness_centrality(G)
cr = nx.closeness_centrality(G.reverse())
d = {0: 0.0, 1: 0.500, 2: 0.667}
dr = {0: 0.667, 1: 0.500, 2: 0.0}
for n in sorted(self.P3):
assert_almost_equal(c[n], d[n], places=3)
assert_almost_equal(cr[n], dr[n], places=3)
示例11: test_k5_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_k5_closeness(self):
c = nx.closeness_centrality(self.K5)
d = {0: 1.000,
1: 1.000,
2: 1.000,
3: 1.000,
4: 1.000}
for n in sorted(self.K5):
assert_almost_equal(c[n], d[n], places=3)
示例12: test_p3_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_p3_closeness(self):
c = nx.closeness_centrality(self.P3)
d = {0: 0.667,
1: 1.000,
2: 0.667}
for n in sorted(self.P3):
assert_almost_equal(c[n], d[n], places=3)
示例13: test_krackhardt_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_krackhardt_closeness(self):
c = nx.closeness_centrality(self.K)
d = {0: 0.529,
1: 0.529,
2: 0.500,
3: 0.600,
4: 0.500,
5: 0.643,
6: 0.643,
7: 0.600,
8: 0.429,
9: 0.310}
for n in sorted(self.K):
assert_almost_equal(c[n], d[n], places=3)
示例14: test_weighted_closeness
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_weighted_closeness(self):
edges = ([('s', 'u', 10), ('s', 'x', 5), ('u', 'v', 1),
('u', 'x', 2), ('v', 'y', 1), ('x', 'u', 3),
('x', 'v', 5), ('x', 'y', 2), ('y', 's', 7), ('y', 'v', 6)])
XG = nx.Graph()
XG.add_weighted_edges_from(edges)
c = nx.closeness_centrality(XG, distance='weight')
d = {'y': 0.200,
'x': 0.286,
's': 0.138,
'u': 0.235,
'v': 0.200}
for n in sorted(XG):
assert_almost_equal(c[n], d[n], places=3)
示例15: test_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import closeness_centrality [as 别名]
def test_digraph(self):
G = nx.path_graph(3, create_using=nx.DiGraph())
c = nx.closeness_centrality(G)
cr = nx.closeness_centrality(G, reverse=True)
d = {0: 0.0, 1: 0.500, 2: 0.667}
dr = {0: 0.667, 1: 0.500, 2: 0.0}
for n in sorted(self.P3):
assert_almost_equal(c[n], d[n], places=3)
assert_almost_equal(cr[n], dr[n], places=3)