liveBook · Manning

Community detection

As explained in Financial AI in Practice

The identification of groups of graph nodes that are more densely connected to one another than to the rest of the graph.

A fraud ring can be hard to spot when its transactions look ordinary one by one. Community detection looks for groups of graph nodes that are more densely connected to one another than to the rest of the network. In a transaction graph, such groups may reveal coordinated accounts or shared resources worth investigating—not establish fraud on their own. section 9.0 section 9.3.3

A practical workflow moves from transaction records to inspectable clusters:

  • Build a graph with nodes for users, IP addresses, devices, phones, and cards. Connect entities that occur in the same transaction; an edge weight records how many transactions linked the pair. section 9.3.2
  • Run Louvain on the constructed graph, then assign its partition result to each node as a community_id. The example uses community_louvain.best_partition(G, random_state=42, resolution=1.0). section 9.3.3
  • Group nodes by community ID. For each induced subgraph, calculate node count and density, and count each entity type. Density measures internal connectedness; 1 denotes a fully connected clique. section 9.3.3
  • Compare community summaries, then visualize a selected subgraph with distinct markers for entity types. Look for structural patterns such as many users sharing one IP, and use those findings to guide investigation. section 9.3.3 section 9.6
Listing 9.3 Applying Louvain community detection
# Ensure NetworkX (nx) is imported and G is the constructed graph from Listing 9.2
# Ensure pandas as pd and display from IPython.display are imported

if G is not None and G.number_of_nodes() > 0: 
    print("\nPerforming Louvain community detection...")
    try:
        import community.community_louvain as community_louvain 
        partition = community_louvain.best_partition(G, random_state=42, resolution=1.0) 
        nx.set_node_attributes(G, partition, 'community_id')
        num_communities = len(set(partition.values()))
        print(f"Louvain algorithm detected {num_communities} communities.")

    except ImportError:
        print("Error: 'community_louvain' library not found. Please install it using 'pip install python-louvain'.")
        partition = None
    except Exception as e:
        print(f"An error occurred during community detection: {e}")
        partition = None
else:
    print("Graph G not available or empty. Skipping community detection.")
    partition = None # Ensure partition is defined

The Louvain listing shows the partition call, node-attribute assignment, community count, and a missing-library check. It requires an already constructed, nonempty graph. section 9.3.3

From graph statistics to a reviewable cluster

One transaction graph contains 14,906 entity nodes and 30,567 edges; Louvain reports 2,913 communities. That count is a starting point, not a fraud finding. A summary of the largest communities adds size, density, and entity-type counts, while a visualization of the largest shows relationships and possible bottlenecks. section 9.3.2 section 9.3.3

How Financial AI in Practice covers it

How deep it goes
Works through Louvain partitioning, community profiling, parallel summaries, and visualization, but does not develop the algorithm’s internals.
Examples
Uses e-commerce transactions, a 2,913-community result, ranked cluster metrics, and a largest-community visualization.
What it assumes
Assumes Python library imports and a constructed, nonempty NetworkX graph; builds that graph from tabular transactions beforehand.
What you can do after
Apply it via NetworkX and python-louvain to label, profile, and visualize communities for fraud investigation.
How it connects
Links transaction graphs and graph visualization to fraud-ring detection, alongside link analysis and graph neural networks.

Also explained in

Compare how 4 books explain community detection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Financial AI in Practice. The section links point to the parts of the book it draws on.