Skip to main content

threshold-clustering

Threshold Spectral Community Detection for NetworkX

NetworkX Community detection based on the algorithm proposed in Guzzi et. al. 2013 (*).

Developed for semantic similarity networks, this algorithm specifically targets weighted and directed graphs. This implementation adds a couple of options to the algorithm proposed in the paper, such as passing an arbitrary community detection function (e.g. python-louvain).

Similarity networks are typically dense, weighted and difficult to cluster. Experience shows that algorithms such as python-louvain have difficulty finding outliers and smaller partitions.

Given a networkX.DiGraph object, threshold-clustering will try to remove insignificant ties according to a local threshold. This threshold is refined until the network breaks into distinct components in a sparse, undirected network.

As a next step, either these components are taken communities directly, or, alternatively, another community detection (e.g. python-louvain) can be applied.

Example

Consider the cosine similarities in the Karate Club Network. Although these similarities are not directed, they are rather dense.

import networkx as nx
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import cosine_similarity

# load graph
G = nx.karate_club_graph()

# Generate a similarity style weighted graph
Adj=nx.to_numpy_matrix(G)
cos_Adj=cosine_similarity(Adj.T)
G=nx.from_numpy_matrix(cos_Adj)

pos = nx.spring_layout(G)
weights = np.array([G[u][v]['weight'] for u,v in G.edges()])*5
nx.draw_networkx_nodes(G, pos, node_size=40)
nx.draw_networkx_edges(G, pos, alpha=0.2, width=weights)
plt.show()

Similarity Network

Let's use python-louvain to find the best partition.

partition=community_louvain.best_partition(G.to_undirected())

cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
                       cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.2,width=weights)
plt.show()

Best Partition

We get three rather large partition and no sense of outliers.

Instead, we can use threshold-clustering's best_partition function to run python_louvain's community detection on a transformed network.

from thresholdclustering import best_partition

cluster_function = community_louvain.best_partition
partition, alpha = best_partition(G, cluster_function=cluster_function)



cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
                       cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.2,width=weights)
plt.show()

Best Partition with threshold-clustering

We can see that more communities of similarity can be identified. Note in particular outliers drawn in yellow.

Installation

(*) Guzzi, Pietro Hiram, Pierangelo Veltri, and Mario Cannataro. "Thresholding of semantic similarity networks using a spectral graph-based technique." International Workshop on New Frontiers in Mining Complex Patterns. Springer, Cham, 2013.

History

1.0

First release. Since this is a small, yet nifty add-on to python-louvain, it is a quick write. Please let me know if anything more should be added!

Metadata

Release files for thresholdclustering 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thresholdclustering 1.1
File Size Uploaded
thresholdclustering-1.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for thresholdclustering 1.1
File Interpreter ABI Platform
thresholdclustering-1.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / thresholdclustering-1.1.tar.gz

Download URL thresholdclustering-1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
af27ceec7a4a8903e1b34e0d57ac28a42deb7d2beff3c403330579b11525bee1
BLAKE2b-256 checksum
How to use checksums
273884c0b0662503d94cb09a5709447257fa9b8ecc93648618d56d7303943cdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/54.1.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5

Release files / thresholdclustering-1.1-py3-none-any.whl

Download URL thresholdclustering-1.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdad9cd6f58b1e551b0232f3d76f662622aaaacb27397a0372ebb8f9cd629e10
BLAKE2b-256 checksum
How to use checksums
49262b0d326e9d637c687a8f1aeeb5b7414866576a6721355bdf6532d05d4793
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/54.1.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page