metrics¶
Node centrality, community detection, modularity and community graphs.
Node centrality and community detection for flow graphs.
CentralityKind
module-attribute
¶
CentralityKind: TypeAlias = Literal[
"in_strength",
"out_strength",
"strength",
"in_degree",
"out_degree",
"degree",
"pagerank",
"reverse_pagerank",
"betweenness",
]
Supported centrality measures.
in_strength/out_strength/strength: weighted in, out and total degree, in the graph's unit.in_degree/out_degree/degree: number of partners.- On a directed graph,
degreeandstrengthcount incoming plus outgoing edges. pagerank: weighted PageRank. A node ranks high when large flows arrive from nodes that themselves rank high (an important destination).reverse_pagerank: PageRank on the reversed graph. A node ranks high when it sends large flows to nodes that themselves rank high (an important source). Equal topagerankon undirected graphs.betweenness: weighted betweenness, using1 / weightas the edge length so that large flows count as short, well-used routes.
centrality
¶
centrality(
g: Graph[Any],
kinds: Sequence[CentralityKind] = DEFAULT_KINDS,
*,
normalized: bool = True,
alpha: float = 0.85,
) -> DataFrame
Compute several centrality measures at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Graph[Any]
|
A graph whose edges carry |
required |
kinds
|
Sequence[CentralityKind]
|
Measures to compute, in column order. See |
DEFAULT_KINDS
|
normalized
|
bool
|
Normalize betweenness by the number of node pairs. |
True
|
alpha
|
float
|
PageRank damping factor. |
0.85
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per node, one column per measure, sorted by the first measure
in descending order. The index is named |
Raises:
| Type | Description |
|---|---|
UnknownMetricError
|
If a name in |
Examples:
communities
¶
communities(
g: Graph[Any],
method: CommunityMethod = "louvain",
*,
seed: int = 0,
resolution: float = 1.0,
) -> Series
Partition the nodes of a graph into communities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Graph[Any]
|
A graph whose edges carry |
required |
method
|
CommunityMethod
|
|
'louvain'
|
seed
|
int
|
Random seed for Louvain, so results are reproducible. |
0
|
resolution
|
float
|
Modularity resolution. Values above 1 give more, smaller communities. |
1.0
|
Returns:
| Type | Description |
|---|---|
Series
|
Community id per node, named |
Raises:
| Type | Description |
|---|---|
UnknownMetricError
|
If |
InsufficientDataError
|
If the graph has no edges. |
modularity
¶
Return the weighted modularity of a partition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Graph[Any]
|
The graph that was partitioned. |
required |
partition
|
Series
|
Community id per node, as returned by |
required |
Returns:
| Type | Description |
|---|---|
float
|
Modularity, between -0.5 and 1. Higher means denser within-community flows than expected by chance. |
community_graph
¶
Collapse each community into a single node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Graph[Any]
|
The graph that was partitioned. |
required |
partition
|
Series
|
Community id per node, as returned by |
required |
Returns:
| Type | Description |
|---|---|
DiGraph or Graph
|
One node per community with attributes |