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, 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 import IALSRecommender, df_to_sparse
from irspack.dataset.movielens import MovieLens100KDataManager

df = MovieLens100KDataManager().read_interaction()

# Convert pandas.Dataframe into scipy's sparse matrix.
# The i'th row of `X_interaction` corresponds to `unique_user_id[i]`
# and j'th column of `X_interaction` corresponds to `unique_movie_id[j]`.
X_interaction, unique_user_id, unique_movie_id = df_to_sparse(
  df, 'userId', 'movieId'
)

recommender = IALSRecommender(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_state=0
)

evaluator = Evaluator(ground_truth=X_val)

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

This will print something like

{
    'appeared_item': 435.0,
    'entropy': 5.160409123151053,
    'gini_index': 0.9198367595008214,
    'hit': 0.40084835630965004,
    'map': 0.013890322881619916,
    'n_items': 1682.0,
    'ndcg': 0.07867240014767263,
    'precision': 0.06797454931071051,
    'recall': 0.03327028758587522,
    '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 IALSOptimizer

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

# maximal ndcg around 0.43 ~ 0.45
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.2.4.tar.gz (140.1 kB view details)

Uploaded Source

Built Distributions

irspack-0.2.4-cp310-cp310-win_amd64.whl (565.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

irspack-0.2.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (791.3 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

irspack-0.2.4-cp310-cp310-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

irspack-0.2.4-cp39-cp39-win_amd64.whl (561.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

irspack-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (847.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

irspack-0.2.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (792.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

irspack-0.2.4-cp39-cp39-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

irspack-0.2.4-cp38-cp38-win_amd64.whl (565.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

irspack-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (845.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

irspack-0.2.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (790.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

irspack-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl (629.8 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

irspack-0.2.4-cp37-cp37m-win_amd64.whl (567.5 kB view details)

Uploaded CPython 3.7m Windows x86-64

irspack-0.2.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (810.6 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

irspack-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (835.2 kB view details)

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

irspack-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl (623.0 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

irspack-0.2.4-cp36-cp36m-win_amd64.whl (572.3 kB view details)

Uploaded CPython 3.6m Windows x86-64

File details

Details for the file irspack-0.2.4.tar.gz.

File metadata

  • Download URL: irspack-0.2.4.tar.gz
  • Upload date:
  • Size: 140.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4.tar.gz
Algorithm Hash digest
SHA256 cee928167e20c5c292b04accc07f88079459d3313b38ed05cfa73dc16fbe1f2b
MD5 61bef17b414e3ed45656bb01b616a5ba
BLAKE2b-256 3fa205c1dcca89933e641454c2d7c6ab40468fcac2da82d47bfc3c9d8109ab27

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: irspack-0.2.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 565.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 817991dcb8ca9834e25632e4343bd45abd4f08c8528800892303f595bd572e8c
MD5 047397b98e58fef1dd9e764d759a4d36
BLAKE2b-256 4140e06e7b752750e3c00b7ad95eafe8a0338be8b8093d8e0b86db8ab7fd9f58

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7208ca22d9c39848469a13ca5a5afad79a564091aa102342af2f24a3719db740
MD5 b7ec71cbb1e063f1db983bc6d0c499d9
BLAKE2b-256 b764e5c77f89f33f83bd6538c15b8349c78ee08fa31189efd0e25172b2dd5213

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 58d1a2581fe9b2a8f73203425bbb02ae7ec9b3c317d91787e59976b7b93ea157
MD5 7f085b8e673eb86282e3a2cb3ef66be4
BLAKE2b-256 f2fe65efcae250e3df240a2ce96812cdc90994180e8d49b1a535b5b6a1307cd4

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: irspack-0.2.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 561.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0ddcf4135132e79caff46ec1561d0e12c2d520b4a8923373eb72e060471563a2
MD5 1fa4eeebfc05293516db6b8fef7d007d
BLAKE2b-256 b7fea2f0af7e44edff4e157d85a8e3cc36b95b4d5694133a8fe5a799c24d2908

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1334f393d768336bf724fd3e1adc9be0beeb66ce90922331a38943278e85515c
MD5 a5adba6a637f4faacd4fb00171834f5c
BLAKE2b-256 7dc2dc66fa2d62dd123fb778381dac61a3f7b03049249083842de1903d9f28d8

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0e6b95bdcae5211ef4bbfc52161a145a6c616648297ba052d3f11b285fb3d39a
MD5 7bd273da60ac9dbf9671c2dbbe84001b
BLAKE2b-256 a708cc8f2c804217f5ad29bff58b4758bfb4fb7fad712fcd9f963f4f1b20729c

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 09b7d7259e3a04bc7e654ae653b9b73b2a4e63040d349971e36bf1fbba83e134
MD5 719d2b9d3fb9b1d39695f519137480a5
BLAKE2b-256 6dd0d48d99ebeba904d4ee3919037bcfb585a1528c151841e220f13c5e738076

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: irspack-0.2.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 565.5 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 ec526d61deacb9e10418e195364b32c15d6bc853149097a1d0341ce61c8838f6
MD5 78ab56583ff602b3e92467571b92ea94
BLAKE2b-256 4321c2f3aadbeb530769490633b8506c80ee01f6a46b6bea520fb14d960114fe

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a2d5c06bd0cc7d10d9e7c08a90d7bb3bbf4dbddef6a7929f3cc98fedfcef0b92
MD5 219e45906e4d1d7d22b230557983eebb
BLAKE2b-256 680bbd14060f22554deb10293e1be3eff8ed81bd6510e1fdbd9ca03f6ef8ba43

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cf54603689dc7620dc71e3f67b1717a53f99fb4aed0d4264319915242f5e6da1
MD5 0251ffaea6810164b68ea8e0dbaf0254
BLAKE2b-256 8bb61ca7e3b4ecc73599279803bd40532a055b32023f0c3992dc0d5d899cc770

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3b2b3695ce0f404160a7c1c3e8e8bd2c4c727b681c3f28e12d5d80608c8c7693
MD5 730217d03bc0118dddf9eda88daaa0ec
BLAKE2b-256 07a78fd59e7ccb6083f84894d305bc6c5dffb15adf569365d65b942e273536d7

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: irspack-0.2.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 567.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 30e9afc636f2104ba4d065140a6b1c025ff2870c2095d47b4177b40c2695bf91
MD5 b0e5f82223adb06f47d77c332d76ceeb
BLAKE2b-256 ab811dc816def2ddaf63a011fb8f9ed5e17ff2f1937a2e6c3a03d686b3886798

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c0db8c2f69beb33135e32572dc822de5833482fdeae2be8c3f21490f54f38c90
MD5 59dba30cf746c8bfa6defbd6abc48213
BLAKE2b-256 cd9cb41a21dfff1e4748aca9800aff65462e297465f2360df6e6bb9ab6c4fe07

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1b398b09fd2b7b997950358a1cfe7a90524e7f9bf0d2b3efc91b7be2213daf3d
MD5 fb793663b6c360c8fedd8761f9213f9b
BLAKE2b-256 4873c6948b44203d4b47674116d324fc30d84879e3276b642fb19cc8d7fed5ff

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for irspack-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3874b85d5cc6f1f9576e19ef35daff50574d8b6ea18fc98f7301e81afd085e42
MD5 577262775be8978ed205ee26a116c1f7
BLAKE2b-256 294f6a9b05a8f6e7bfd44cb7e2246cca6d7373ce37f4e58d698e8a9d15cd156c

See more details on using hashes here.

File details

Details for the file irspack-0.2.4-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: irspack-0.2.4-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 572.3 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for irspack-0.2.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 2f9d8096104029d5d5164596b5b8f3b4e94aab2acf372819a65df020ca687132
MD5 409ce5b2f3f8dfa59870b67584febad8
BLAKE2b-256 9fdc69a23029b786dbf884b928a49b24e1a561e3d61cc42c99ca6801f84cb785

See more details on using hashes here.

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