如何从networkx图中提取随机节点?

时间:2014-11-05 22:07:13

标签: python random nodes networkx

如何从networkx图中提取随机节点?我有一个networkx图形式的地图,我必须从中提取5个随机节点,并获得与每个节点及其边缘相关的数据。我想我可以用“np.random.choice”做到这一点,但我仍然无法得到任何结果。

4 个答案:

答案 0 :(得分:4)

import networkx as nx
from random import choice

g = nx.Graph()
g.add_edge(1,2)
g.add_edge(1,3)
g.add_edge(1,4)
g.add_edge(1,5)
g.add_edge(5,6)

random_node = choice(g.nodes())

可能重复: how to select two nodes (pairs of nodes) randomly from a graph that are NOT connected, Python, networkx

答案 1 :(得分:1)

请检查choice方法,而不是使用sample

from random import sample
import os
import networkx as nx

# load a graph from local
working_path = r'XXX'
graph = nx.read_gexf(os.path.join(working_path, 'XXX.gexf'))
# random sample 3 nodes from the graph
random_nodes = sample(list(graph.nodes()), 3)

如果您有一个具有不同节点类型的图g,则可以使用followig函数来计算具有特定类型的节点的数量:

def count_nodes_with_type(graph, attribute_name, query):
    """
    Count the number of nodes with specific type
    :param graph: a networkx graph whose nodes have several different types
    :param attribute_name: the attribute name used to access different type of nodes
    :param query: the search query
    :return: number of nodes satisfying the query
    """
    node_attribute_dict = nx.get_node_attributes(graph, attribute_name)
    filtered_nodes = {key: value for (key, value) in node_attribute_dict.items() if value == query}
    return len(filtered_nodes)

您可以使用相同的逻辑,通过nx.get_edge_attributes方法来计算特定类型的边数。

答案 2 :(得分:1)

我在选择,样本和numpy.random.choice之间进行了时间比较。在三分之二中,numpy的速度最慢,而且幅度很大。选择和样本似乎具有可比性(选择速度更快):

from random import choice, sample
import numpy as np
from time import time
import networkx as nx

# Let's pick 10 samples from a graph
n = 10
graph = nx.gnm_random_graph(5000, 20000) # make graph with 5k nodes and 20k edges
nodes = list(graph.nodes())
s = time()
samples = sample(nodes, n)
tsample = time() - s
s = time()
for _ in range(n):
    node = choice(nodes)
tchoice = time() -s
s= time()
for _ in range(n):
    node = np.random.choice(nodes)
tnp = time() -s 
print(tsample, tchoice, tnp)
>>> 8.916854858398438e-05    7.700920104980469e-05    0.0042879581451416016

答案 3 :(得分:1)

使用 NetworkX 的最新版本(我认为是 >= 2.5),您可以直接在节点视图上使用 random.sample() 来获取节点的标签/索引或标签的样本以及节点的数据。

import networkx as nx
import random as rd

# Generate the example Karate club graph provided in NetworkX
g = nx.karate_club_graph()
print(g.nodes)  # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]

# Get a random sample (without replacement) of the node labels/indexes:
sample = rd.sample(g.nodes, 3)
print(sample)  # Output: [22, 18, 6]

# Get a random sample (without replacement) of the node labels and data:
sample = rd.sample(g.nodes.items(), 3)
print(sample)  # Output: [(24, {'club': 'Officer'}), (27, {'club': 'Officer'}), (31, {'club': 'Officer'})]

在稍旧的版本中(从 2.0 但在 2.5 之前),您需要在使用 random.sample 之前将节点视图转换为列表。

注意:如果您安装了最新的 NetworkX 软件包,则不必担心这一点。

# Get a random sample (without replacement) of the node labels/indexes
# in older version of NetworkX.
sample = rd.sample(list(g.nodes), 3)