Skip to main content

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

Project description

SparseBM: a python module for handling sparse graphs with Block Models

Installing

From pypi:

pip3 install sparsebm

To use GPU acceleration:

pip3 install sparsebm[gpu]

Or

pip3 install sparsebm
pip3 install cupy

Example with Stochastic Block Model

Generate SBM Synthetic graph

  • 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 sparsebm SBM:

  • Use 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))

To use GPU acceleration, CUPY needs to be installed and replace gpu_number to the desired GPU index.

Example with Latent Block Model

Generate LBM Synthetic graph

  • 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"]

Infere with sparsebm LBM:

  • 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.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

sparsebm-1.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sparsebm-1.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4

File hashes

Hashes for sparsebm-1.0.tar.gz
Algorithm Hash digest
SHA256 336c727a96515d2cc187c7809b0e8989128c71815619c4f1742959d46fe8611d
MD5 4882ea92084e54011d4f1a5c131b1488
BLAKE2b-256 c1cfbbd8fc710775047d895fb717533e716ba980263ca537503d66f27025ce0f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sparsebm-1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4

File hashes

Hashes for sparsebm-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d711b4eed42dab0619ebf8c82f130f73f23d13cb8df7c1732b4601c2c513a865
MD5 e183b0ce1cbde281f176db41842c7bde
BLAKE2b-256 9959e928a2fca7e4c18a7377855f88be1d5911dc5e858e695bd314d5b81532e0

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