rtrec: Realtime Recommendation Library in Python
A realtime recommendation system supporting online updates.
Highlights
- ❇️ Supporting online updates.
- ⚡️ Fast implementation (>=190k samples/sec training on laptop).
- ◍ efficient sparse data support.
- 🕑 decaying weights of user-item interactions based on recency.
- 🔄 Hybrid CF(Collaborative-Filtering)/CB(Content-based) model for cold-start users.
experimental Rust implementation
Supported Recommendation Algorithims
- Sparse SLIM with time-weighted interactions.
- Factorization Machines using LightFM
- Hybrid model of SLIM (CF) and Factorization Machines (CB). Based on the number of user-item interactions, balances prediction weights between CF and CB.
Installation
# using pip
pip install rtrec
pip install rtrec[serving]
# using uv
uv add rtrec
uv sync --no-extra serving
uv add rtrec[serving]
uv sync --extra serving
Usage
Find usages in notebooks/examples.
Examples using Raw-level APIs
# Dataset consists of user, item, tstamp, rating
import time
current_unixtime = time.time()
interactions = [('user_1', 'item_1', current_unixtime, 5.0),
('user_2', 'item_2', current_unixtime, -2.0),
('user_2', 'item_1', current_unixtime, 3.0),
('user_2', 'item_4', current_unixtime, 3.0),
('user_1', 'item_3', current_unixtime, 4.0)]
# Fit SLIM model
from rtrec.models import SLIM
model = SLIM()
model.fit(interactions)
# can fit from streams using yield as follows:
def yield_interactions():
for interaction in interactions:
yield interaction
model.fit(yield_interactions())
# Recommend top-5 items for a user
recommendations = model.recommend('user_1', top_k=5)
assert recommendations == ["item_4", "item_2"]
Examples using high level DataFrame APIs
# load dataset
from rtrec.experiments.datasets import load_dataset
df = load_dataset(name='movielens_1m')
# Split data set by temporal user split
from rtrec.experiments.split import temporal_user_split
train_df, test_df = temporal_user_split(df)
# Initialize SLIM model with custom options
from rtrec.recommender import Recommender
from rtrec.models import SLIM
model = SLIM(min_value=0, max_value=15, decay_in_days=180, nn_feature_selection=50)
recommender = Recommender(model)
# Bulk fit
recommender.bulk_fit(train_df)
# Partial fit
from rtrec.experiments.split import temporal_split
test_df1, test_df2 = temporal_split(test_df, test_frac=0.5)
recommender.fit(test_df1, update_interaction=True, parallel=True)
# Evaluation
metrics = recommender.evaluate(test_df2, recommend_size=10, filter_interacted=True)
print(metrics)
# User to Item Recommendation
recommended = recommender.recommend(user=10, top_k=10, filter_interacted=True)
assert len(recommended) == 10
# Item to Item recommendation
similar_items = recommender.similar_items(query_items=[3,10], top_k=5)
assert len(similar_items) == 2
Metadata
Release files for rtrec 0.2.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rtrec-0.2.7.tar.gz | 75.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rtrec-0.2.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 136.8 kB
Release files / rtrec-0.2.7.tar.gz
| Download URL | rtrec-0.2.7.tar.gz |
|---|---|
| Size | 75.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
842ffc2e84942fda4041b3bdf0b70666890764e73e7f5b359d8f1968aabee60e
|
|
BLAKE2b-256 checksum How to use checksums |
56a25870ae477eca217fe936b64e67dc8e08bb4151bd0119fe508d5f1e48189b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|
Release files / rtrec-0.2.7-py3-none-any.whl
| Download URL | rtrec-0.2.7-py3-none-any.whl |
|---|---|
| Size | 61.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a442ad2c2db0b6dc907205796dcbe96a4bcc79fed97656dfdc1eb49018164db5
|
|
BLAKE2b-256 checksum How to use checksums |
c6f0727fb8c313f09fe944ca2e69cba0bc2c5f3426c499ba2d885ca6562eb3cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|