Skip to main content

Mixed Membership Stochastic Block Models

PyPI version Documentation Status Python Versions Tests Coverage Status License: MIT Downloads

A Python implementation of Mixed Membership Stochastic Block Models for recommendation systems, based on the work by Godoy-Lorite et al. (2016). This library provides an efficient, vectorized implementation suitable for both research and production environments.

Freatures

  • Fast, vectorized implementation of MMSBM.
  • Support for both simple and cross-validated fitting.
  • Parallel processing for multiple sampling runs.
  • Comprehensive model statistics and evaluation metrics.
  • Compatible with Python 3.6+ through 3.12.

Installation

pip install mmsbm

Quick Start

from mmsbm import MMSBM

# Create a model
model = MMSBM(user_groups=2, item_groups=4)

# Fit and predict
model.fit(train_data)
predictions = model.predict(test_data)

# Get model results
results = model.score()

Detailed Usage

Data Format

The input data should be a pandas DataFrame with exactly 3 columns representing users, items, and ratings:

import pandas as pd
from random import choice

train = pd.DataFrame(
    {
    "users": [f"user{choice(list(range(5)))}" for _ in range(100)],
    "items": [f"item{choice(list(range(10)))}" for _ in range(100)],
    "ratings": [choice(list(range(1, 6))) for _ in range(100)]
    }
)

test = pd.DataFrame(
    {
    "users": [f"user{choice(list(range(5)))}" for _ in range(50)],
    "items": [f"item{choice(list(range(10)))}" for _ in range(50)],
    "ratings": [choice(list(range(1, 6))) for _ in range(50)]
    }
)

Model Configuration

from mmsbm import MMSBM

# Initialize the MMSBM class:
model = MMSBM(
    user_groups=2,      # Number of user groups
    item_groups=4,      # Number of item groups
    iterations=500,     # Number of EM iterations
    sampling=5,         # Number of parallel runs
    seed=1,             # Random seed for reproducibility
    debug=False         # Enable debug logging
)

Training Methods

Simple Fit

mmsbm.fit(train)

Cross-Validation Fit

accuracies = mmsbm.cv_fit(train, folds=5)
print(f"Mean accuracy: {np.mean(accuracies):.3f} ± {np.std(accuracies):.3f}")

Making Predictions

predictions = mmsbm.predict(test)

Model Evaluation

results = model.score()

# Access various metrics
accuracy = results['stats']['accuracy']
mae = results['stats']['mae']

# Access model parameters
theta = results['objects']['theta']  # User group memberships
eta = results['objects']['eta']      # Item group memberships
pr = results['objects']['pr']        # Rating probabilities

Performance Considerations

  • Computation is vectorized for efficient processing of large datasets.
  • Parallel processing for multiple sampling runs
  • Computational complexity scales primarily with the number of unique items, but not users
  • Memory usage scales primarily with the number of unique users and items

Running Tests

To run tests do the following:

# Install development dependencies
pip install -e ".[dev]"

# Run tests
python -m pytest tests/*

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

TODO

  • Progress bars are not working for jupyter notebooks.
  • Include user_groups and item_groups optimization procedure.
  • The cv_fit test is not working on travis.

References

[1]: Godoy-Lorite, Antonia, et al. "Accurate and scalable social recommendation using mixed-membership stochastic block models." Proceedings of the National Academy of Sciences 113.50 (2016): 14207-14212.

Download files

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

Source Distribution

mmsbm-0.3.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mmsbm-0.3.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file mmsbm-0.3.0.tar.gz.

File metadata

  • Download URL: mmsbm-0.3.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for mmsbm-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d21b7ee2e155b5fdcbc4ea31f5c080c9fc284f80470e262fc7965f8a0ef85858
MD5 ae9cb0c0ac3a4db1fd2922ec9424d5de
BLAKE2b-256 3482612136181464bc9049eacc63344827530a258ccaa577ab0b7c9d48511c30

See more details on using hashes here.

File details

Details for the file mmsbm-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mmsbm-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for mmsbm-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef364db1faf0b684ee58e36a47a4d3fe4675b6d719aebc36ba8d67693bbd150e
MD5 7b770b16f3311b6dfe3cc0f724debd39
BLAKE2b-256 41847f17bd966f885ab470c0e95ff128d8399d9cd84a32698f8cf3ce3c01943b

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.4

2 files

0.3.1

2 files

This release

0.3.0 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.7

2 files

0.0.1

2 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