本文整理汇总了Python中networkx.degree_histogram方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.degree_histogram方法的具体用法?Python networkx.degree_histogram怎么用?Python networkx.degree_histogram使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.degree_histogram方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def decode_graph(adj, prefix):
adj = np.asmatrix(adj)
G = nx.from_numpy_matrix(adj)
# G.remove_nodes_from(nx.isolates(G))
print('num of nodes: {}'.format(G.number_of_nodes()))
print('num of edges: {}'.format(G.number_of_edges()))
G_deg = nx.degree_histogram(G)
G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
if nx.is_connected(G):
print('average path length: {}'.format(nx.average_shortest_path_length(G)))
print('average diameter: {}'.format(nx.diameter(G)))
G_cluster = sorted(list(nx.clustering(G).values()))
print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
cycle_len = []
cycle_all = nx.cycle_basis(G, 0)
for item in cycle_all:
cycle_len.append(len(item))
print('cycles', cycle_len)
print('cycle count', len(cycle_len))
draw_graph(G, prefix=prefix)
示例2: _compute_rc
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def _compute_rc(G):
# compute rich club coefficient for all k degrees in G
deghist = nx.degree_histogram(G)
total = sum(deghist)
# number of nodes with degree > k (omit last entry which is zero)
nks = [total-cs for cs in nx.utils.accumulate(deghist) if total-cs > 1]
deg=G.degree()
edge_degrees=sorted(sorted((deg[u],deg[v])) for u,v in G.edges_iter())
ek=G.number_of_edges()
k1,k2=edge_degrees.pop(0)
rc={}
for d,nk in zip(range(len(nks)),nks):
while k1 <= d:
if len(edge_degrees)==0:
break
k1,k2=edge_degrees.pop(0)
ek-=1
rc[d] = 2.0*ek/(nk*(nk-1))
return rc
示例3: test_grid_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def test_grid_graph(self):
"""grid_graph([n,m]) is a connected simple graph with the
following properties:
number_of_nodes = n*m
degree_histogram = [0,0,4,2*(n+m)-8,(n-2)*(m-2)]
"""
for n, m in [(3, 5), (5, 3), (4, 5), (5, 4)]:
dim = [n, m]
g = nx.grid_graph(dim)
assert_equal(len(g), n * m)
assert_equal(nx.degree_histogram(g), [0, 0, 4, 2 * (n + m) - 8,
(n - 2) * (m - 2)])
for n, m in [(1, 5), (5, 1)]:
dim = [n, m]
g = nx.grid_graph(dim)
assert_equal(len(g), n * m)
assert_true(nx.is_isomorphic(g, nx.path_graph(5)))
# mg = nx.grid_graph([n,m], create_using=MultiGraph())
# assert_equal(mg.edges(), g.edges())
示例4: test_grid_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def test_grid_graph(self):
"""grid_graph([n,m]) is a connected simple graph with the
following properties:
number_of_nodes = n*m
degree_histogram = [0,0,4,2*(n+m)-8,(n-2)*(m-2)]
"""
for n, m in [(3, 5), (5, 3), (4, 5), (5, 4)]:
dim = [n, m]
g = nx.grid_graph(dim)
assert_equal(len(g), n*m)
assert_equal(nx.degree_histogram(g), [0, 0, 4, 2 * (n + m) - 8,
(n - 2) * (m - 2)])
for n, m in [(1, 5), (5, 1)]:
dim = [n, m]
g = nx.grid_graph(dim)
assert_equal(len(g), n*m)
assert_true(nx.is_isomorphic(g, nx.path_graph(5)))
# mg = nx.grid_graph([n,m], create_using=MultiGraph())
# assert_equal(mg.edges(), g.edges())
示例5: get_degree_distribution
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def get_degree_distribution(edge_list):
G = nx.from_edgelist(edge_list)
hist = nx.degree_histogram(G)
return hist
示例6: degree_worker
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def degree_worker(G):
return np.array(nx.degree_histogram(G))
示例7: degree_stats
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def degree_stats(graph_ref_list, graph_pred_list, is_parallel=False):
''' Compute the distance between the degree distributions of two unordered sets of graphs.
Args:
graph_ref_list, graph_target_list: two lists of networkx graphs to be evaluated
'''
sample_ref = []
sample_pred = []
# in case an empty graph is generated
graph_pred_list_remove_empty = [G for G in graph_pred_list if not G.number_of_nodes() == 0]
prev = datetime.now()
if is_parallel:
with concurrent.futures.ProcessPoolExecutor() as executor:
for deg_hist in executor.map(degree_worker, graph_ref_list):
sample_ref.append(deg_hist)
with concurrent.futures.ProcessPoolExecutor() as executor:
for deg_hist in executor.map(degree_worker, graph_pred_list_remove_empty):
sample_pred.append(deg_hist)
else:
for i in range(len(graph_ref_list)):
degree_temp = np.array(nx.degree_histogram(graph_ref_list[i]))
sample_ref.append(degree_temp)
for i in range(len(graph_pred_list_remove_empty)):
degree_temp = np.array(nx.degree_histogram(graph_pred_list_remove_empty[i]))
sample_pred.append(degree_temp)
print(len(sample_ref),len(sample_pred))
mmd_dist = mmd.compute_mmd(sample_ref, sample_pred, kernel=mmd.gaussian_emd)
elapsed = datetime.now() - prev
if PRINT_TIME:
print('Time computing degree mmd: ', elapsed)
return mmd_dist
示例8: get_degree_hist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def get_degree_hist(graph):
r"""
Compute histogram of node degrees for a graph.
:param graph: graph representation in networkx format: nx.from_numpy_matrix(A)
:return: histogram of degrees
"""
hist_d = nx.degree_histogram(graph)
return np.array(hist_d)
示例9: test_degree_histogram
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def test_degree_histogram(self):
assert_equal(nx.degree_histogram(self.G), [1,1,1,1,1])
示例10: test_degree_histogram
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def test_degree_histogram(self):
assert_equal(nx.degree_histogram(self.G), [1, 1, 1, 1, 1])
示例11: _compute_rc
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def _compute_rc(G):
"""Returns the rich-club coefficient for each degree in the graph
`G`.
`G` is an undirected graph without multiedges.
Returns a dictionary mapping degree to rich-club coefficient for
that degree.
"""
deghist = nx.degree_histogram(G)
total = sum(deghist)
# Compute the number of nodes with degree greater than `k`, for each
# degree `k` (omitting the last entry, which is zero).
nks = (total - cs for cs in accumulate(deghist) if total - cs > 1)
# Create a sorted list of pairs of edge endpoint degrees.
#
# The list is sorted in reverse order so that we can pop from the
# right side of the list later, instead of popping from the left
# side of the list, which would have a linear time cost.
edge_degrees = sorted((sorted(map(G.degree, e)) for e in G.edges()),
reverse=True)
ek = G.number_of_edges()
k1, k2 = edge_degrees.pop()
rc = {}
for d, nk in enumerate(nks):
while k1 <= d:
if len(edge_degrees) == 0:
ek = 0
break
k1, k2 = edge_degrees.pop()
ek -= 1
rc[d] = 2 * ek / (nk * (nk - 1))
return rc
示例12: test_degree_distribution
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def test_degree_distribution(self):
m, n = 5, 6
G = nx.grid_2d_graph(m, n)
expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)]
assert_equal(nx.degree_histogram(G), expected_histogram)
示例13: Graph_synthetic
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import degree_histogram [as 别名]
def Graph_synthetic(seed):
G = nx.Graph()
np.random.seed(seed)
base = np.repeat(np.eye(5), 20, axis=0)
rand = np.random.randn(100, 5) * 0.05
node_features = base + rand
# # print('node features')
# for i in range(node_features.shape[0]):
# print(np.around(node_features[i], decimals=4))
node_distance_l1 = np.ones((node_features.shape[0], node_features.shape[0]))
node_distance_np = np.zeros((node_features.shape[0], node_features.shape[0]))
for i in range(node_features.shape[0]):
for j in range(node_features.shape[0]):
if i != j:
node_distance_l1[i,j] = np.sum(np.abs(node_features[i] - node_features[j]))
# print('node distance', node_distance_l1[i,j])
node_distance_np[i, j] = 1 / np.sum(np.abs(node_features[i] - node_features[j]) ** 2)
print('node distance max', np.max(node_distance_l1))
print('node distance min', np.min(node_distance_l1))
node_distance_np_sum = np.sum(node_distance_np, axis=1, keepdims=True)
embedding_dist = node_distance_np / node_distance_np_sum
# generate the graph
average_degree = 9
for i in range(node_features.shape[0]):
for j in range(i + 1, embedding_dist.shape[0]):
p = np.random.rand()
if p < embedding_dist[i, j] * average_degree:
G.add_edge(i, j)
G.remove_nodes_from(nx.isolates(G))
print('num of nodes', G.number_of_nodes())
print('num of edges', G.number_of_edges())
G_deg = nx.degree_histogram(G)
G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
print('average degree', sum(G_deg_sum) / G.number_of_nodes())
print('average path length', nx.average_shortest_path_length(G))
print('diameter', nx.diameter(G))
G_cluster = sorted(list(nx.clustering(G).values()))
print('average clustering coefficient', sum(G_cluster) / len(G_cluster))
print('Graph generation complete!')
# node_features = np.concatenate((node_features, np.zeros((1,node_features.shape[1]))),axis=0)
return G, node_features
# G = Graph_synthetic(10)
# return adj and features from a single graph