Skip to main content

An implementation of Stochastic Bloc model and Latent Block model efficient with sparse matrices

Project description

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.

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 a 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))
    

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 a 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))
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparsebm-1.6.4.tar.gz (138.1 kB view details)

Uploaded Source

Built Distribution

sparsebm-1.6.4-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

Details for the file sparsebm-1.6.4.tar.gz.

File metadata

  • Download URL: sparsebm-1.6.4.tar.gz
  • Upload date:
  • Size: 138.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for sparsebm-1.6.4.tar.gz
Algorithm Hash digest
SHA256 24989b79a849729effd38468b3a1808df50427d09759e42687d6d4de18739974
MD5 12e3b03cf60c947259d74cf9a350531b
BLAKE2b-256 b12352a6617b113be4cd53bfaab6c27a81fe3674c898671864f852aa94f1183f

See more details on using hashes here.

File details

Details for the file sparsebm-1.6.4-py3-none-any.whl.

File metadata

  • Download URL: sparsebm-1.6.4-py3-none-any.whl
  • Upload date:
  • Size: 37.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for sparsebm-1.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e162e7b33a597bf7728f66024e07af54f9bdb829381e5136e071b591a15cd7bc
MD5 5f36354ce3cca8d2e1124bcd2dd92ceb
BLAKE2b-256 d4897549c86b2d348d53ab18667e06a73dccc09d160f9c74546a70b0eb8c4fa4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page