Skip to main content

Implicit feedback-based recommender system pack

Project description

irspack

Python pypi GitHub license Build Read the Docs codecov

Docs

irspack is a Python package to train, evaluate, and optimize recommender systems based on implicit feedback.

There are already great packages for this purpose like

However, I decided to implement my own one to

  • Use optuna for more efficient parameter search. In particular, if an early stopping scheme is available, optuna can prune unpromising trial based on the intermediate validation score, which drastically reduces overall running time for tuning.
  • Use multi-threaded implementations wherever possible. Currently, several important algorithms (KNN, iALS, SLIM) and performance evaluators are parallelized using C++ thread.

Installation & Optional Dependencies

There are binaries for Linux, MacOS, and Windows with python>=3.6 with x86 architectures. You can install them via

pip install irspack

The binaries have been compiled to use AVX instruction. If you want to use AVX2/AVX512 or your environment does not support AVX (like Rosetta 2 on Apple M1), install it from source like

CFLAGS="-march=native" pip install git+https://github.com/tohtsky/irspack.git

In that case, you must have a decent version of C++ compiler (with C++11 support). If it doesn't work, feel free to make an issue!

Optional Dependencies

I have also prepared a wrapper class (BPRFMRecommender) to train/optimize BPR/warp loss Matrix factorization implemented in lightfm. To use it you have to install lightfm separately, e.g. by

pip install lightfm

If you want to use Mult-VAE and CB2CF features in cold-start scenarios, you'll need the following additional (pip-installable) packages:

Basic Usage

Step 1. Train a recommender

We first represent the user/item interaction as a scipy.sparse matrix. Then we can feed it into our Recommender classes:

import numpy as np
import scipy.sparse as sps
from irspack.recommenders import P3alphaRecommender
from irspack.dataset.movielens import MovieLens100KDataManager

df = MovieLens100KDataManager().read_interaction()
unique_user_id, user_index = np.unique(df.userId, return_inverse=True)
unique_movie_id, movie_index = np.unique(df.movieId, return_inverse=True)
X_interaction = sps.csr_matrix(
  (np.ones(df.shape[0]), (user_index, movie_index))
)

recommender = P3alphaRecommender(X_interaction)
recommender.learn()

# for user 0 (whose userId is unique_user_id[0]),
# compute the masked score (i.e., already seen items have the score of negative infinity)
# of items.
recommender.get_score_remove_seen([0])

Step 2. Evaluate on a validation set

We have to split the dataset to train and validation sets

from irspack.split import rowwise_train_test_split
from irspack.evaluator import Evaluator

# Random split
X_train, X_val = rowwise_train_test_split(
    X_interaction, test_ratio=0.2, random_seed=0
)

evaluator = Evaluator(ground_truth=X_val)

recommender = P3alphaRecommender(X_train)
recommender.learn()
evaluator.get_score(recommender)

This will print something like

{
  'appeared_item': 106.0,
  'entropy': 3.840445116672292,
  'gini_index': 0.9794929280523742,
  'hit': 0.8854718981972428,
  'map': 0.11283343078231302,
  'n_items': 1682.0,
  'ndcg': 0.3401244303579389,
  'precision': 0.27560975609756017,
  'recall': 0.19399215770339678,
  'total_user': 943.0,
  'valid_user': 943.0
}

Step 3. Optimize the Hyperparameter

Now that we can evaluate the recommenders' performance against the validation set, we can use optuna-backed hyperparameter optimizer.

from irspack.optimizers import P3alphaOptimizer

optimizer = P3alphaOptimizer(X_train, evaluator)
best_params, trial_dfs  = optimizer.optimize(n_trials=20)

# maximal ndcg around 0.38 ~ 0.39
trial_dfs["ndcg@10"].max()

Of course, we have to hold-out another interaction set for test, and measure the performance of tuned recommender against the test set. See examples/ for more complete examples.

TODOs

  • more benchmark dataset

Project details


Download files

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

Source Distribution

irspack-0.1.19.tar.gz (146.5 kB view hashes)

Uploaded Source

Built Distributions

irspack-0.1.19-cp39-cp39-win_amd64.whl (546.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

irspack-0.1.19-cp39-cp39-win32.whl (489.2 kB view hashes)

Uploaded CPython 3.9 Windows x86

irspack-0.1.19-cp39-cp39-manylinux2010_x86_64.whl (9.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

irspack-0.1.19-cp39-cp39-manylinux2010_i686.whl (9.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

irspack-0.1.19-cp39-cp39-macosx_10_9_x86_64.whl (666.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

irspack-0.1.19-cp38-cp38-win_amd64.whl (550.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

irspack-0.1.19-cp38-cp38-win32.whl (488.9 kB view hashes)

Uploaded CPython 3.8 Windows x86

irspack-0.1.19-cp38-cp38-manylinux2010_x86_64.whl (9.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

irspack-0.1.19-cp38-cp38-manylinux2010_i686.whl (9.5 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

irspack-0.1.19-cp38-cp38-macosx_10_9_x86_64.whl (665.9 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

irspack-0.1.19-cp37-cp37m-win_amd64.whl (552.1 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

irspack-0.1.19-cp37-cp37m-win32.whl (491.2 kB view hashes)

Uploaded CPython 3.7m Windows x86

irspack-0.1.19-cp37-cp37m-manylinux2010_x86_64.whl (9.9 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

irspack-0.1.19-cp37-cp37m-manylinux2010_i686.whl (9.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

irspack-0.1.19-cp37-cp37m-macosx_10_9_x86_64.whl (661.0 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

irspack-0.1.19-cp36-cp36m-win_amd64.whl (552.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

irspack-0.1.19-cp36-cp36m-win32.whl (491.1 kB view hashes)

Uploaded CPython 3.6m Windows x86

irspack-0.1.19-cp36-cp36m-manylinux2010_x86_64.whl (9.9 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

irspack-0.1.19-cp36-cp36m-manylinux2010_i686.whl (9.7 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

irspack-0.1.19-cp36-cp36m-macosx_10_9_x86_64.whl (660.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page