本文整理汇总了Python中networkx.nodes方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.nodes方法的具体用法?Python networkx.nodes怎么用?Python networkx.nodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.nodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_from_recipes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def build_from_recipes(recipes):
logger.info("Building Recipe DAG")
package2recipes = {}
recipe_list = []
for recipe in recipes:
for package in recipe.package_names:
package2recipes.setdefault(package, set()).add(recipe)
recipe_list.append(recipe)
dag = nx.DiGraph()
dag.add_nodes_from(recipe.reldir for recipe in recipes)
dag.add_edges_from(
(recipe2, recipe)
for recipe in recipe_list
for dep in recipe.get_deps()
for recipe2 in package2recipes.get(dep, [])
)
logger.info("Building Recipe DAG: done (%i nodes, %i edges)", len(dag), len(dag.edges()))
return dag
示例2: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def __init__(self, nx_G, is_directed, args):
"""
Constructor for SecondOrderRandomWalker.
:param nx_G: Nx graph object.
:param is_directed: Directed nature of the graph -- True/False.
:param args: Arguments object.
"""
self.G = nx_G
self.nodes = nx.nodes(self.G)
print("Edge weighting.\n")
for edge in tqdm(self.G.edges()):
self.G[edge[0]][edge[1]]['weight'] = 1.0
self.G[edge[1]][edge[0]]['weight'] = 1.0
self.is_directed = is_directed
self.walk_length = args.walk_length
self.walk_number = args.walk_number
self.p = args.P
self.q = args.Q
示例3: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def __init__(self, G, p, q, num_walks, walk_length):
"""
:param G: NetworkX graph object.
:param p: Return parameter.
:param q: In-out parameter.
:param num_walks: Number of walks per source node.
:param walk_length: Random walk length.
"""
self.G = G
self.nodes = nx.nodes(self.G)
print("Edge weighting.\n")
for edge in tqdm(self.G.edges()):
self.G[edge[0]][edge[1]]['weight'] = 1.0
self.G[edge[1]][edge[0]]['weight'] = 1.0
self.p = p
self.q = q
self.num_walks = num_walks
self.walk_length = walk_length
self.preprocess_transition_probs()
self.simulate_walks()
示例4: preprocess_transition_probs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def preprocess_transition_probs(self):
"""
Preprocessing of transition probabilities for guiding the random walks.
"""
G = self.G
alias_nodes = {}
print("")
print("Preprocesing.\n")
for node in tqdm(G.nodes()):
unnormalized_probs = [G[node][nbr]['weight'] for nbr in sorted(G.neighbors(node))]
norm_const = sum(unnormalized_probs)
normalized_probs = [float(u_prob)/norm_const for u_prob in unnormalized_probs]
alias_nodes[node] = alias_setup(normalized_probs)
alias_edges = {}
triads = {}
for edge in tqdm(G.edges()):
alias_edges[edge] = self.get_alias_edge(edge[0], edge[1])
alias_edges[(edge[1], edge[0])] = self.get_alias_edge(edge[1], edge[0])
self.alias_nodes = alias_nodes
self.alias_edges = alias_edges
print("\n")
示例5: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def __init__(self, graph=None, network_filename=None, epsilon=0.25, min_community_size=3, file_output=None):
"""
Constructor
:@param network_filename: the networkx filename
:@param epsilon: the tolerance required in order to merge communities
:@param min_community_size:min nodes needed to form a community
:@param file_output: True/False
"""
if graph is None:
self.g = nx.Graph()
if network_filename is not None:
self.__read_graph(network_filename)
else:
raise ImportError
else:
self.g = graph
self.epsilon = epsilon
self.min_community_size = min_community_size
self.file_output = file_output
self.base = os.getcwd()
示例6: simulate_walks
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def simulate_walks(self, num_walks, walk_length):
"""
Repeatedly simulate random walks from each node.
"""
G = self.G
walks = []
nodes = list(G.nodes())
for walk_iter in range(num_walks):
print(" ")
print("Random walk series " + str(walk_iter+1) + ". initiated.")
print(" ")
random.shuffle(nodes)
for node in tqdm(nodes):
walks.append(self.node2vec_walk(walk_length=walk_length, start_node=node))
return walks, self.count_frequency_values(walks)
示例7: test_info
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_info(self):
G=nx.path_graph(5)
info=nx.info(G)
expected_graph_info='\n'.join(['Name: path_graph(5)',
'Type: Graph',
'Number of nodes: 5',
'Number of edges: 4',
'Average degree: 1.6000'])
assert_equal(info,expected_graph_info)
info=nx.info(G,n=1)
expected_node_info='\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 0 2'])
assert_equal(info,expected_node_info)
示例8: test_info_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_info_digraph(self):
G=nx.DiGraph(name='path_graph(5)')
G.add_path([0,1,2,3,4])
info=nx.info(G)
expected_graph_info='\n'.join(['Name: path_graph(5)',
'Type: DiGraph',
'Number of nodes: 5',
'Number of edges: 4',
'Average in degree: 0.8000',
'Average out degree: 0.8000'])
assert_equal(info,expected_graph_info)
info=nx.info(G,n=1)
expected_node_info='\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 2'])
assert_equal(info,expected_node_info)
assert_raises(nx.NetworkXError,nx.info,G,n=-1)
示例9: test_neighbors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_neighbors(self):
graph = nx.complete_graph(100)
pop = random.sample(graph.nodes(), 1)
nbors = list(nx.neighbors(graph, pop[0]))
# should be all the other vertices in the graph
assert_equal(len(nbors), len(graph) - 1)
graph = nx.path_graph(100)
node = random.sample(graph.nodes(), 1)[0]
nbors = list(nx.neighbors(graph, node))
# should be all the other vertices in the graph
if node != 0 and node != 99:
assert_equal(len(nbors), 2)
else:
assert_equal(len(nbors), 1)
# create a star graph with 99 outer nodes
graph = nx.star_graph(99)
nbors = list(nx.neighbors(graph, 0))
assert_equal(len(nbors), 99)
示例10: test_set_node_attributes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_set_node_attributes():
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
for G in graphs:
G = nx.path_graph(3, create_using=G)
# Test single value
attr = 'hello'
vals = 100
nx.set_node_attributes(G, attr, vals)
assert_equal(G.node[0][attr], vals)
assert_equal(G.node[1][attr], vals)
assert_equal(G.node[2][attr], vals)
# Test multiple values
attr = 'hi'
vals = dict(zip(sorted(G.nodes()), range(len(G))))
nx.set_node_attributes(G, attr, vals)
assert_equal(G.node[0][attr], 0)
assert_equal(G.node[1][attr], 1)
assert_equal(G.node[2][attr], 2)
示例11: test_add_star
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_add_star(self):
G = self.G.copy()
nlist = [12, 13, 14, 15]
nx.add_star(G, nlist)
assert_edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)])
G = self.G.copy()
nx.add_star(G, nlist, weight=2.0)
assert_edges_equal(G.edges(nlist, data=True),
[(12, 13, {'weight': 2.}),
(12, 14, {'weight': 2.}),
(12, 15, {'weight': 2.})])
G = self.G.copy()
nlist = [12]
nx.add_star(G, nlist)
assert_nodes_equal(G, list(self.G) + nlist)
G = self.G.copy()
nlist = []
nx.add_star(G, nlist)
assert_nodes_equal(G.nodes, self.Gnodes)
assert_edges_equal(G.edges, self.G.edges)
示例12: test_info_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_info_digraph(self):
G = nx.DiGraph(name='path_graph(5)')
nx.add_path(G, [0, 1, 2, 3, 4])
info = nx.info(G)
expected_graph_info = '\n'.join(['Name: path_graph(5)',
'Type: DiGraph',
'Number of nodes: 5',
'Number of edges: 4',
'Average in degree: 0.8000',
'Average out degree: 0.8000'])
assert_equal(info, expected_graph_info)
info = nx.info(G, n=1)
expected_node_info = '\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 2'])
assert_equal(info, expected_node_info)
assert_raises(nx.NetworkXError, nx.info, G, n=-1)
示例13: test_neighbors_complete_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_neighbors_complete_graph(self):
graph = nx.complete_graph(100)
pop = random.sample(list(graph), 1)
nbors = list(nx.neighbors(graph, pop[0]))
# should be all the other vertices in the graph
assert_equal(len(nbors), len(graph) - 1)
graph = nx.path_graph(100)
node = random.sample(list(graph), 1)[0]
nbors = list(nx.neighbors(graph, node))
# should be all the other vertices in the graph
if node != 0 and node != 99:
assert_equal(len(nbors), 2)
else:
assert_equal(len(nbors), 1)
# create a star graph with 99 outer nodes
graph = nx.star_graph(99)
nbors = list(nx.neighbors(graph, 0))
assert_equal(len(nbors), 99)
示例14: test_info
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def test_info(self):
G = nx.path_graph(5)
G.name = "path_graph(5)"
info = nx.info(G)
expected_graph_info = '\n'.join(['Name: path_graph(5)',
'Type: Graph',
'Number of nodes: 5',
'Number of edges: 4',
'Average degree: 1.6000'])
assert_equal(info, expected_graph_info)
info = nx.info(G, n=1)
expected_node_info = '\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 0 2'])
assert_equal(info, expected_node_info)
示例15: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import nodes [as 别名]
def __init__(self, G, p, q, num_walks, walk_length):
"""
:param G: NetworkX object.
:param p: Return parameter.
:param q: In-out parameter.
:param num_walks: Number of walks per source.
:param walk_length: Number of nodes in walk.
"""
self.G = G
self.nodes = nx.nodes(self.G)
print("Edge weighting.\n")
for edge in tqdm(self.G.edges()):
self.G[edge[0]][edge[1]]['weight'] = 1.0
self.G[edge[1]][edge[0]]['weight'] = 1.0
self.p = p
self.q = q
self.num_walks = num_walks
self.walk_length = walk_length
self.preprocess_transition_probs()
self.simulate_walks()