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 and the latent block model for sparse graphs, which leverages on the sparsity of edges to scale upto a very large number of nodes. The module can use Cupy to take advantage of the hardware speed up provided by graphics processingunits (GPU).

Installing

SparseBMmodule is distributed through the PyPI repository and the documentation is available at sparsebm.readthedocs.io. The module can be installed with the package installer pip:

pip3 install sparsebm

:warning: To leverage GPU accelaration, the Cupy module must be installed with pip or anaconda or directly with the extra argument when installing SparseBM:

pip3 install sparsebm[gpu]

Or directly with

pip3 install sparsebm
pip3 install cupy

For users that do not have GPU, we advise the free serverless Jupyter notebook environment provided by Google Colab where the Cupy module is already installed and ready to use with one GPU.

Example with Stochastic Block Model

  • Generate a synthetic graph to analyse with SBM:
from sparsebm import generate_SBM_dataset

dataset = generate_SBM_dataset(symmetric=True)
graph = dataset["data"]
cluster_indicator = dataset["cluster_indicator"]
  • Infere with the bernoulli Stochastic Bloc Model:
    from sparsebm import SBM

    number_of_clusters = cluster_indicator.shape[1]

    # A number of classes must be specify. Otherwise see model selection.
    model = SBM(number_of_clusters)
    model.fit(graph, symmetric=True)
    print("Labels:", model.labels)
  • Compute performances:
    from sparsebm.utils import ARI
    ari = ARI(cluster_indicator.argmax(1), model.labels)
    print("Adjusted Rand index is {:.2f}".format(ari))

Example with Latent Block Model

  • Generate a synthetic graph to analyse with LBM:
from sparsebm import generate_LBM_dataset

dataset = generate_LBM_dataset()
graph = dataset["data"]
row_cluster_indicator = dataset["row_cluster_indicator"]
column_cluster_indicator = dataset["column_cluster_indicator"]
  • Use the bernoulli Latent Bloc Model:
    from sparsebm import LBM

    number_of_row_clusters = row_cluster_indicator.shape[1]
    number_of_columns_clusters = column_cluster_indicator.shape[1]

    # A number of classes must be specify. Otherwise see model selection.
    model = LBM(
        number_of_row_clusters,
        number_of_columns_clusters,
        n_init_total_run=1,
    )
    model.fit(graph)
    print("Row Labels:", model.row_labels)
    print("Column Labels:", model.column_labels)
  • Compute performances:
    from sparsebm.utils import CARI
    cari = CARI(
        row_cluster_indicator.argmax(1),
        column_cluster_indicator.argmax(1),
        model.row_labels,
        model.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.3.tar.gz (27.8 kB view hashes)

Uploaded Source

Built Distribution

sparsebm-1.3-py3-none-any.whl (30.6 kB view hashes)

Uploaded Python 3

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