networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturnsBackRef
stochastic_block_model(sizes, p, nodelist=None, seed=None, directed=False, selfloops=False, sparse=True)

This model partitions the nodes in blocks of arbitrary sizes, and places edges between pairs of nodes independently, with a probability that depends on the blocks.

Parameters

sizes : list of ints

Sizes of blocks

p : list of list of floats

Element (r,s) gives the density of edges going from the nodes of group r to nodes of group s. p must match the number of groups (len(sizes) == len(p)), and it must be symmetric if the graph is undirected.

nodelist : list, optional

The block tags are assigned according to the node identifiers in nodelist. If nodelist is None, then the ordering is the range [0,sum(sizes)-1].

seed : integer, random_state, or None (default)

Indicator of random number generation state. See Randomness<randomness> .

directed : boolean optional, default=False

Whether to create a directed graph or not.

selfloops : boolean optional, default=False

Whether to include self-loops or not.

sparse: boolean optional, default=True :

Use the sparse heuristic to speed up the generator.

Raises

NetworkXError

If probabilities are not in [0,1]. If the probability matrix is not square (directed case). If the probability matrix is not symmetric (undirected case). If the sizes list does not match nodelist or the probability matrix. If nodelist contains duplicate.

Returns

g : NetworkX Graph or DiGraph

Stochastic block model graph of size sum(sizes)

Returns a stochastic block model graph.

See Also

gaussian_random_partition_graph
gnp_random_graph
planted_partition_graph
random_partition_graph

Examples

>>> 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
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.generators.community.stochastic_block_model

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/generators/community.py#488
type: <class 'function'>
Commit: