Skip to main content

Getting started with SparseBM

SparseBM is a python module for handling sparse graphs with Block Models. The module is an implementation of the variational inference algorithm for the Stochastic Block Model (SBM) and the Latent Block Model (LBM) for sparse graphs, which leverages the sparsity of edges to scale to very large numbers of nodes. The module can use Cupy to take advantage of the hardware acceleration provided by graphics processing units (GPU).

Installing

The SparseBM module is distributed through the PyPI repository and the documentation is available here.

With GPU acceleration (recommended if GPUs are available)

This option is recommended if GPUs are available to speedup computation.

With the package installer pip:

pip3 install sparsebm[gpu]

The Cupy module will be installed as a dependency.

Alternatively Cupy can be installed separately, and will be used by sparsebm if available.

pip3 install sparsebm
pip3 install cupy

Without GPU acceleration

Without GPU acceleration, only CPUs are used. The infererence process still uses sparsity, but no GPU linear algebra operations.

pip3 install sparsebm

For users who do not have GPU, we recommend the free serverless Jupyter notebook environment provided by Google Colab where the Cupy module is already installed and ready to be used with a GPU.

Complete examples (notebook for Google Colab and scripts) are given in the Examples section (and in the examples/ of the repository).

Quick example with the Stochastic Block Model

  • Generate a synthetic graph for analysis with SBM:

    from sparsebm import generate_SBM_dataset
    
    dataset = generate_SBM_dataset(symmetric=True)
    
  • Infer with the Bernoulli Stochastic Bloc Model:

    from sparsebm import SBM
    
    # A number of classes must be specified. Otherwise see model selection.
    model = SBM(dataset.clusters)
    model.fit(dataset.data, symmetric=True)
    print("Labels:", model.labels)
    
  • Compute performance:

    from sparsebm.utils import ARI
    ari = ARI(dataset.labels, model.labels)
    print("Adjusted Rand index is {:.2f}".format(ari))
    
  • Model selection: Infer with the Bernoulli Stochastic Bloc Model with an unknown number of groups:

    from sparsebm import ModelSelection
    
    model_selection = ModelSelection("SBM")
    models = model_selection.fit(dataset.data, symmetric=True)
    print("Labels:", models.best.labels)
    
    from sparsebm.utils import ARI
    ari = ARI(dataset.labels, models.best.labels)
    print("Adjusted Rand index is {:.2f}".format(ari))
    

Quick example with the Latent Block Model

  • Generate a synthetic graph for analysis with LBM:

    from sparsebm import generate_LBM_dataset
    
    dataset = generate_LBM_dataset()
    
  • Use the Bernoulli Latent Bloc Model:

    from sparsebm import LBM
    
    # A number of classes must be specified. Otherwise see model selection.
    model = LBM(
        dataset.row_clusters,
        dataset.column_clusters,
        n_init_total_run=1,
    )
    model.fit(dataset.data)
    print("Row Labels:", model.row_labels)
    print("Column Labels:", model.column_labels)
    
  • Compute performance:

    from sparsebm.utils import CARI
    cari = CARI(
        dataset.row_labels,
        dataset.column_labels,
        model.row_labels,
        model.column_labels,
    )
    print("Co-Adjusted Rand index is {:.2f}".format(cari))
    
  • Model selection: Infer with the Bernoulli Latent Bloc Model with an unknown number of groups:

    from sparsebm import ModelSelection
    
    model_selection = ModelSelection("LBM")
    models = model_selection.fit(dataset.data)
    print("Row Labels:", models.best.row_labels)
    print("Column Labels:", models.best.column_labels)
    
    from sparsebm.utils import CARI
    cari = CARI(
        dataset.row_labels,
        dataset.column_labels,
        models.best.row_labels,
        models.best.column_labels,
    )
    print("Co-Adjusted Rand index is {:.2f}".format(cari))
    

Release files for sparsebm 1.6.7

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

Source distribution (sdist)

Source distribution for sparsebm 1.6.7
File Size Uploaded
sparsebm-1.6.7.tar.gz 140.0 kB Details

Built distribution (wheel)

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

Total release size: 177.9 kB

Release files / sparsebm-1.6.7.tar.gz

Download URL sparsebm-1.6.7.tar.gz
Size 140.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a0d12f395b580e96bc0cc2ede93ea792adbb1b3228159705f11556cdb67dd4d8
BLAKE2b-256 checksum
How to use checksums
a79eec5313de29f1aed3f6ea26725172326d0aefcd67fa3701876bebe49eba79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / sparsebm-1.6.7-py3-none-any.whl

Download URL sparsebm-1.6.7-py3-none-any.whl
Size 37.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4ba270ccd74edbde6bdf6845db6fb78c72e084893ad2524b665659038b40123
BLAKE2b-256 checksum
How to use checksums
716bb724c8f7053beac85bef72dbe2e46b6cbc1becad348902915fce3f117c8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

1.6.7 This release

2 release files

1.6.6

2 release files

1.6.5

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.3

2 release files

0.2

2 release files

0.1

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