Skip to main content

Mixed Membership Stochastic Block Models

Build Status

This repo follows this work on Mixed Membership Stochastic Block Models to build a recommender system [1].

Installation

pip install mmsbm

Usage

Input data

You'll need a pandas dataframe with exactly 3 columns: users, items and ratings, e.g.:

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)]
    }
)

Setup

from mmsbm import MMSBM

# Initialize the MMSBM class:
mmsbm = MMSBM(
    user_groups=2,
    item_groups=4,
    iterations=500,
    sampling=5,
    seed=1,
)

Fit models

In here you have two options, a simple fit where we run "sampling" times the fitting algorithm and return the results for all runs, you are then in charge of choosing the best one.

mmsbm.fit(train)

The other option is the cv_fit, whereby we split the input data in "folds" number of folds and run the fitting in each one and test on the excluded fold. We then return all the samplings of the best performing model.

mmsbm.cv_fit(train, folds=5)

Prediction

Once the model is fitted, we can predict on test data. The function predict returns the prediction matrix (the probability of each user to belong to each group) as a numpy array.

pred_matrix = mmsbm.predict(test)

Score

Finally, you can get statistics about the goodness of fit and other parameters of the model, as well as the computed objects: the theta matrix, the eta matrix and the probability distributions.

The function score returns a dictionary with two sub-dictionaries, one for statistics about the model (called "stats") and the other one with the computed objects (called "objects").

results = mmsbm.score()

Performance

Each iteration takes a little about half a second in an Intel i7. This means that a 500 iteration runs takes around 4 minutes. The computation is vectorized, so, as long as you don't go crazy with the number of observations, the time should be approximately the same regardless of training set size. It is also parallelized over sampling, so, as long as you choose less sampling than number of cores, you should have approximately the same performance regardless of training set size and sampling number.

A complete study could be something like 100 hyperparameter optimization runs of 6 samples of 400 iterations, which will take about 10 hours.

Tests

To run tests do the usual:

python -m pytest tests/*

TODO

  • Progress bars are not working for jupyter notebooks.
  • Include user_groups and item_groups optimization procedure.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

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.1.3.tar.gz (11.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.1.3-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mmsbm-0.1.3.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.8

File hashes

Hashes for mmsbm-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9e9deb6021b2fc399a6b7cc7b0ccd7e399fc059f0d0f0e9dea957964d37147e1
MD5 d63c2db0a634e23b89504fac120220c9
BLAKE2b-256 c607eea4e117a652c48a2bfabd4feb7a0021a81a5f1bc6a5e82e2f86b520bdd2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mmsbm-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.8

File hashes

Hashes for mmsbm-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4909e72354b76040470a02c682c7aaa3c74603f10992a3b898447ce37786ffbe
MD5 45bcd5f124b35dfcc8e8b7144517c1d5
BLAKE2b-256 ec0f2e04d73d8630a96e9d3a7a58c6630c6369c6ceef53656068543f7074cf22

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

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

This release

0.1.3 This release

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