Skip to main content

ci PyPI version fury.io PyPI license PRs Welcome Downloads

Mab2Rec: Multi-Armed Bandits Recommender

Mab2Rec (AAAI'24) is a Python library for building bandit-based recommendation algorithms. It supports context-free, parametric and non-parametric contextual bandit models powered by MABWiser and fairness and recommenders evaluations powered by Jurity. The library is designed with rapid experimentation in mind, follows the PEP-8 standards, and is tested heavily.

Mab2Rec is built on top of several other open-source software developed at the AI Center at Fidelity:

An introduction to content- and context-aware recommender systems and an overview of the building blocks of the library is presented at AAAI 2024 and All Things Open 2021. There is a corresponding blogpost to serve as a starting point for practioners to build and deploy bandit-based recommenders using Mab2Rec.

Documentation is available at fidelity.github.io/mab2rec.

Usage Patterns

Mab2Rec supports prototyping with a single bandit algorithm or benchmarking with multiple bandit algorithms. If you are new user, the best place to start is to experiment with multiple bandits using the tutorial notebooks.

Quick Start

Single Recommender

# Example of how to train an singler recommender to generate top-4 recommendations

# Import 
from mab2rec import BanditRecommender, LearningPolicy
from mab2rec.pipeline import train, score

# LinGreedy recommender to select top-4 items with 10% random exploration 
rec = BanditRecommender(LearningPolicy.LinGreedy(epsilon=0.1), top_k=4)

# Train on (user, item, response) interactions in train data using user features 
train(rec, data='data/data_train.csv', 
      user_features='data/features_user.csv')

# Score recommendations for users in test data. The output df holds 
# user_id, item_id, score columns for every test user for top-k items 
df = score(rec, data='data/data_test.csv', 
           user_features='data/features_user.csv')

Multiple Recommenders

# Example of how to benchmark multiple recommenders to generate top-4 recommendations

from mab2rec import BanditRecommender, LearningPolicy
from mab2rec.pipeline import benchmark
from jurity.recommenders import BinaryRecoMetrics, RankingRecoMetrics

# Recommenders (many more available)
recommenders = {"Random": BanditRecommender(LearningPolicy.Random()),
                "Popularity": BanditRecommender(LearningPolicy.Popularity()),
                "LinGreedy": BanditRecommender(LearningPolicy.LinGreedy(epsilon=0.1))}

# Column names for the response, user, and item id columns
metric_params = {'click_column': 'score', 'user_id_column': 'user_id', 'item_id_column':'item_id'}

# Performance metrics for benchmarking (many more available)
metrics = []
for top_k in [3, 5, 10]:
    metrics.append(BinaryRecoMetrics.CTR(**metric_params, k=top_k))
    metrics.append(RankingRecoMetrics.NDCG(**metric_params, k=top_k))

# Benchmarking with a collection of recommenders and metrics 
# This returns two dictionaries; 
# reco_to_results: recommendations for each algorithm on cross-validation data
# reco_to_metrics: evaluation metrics for each algorithm
reco_to_results, reco_to_metrics = benchmark(recommenders,
                                             metrics=metrics,
                                             train_data="data/data_train.csv",
                                             cv=5,
                                             user_features="data/features_user.csv")

Usage Examples

We provide extensive tutorials in the notebooks folder with guidelines on building recommenders, performing model selection, and evaluating performance.

  1. Data Overview: Overview of data required to train recommender.
  2. Feature Engineering: Creating user and item features from structured, unstructured, and sequential data.
  3. Model Selection: Model selection by benchmarking recommenders using cross-validation.
  4. Evaluation: Benchmarking of selected recommenders and baselines on test data with detailed evaluation.
  5. Advanced: Demonstration of advanced functionality such as persistency, eligibility, item availability, and memory efficiency.

Installation

Mab2Rec requires Python 3.8+ and can be installed from PyPI using pip install mab2rec or by building from source as shown in installation instructions.

Support

Please submit bug reports and feature requests as Issues.

License

Mab2Rec is licensed under the Apache License 2.0.


Metadata

Release files for mab2rec 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mab2rec 1.3.1
File Size Uploaded Provenance
mab2rec-1.3.1.tar.gz 35.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mab2rec 1.3.1
File Interpreter ABI Platform
mab2rec-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 61.6 kB

Release files / mab2rec-1.3.1.tar.gz

Download URL mab2rec-1.3.1.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cff12f5834093d55efa225087ee7bfeaa726187dbd6c2dc0aef0bf064da03d9a
BLAKE2b-256 checksum
How to use checksums
8014a84d631d73e40e40eb17023cba1589144f3210b67a7c4023f40ccab33676
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.19

Release files / mab2rec-1.3.1-py3-none-any.whl

Download URL mab2rec-1.3.1-py3-none-any.whl
Size 25.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b46cb504f863e7b13eb036cb2b7be35d0eb228e7690eb1c47d6cab1c0e77cd96
BLAKE2b-256 checksum
How to use checksums
76491fc02014e2bca7bac777e227d0b1adf475e300176e269ac52f46d03f2c52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.19

Security for mab2rec 1.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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