Recency-Frequency based recommendation scoring
Project description
rfscorer
rfscorer is a Python package for Recency-Frequency based recommendation scoring.
It estimates revisit probabilities — the preference score for each user-item pair, forming a matrix analogous to a rating matrix — from interaction histories, using two simple but powerful behavioral signals: recency, which captures how recently a user interacted with an item, and frequency, which captures how often the user has interacted with it.
The package is designed for product recommendation and revisit modeling, especially in settings where interpretable scoring based on interaction history is preferred over black-box recommendation models.
Note: In this package, RF stands for Recency-Frequency, not Random Forest.
Features
- scikit-learn-style API — familiar
fit()/transform()interface makes it easy to integrate into existing data science workflows - Minimal data requirements — works with any interaction log that has three columns:
user,item, anddatetime; no ratings or explicit feedback needed - Explainable scoring — probabilities are derived through mathematical optimization under RF monotonicity constraints, making every score fully traceable and auditable; 3D surface visualization further supports intuitive understanding
- Probabilistic output — revisit probabilities serve as preference scores, enabling expected value calculations and probabilistic ranking of recommendations
- Extensible — the user–item probability matrix produced by
transform()can be directly used as input to collaborative filtering or other downstream recommendation models
Installation
pip install rfscorer
Usage
from rfscorer import RecencyFrequencyScorer
Prepare an interaction log with at least three columns: user ID, item ID, and timestamp. Split it into a training set and a test set.
df_train = ... # training interaction log (columns: user, item, datetime)
df_test = ... # test interaction log (columns: user, item, datetime)
| user | item | datetime |
|---|---|---|
| u_001 | i_032 | 2026-07-01 |
| u_001 | i_017 | 2026-07-03 |
| u_001 | i_032 | 2026-07-05 |
| u_002 | i_011 | 2026-07-02 |
| u_002 | i_058 | 2026-07-04 |
The same user-item pair may appear multiple times, representing repeat visits.
Instantiate the scorer, specifying the column names if they differ from the defaults (user, item, datetime).
scorer = RecencyFrequencyScorer()
Call fit() to estimate empirical revisit probabilities from the training log.
Pass target_date as the split point: data up to target_date forms the observation window (default: 28 days back), and data after target_date forms the evaluation window (default: 7 days forward).
scorer.fit(df_train, target_date="2026-07-07")
The empirical surface reflects raw revisit rates and may be irregular due to sparse data.
Optionally, call optimize() to smooth the surface under RF monotonicity constraints using convex quadratic programming.
kind="mono" enforces recency and frequency monotonicity.
scorer.optimize(kind="mono")
kind="mcc" additionally adds convexity in recency and concavity in frequency, yielding a smoother surface.
scorer.optimize(kind="mcc")
Call transform() to score each user-item pair in the test log.
It returns a DataFrame with columns user, item, recency, frequency, probability, and order (rank within each user, sorted by probability descending).
Pass kind="empirical", kind="mono", or kind="mcc" to select which probabilities to use.
df_rec_mcc = scorer.transform(df_test, target_date="2026-07-07", kind="mcc")
| user | item | recency | frequency | probability | order |
|---|---|---|---|---|---|
| u_001 | i_032 | 1 | 4 | 0.1167 | 1 |
| u_001 | i_017 | 2 | 3 | 0.0789 | 2 |
| u_001 | i_045 | 3 | 1 | 0.0248 | 3 |
| u_002 | i_011 | 1 | 2 | 0.0621 | 1 |
| u_002 | i_058 | 4 | 1 | 0.0182 | 2 |
Within each user, rows are sorted by probability descending; order represents the recommendation rank.
Examples
- examples/basic_usage.ipynb — end-to-end walkthrough: load data, fit, optimize, transform, and evaluate
References
Citation
If you use rfscorer in academic work, please cite the following paper:
@article{Iwanaga2016,
author = {Jiro Iwanaga and Naoki Nishimura and Noriyoshi Sukegawa and Yuichi Takano},
title = {Estimating product-choice probabilities from recency and frequency of page views},
journal = {Knowledge-Based Systems},
volume = {99},
pages = {157--167},
year = {2016},
url = {https://www.sciencedirect.com/science/article/abs/pii/S0950705116000848}
}
If you additionally use the probability matrix as input to a collaborative filtering model, please also cite:
@article{Iwanaga2019,
author = {Jiro Iwanaga and Naoki Nishimura and Noriyoshi Sukegawa and Yuichi Takano},
title = {Improving collaborative filtering recommendations by estimating user preferences from clickstream data},
journal = {Electronic Commerce Research and Applications},
volume = {37},
pages = {100877},
year = {2019},
url = {https://www.sciencedirect.com/science/article/abs/pii/S1567422319300547}
}
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rfscorer-0.2.6.tar.gz.
File metadata
- Download URL: rfscorer-0.2.6.tar.gz
- Upload date:
- Size: 749.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de7ff9b31619b28cd3f5b58abe384c96044bfb1b1f2e8169b2bd6accccc605d4
|
|
| MD5 |
06c31b16040fd2310e955b30ac644df1
|
|
| BLAKE2b-256 |
5b3ca977b748b1d980d668a8a048d0be1b220171720803ba75f13188b506913f
|
File details
Details for the file rfscorer-0.2.6-py3-none-any.whl.
File metadata
- Download URL: rfscorer-0.2.6-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0115d66886a492b08c02384e8481a5c13112023705320b678664e3ae2666154
|
|
| MD5 |
17db05bd34c9b1c540267231466fe600
|
|
| BLAKE2b-256 |
5571098329c3b069baf2da7ae08251bb9e7b508cd803c0c930d3060b2d899ddd
|