本文整理汇总了Python中networkx.neighbors方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.neighbors方法的具体用法?Python networkx.neighbors怎么用?Python networkx.neighbors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.neighbors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: annotate_with_bfs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def annotate_with_bfs(orig_graph, start, max_depth=20):
graph = orig_graph.copy()
for u in graph.nodes():
graph.node[u]['vec'].append(1)
visited, queue, dist = set(), [start], dict()
dist[start] = 0
while queue:
vertex = queue.pop(0)
if dist[vertex] <= max_depth:
if vertex not in visited:
visited.add(vertex)
val = max_depth - dist[vertex]
graph.node[vertex]['vec'][-1] = val
next_nodes = [u for u in graph.neighbors(vertex)
if u not in visited]
next_dist = dist[vertex] + 1
for u in next_nodes:
dist[u] = next_dist
queue.extend(next_nodes)
return graph
示例2: compute_matching_neighborhoods_fraction
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def compute_matching_neighborhoods_fraction(GA, GB, pairings):
count = 0
matches = dict([(i, j) for i, j in enumerate(pairings)])
matching_edges = defaultdict(list)
for i, j in GA.edges():
ii = matches[i]
jj = matches[j]
if (ii, jj) in GB.edges():
matching_edges[i].append(j)
matching_edges[j].append(i)
for u in GA.nodes():
if matching_edges.get(u, False):
neighbors = nx.neighbors(GA, u)
matches_neighborhood = True
for v in neighbors:
if v not in matching_edges[u]:
matches_neighborhood = False
break
if matches_neighborhood:
count += 1
return float(count) / len(GA.nodes())
示例3: _make_a_pick
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def _make_a_pick(self, neighbors):
"""
Choosing a neighbor from a propagation source node.
Arg types:
* **neigbours** *(list)* - Neighbouring nodes.
"""
scores = {}
for neighbor in neighbors:
neighbor_label = self._labels[neighbor]
if neighbor_label in scores.keys():
scores[neighbor_label] = scores[neighbor_label] + 1
else:
scores[neighbor_label] = 1
top = [key for key, val in scores.items() if val == max(scores.values())]
return random.sample(top, 1)[0]
示例4: make_step
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def make_step(self, node, graph, features, labels, inverse_labels):
"""
:param node: Source node for step.
:param graph: NetworkX graph.
:param features: Feature matrix.
:param labels: Node labels hash table.
:param inverse_labels: Inverse node label hash table.
"""
orig_neighbors = set(nx.neighbors(graph, node))
label = self.sample_node_label(orig_neighbors, graph, features)
labels = list(set(orig_neighbors).intersection(set(inverse_labels[str(label)])))
new_node = random.choice(labels)
new_node_attributes = torch.zeros((len(self.identifiers), 1))
new_node_attributes[label, 0] = 1.0
attention_score = self.attention[label]
return new_node_attributes, new_node, attention_score
示例5: node2vec_walk
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def node2vec_walk(self, walk_length, start_node):
"""
Simulate a random walk starting from start node.
"""
G = self.G
alias_nodes = self.alias_nodes
alias_edges = self.alias_edges
walk = [start_node]
while len(walk) < walk_length:
cur = walk[-1]
cur_nbrs = sorted(G.neighbors(cur))
if len(cur_nbrs) > 0:
if len(walk) == 1:
walk.append(cur_nbrs[alias_draw(alias_nodes[cur][0], alias_nodes[cur][1])])
else:
prev = walk[-2]
next = cur_nbrs[alias_draw(alias_edges[(prev, cur)][0], alias_edges[(prev, cur)][1])]
walk.append(next)
else:
break
return walk
示例6: nodes_in_neighborhood_of_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def nodes_in_neighborhood_of_nodes(self, nodes, blacklist_nodes, nnodes=100):
"""
Takes a list of nodes and finds the neighbors with most connections to the nodes.
:param nodes: list
:param blacklist_nodes: list of node_pub_keys to be excluded from counting
:param nnodes: int, limit for the number of nodes returned
:return: list of tuples, (str pub_key, int number of neighbors)
"""
nodes = set(nodes)
# eliminate blacklisted nodes
nodes = nodes.difference(blacklist_nodes)
neighboring_nodes = []
for general_node in self.graph.nodes:
neighbors_general_node = set(self.neighbors(general_node))
intersection_with_nodes = nodes.intersection(neighbors_general_node)
number_of_connection_with_nodes = len(intersection_with_nodes)
neighboring_nodes.append((general_node, number_of_connection_with_nodes))
sorted_neighboring_nodes = sorted(neighboring_nodes, key=lambda x: x[1], reverse=True)
return sorted_neighboring_nodes[:nnodes]
示例7: test_neighbors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [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)
示例8: test_neighbors_complete_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [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)
示例9: test_neighbors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def test_neighbors(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)
示例10: node2vec_walk
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def node2vec_walk(self, start_node):
"""
Simulate a random walk starting from start node.
"""
G = self.G
alias_nodes = self.alias_nodes
alias_edges = self.alias_edges
walk = [start_node]
while len(walk) < self.walk_length:
cur = walk[-1]
cur_nbrs = sorted(G.neighbors(cur))
if len(cur_nbrs) > 0:
if len(walk) == 1:
walk.append(cur_nbrs[alias_draw(alias_nodes[cur][0], alias_nodes[cur][1])])
else:
prev = walk[-2]
next = cur_nbrs[alias_draw(alias_edges[(prev, cur)][0], alias_edges[(prev, cur)][1])]
walk.append(next)
else:
break
walk = [str(node) for node in walk]
return walk
示例11: get_alias_edge
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def get_alias_edge(self, src, dst):
"""
Get the alias edge setup lists for a given edge.
"""
G = self.G
p = self.p
q = self.q
unnormalized_probs = []
for dst_nbr in sorted(G.neighbors(dst)):
if dst_nbr == src:
unnormalized_probs.append(G[dst][dst_nbr]['weight']/p)
elif G.has_edge(dst_nbr, src):
unnormalized_probs.append(G[dst][dst_nbr]['weight'])
else:
unnormalized_probs.append(G[dst][dst_nbr]['weight']/q)
norm_const = sum(unnormalized_probs)
normalized_probs = [float(u_prob)/norm_const for u_prob in unnormalized_probs]
return alias_setup(normalized_probs)
示例12: preprocess_transition_probs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [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
示例13: _do_a_propagation
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def _do_a_propagation(self):
"""
Doing a propagation round.
"""
random.shuffle(self._nodes)
new_labels = {}
for node in self._nodes:
neighbors = [neb for neb in nx.neighbors(self._graph, node)]
pick = self._make_a_pick(neighbors)
new_labels[node] = pick
self._labels = new_labels
示例14: do_walk
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def do_walk(self, node):
"""
Doing a single truncated random walk from a source node.
:param node: Source node of the truncated random walk.
:return walk: A single random walk.
"""
walk = [node]
while len(walk) < self.args.walk_length:
nebs = [n for n in nx.neighbors(self.graph, walk[-1])]
if len(nebs) == 0:
break
walk.append(random.choice(nebs))
return walk
示例15: small_walk
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import neighbors [as 别名]
def small_walk(self, start_node):
"""
Doing a truncated random walk.
:param start_node: Start node for random walk.
:return walk: Truncated random walk with fixed maximal length.
"""
walk = [start_node]
while len(walk) < self.length:
nebs = [n for n in nx.neighbors(self.graph, walk[-1])]
if len(nebs) == 0:
break
walk.append(random.choice(nebs))
return walk