graphscope.nx.generators.community.stochastic_block_model

graphscope.nx.generators.community.stochastic_block_model(sizes, p, nodelist=None, seed=None, directed=False, selfloops=False, sparse=True)[源代码]

返回一个随机块模型图。

该模型将节点划分为任意大小的区块,并以取决于区块的概率独立地在节点对之间放置边。

Parameters:
  • sizes (list of ints) – 块的大小列表

  • p (list of list of floats) - 元素(r,s)表示从第r组节点指向第s组节点的边密度。 p必须与分组数量匹配(len(sizes) == len(p)), 如果是无向图,p必须是对称矩阵。

  • nodelist (list, optional) – 块标签将根据nodelist中的节点标识符进行分配。如果nodelist为None,则顺序为范围[0,sum(sizes)-1]。

  • seed (integer, random_state, or None (default)) - 随机数生成状态的指示器。 参见随机性

  • directed (boolean optional, default=False) – 是否创建有向图。

  • selfloops (boolean optional, default=False) – 是否包含自循环边。

  • sparse (boolean optional, default=True) – 使用稀疏启发式方法来加速生成器。

Returns:

g – 大小为 sum(sizes) 的随机块模型图

Return type:

NetworkX 图或 DiGraph

Raises:

NetworkXError – 如果概率值不在[0,1]范围内。 如果概率矩阵不是方阵(有向图情况)。 如果概率矩阵不对称(无向图情况)。 如果尺寸列表与节点列表或概率矩阵不匹配。 如果节点列表包含重复项。

示例

>>> sizes = [75, 75, 300]
>>> probs = [[0.25, 0.05, 0.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]]
>>> g = nx.stochastic_block_model(sizes, probs, seed=0)
>>> len(g)
450
>>> H = nx.quotient_graph(g, g.graph["partition"], relabel=True)
>>> for v in H.nodes(data=True):
...     print(round(v[1]["density"], 3))
...
0.245
0.348
0.405
>>> for v in H.edges(data=True):
...     print(round(1.0 * v[2]["weight"] / (sizes[v[0]] * sizes[v[1]]), 3))
...
0.051
0.022
0.07

参考文献