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.3.tar.gz (141.1 kB view details)

Uploaded Source

Built Distributions

irspack-0.2.3-cp310-cp310-win_amd64.whl (573.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

irspack-0.2.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

irspack-0.2.3-cp310-cp310-macosx_10_9_universal2.whl (1.3 MB view details)

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

irspack-0.2.3-cp39-cp39-win_amd64.whl (568.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

irspack-0.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

irspack-0.2.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

irspack-0.2.3-cp39-cp39-macosx_10_9_universal2.whl (1.3 MB view details)

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

irspack-0.2.3-cp38-cp38-win_amd64.whl (573.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

irspack-0.2.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

irspack-0.2.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

irspack-0.2.3-cp38-cp38-macosx_10_9_x86_64.whl (728.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

irspack-0.2.3-cp37-cp37m-win_amd64.whl (574.8 kB view details)

Uploaded CPython 3.7m Windows x86-64

irspack-0.2.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.4 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

irspack-0.2.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (11.1 MB view details)

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

irspack-0.2.3-cp37-cp37m-macosx_10_9_x86_64.whl (721.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

irspack-0.2.3-cp36-cp36m-win_amd64.whl (578.4 kB view details)

Uploaded CPython 3.6m Windows x86-64

File details

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

File metadata

  • Download URL: irspack-0.2.3.tar.gz
  • Upload date:
  • Size: 141.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.3.tar.gz
Algorithm Hash digest
SHA256 85839567ead4462244851600840a4d99674db60e112c1ac7e6dbeaa5487e5ae3
MD5 3a5c7da5160710dd9a56beaaa9d89747
BLAKE2b-256 76a6cf54dcd820036a91296d67805a8e57b2d07e963bb4bc45ec25ac0d6fa56d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: irspack-0.2.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 573.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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3a51596330032466b34dccc59a41ffd68522046fd799fef4d46d0993237ca688
MD5 b94b38ccff3e18c31762b129531fce3c
BLAKE2b-256 f3222a97a66cf5737f8fe03c981c2f48f53786e1e038028c717518959cfff058

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e90265d28098c58bb15372082b1624d6037831cd1bf76296f1e95621d573e107
MD5 4c9f4495f604032c2ea43aa5618715bc
BLAKE2b-256 5cfd08f8e45839f338d89b0a28c2e6fc90f67f40e99651e7a534551b585cab88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 75708091b3b2a5e63fad719718dbf347cadde27e3632bd651a6de1086e29003f
MD5 4fb6ac4e1491801a916afbba1ed35df5
BLAKE2b-256 c769d81ed32cc49a5ef6d4584ffbaa3884254163f47e26fada9942ad4a80cd33

See more details on using hashes here.

File details

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

File metadata

  • Download URL: irspack-0.2.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 568.0 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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bdd689308b514882fd9de2ff53f5a87a5cffe88bdbb3f48ef0233f9f930aaa80
MD5 8cdfe222b4a675bf0aa64212880a9cf6
BLAKE2b-256 cec3ea51f81999c361fda6805942c5491b5e9348204c290b8181d64934acf586

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f8748990e04a1d170c72bdbcafb3abdbc750b9c8dae50ac7178c631fc3979582
MD5 4fe75274096be767be0e69f882b6ac3e
BLAKE2b-256 bd86bfcb6988d24ba5721750cbdbc561dc0c7531903d789e345c9bcb62a9fb1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 006dafb9e6c9972a4064132b01389b1a5f1f55b702665113f42ab7186a4299e8
MD5 620c314458ad995fde2229df0dd8f78d
BLAKE2b-256 dd4e12c3a93da1f8709d32cacc19b57b89379c8032c330ab37ac753be9407068

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ae8e1f8fc1f206831a97c25e3d1b03499c4fdc8282c73ed97f29609e205327f9
MD5 e463cbf4389d456c658e9253f07f8dd7
BLAKE2b-256 cacba61dd3df2c3d548cb1ecb381c5791feb617571ecfa94a08924f472babf54

See more details on using hashes here.

File details

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

File metadata

  • Download URL: irspack-0.2.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 573.3 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.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b89ce81723329754669adf042b017c11f11ef0f304d2694849abf9f18ec55e01
MD5 02cb651ec0f0dfc3d8d13f31cd51b667
BLAKE2b-256 08360d3ba5b21f35405e1016eefc96d33690aeae2296a27cedb676235a4dadbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 72d8f05f96e9c68d8713eaabb702e84f5063a5deb1c920306073c4ee16a4a60b
MD5 058f5a4572196429dca3f3ad86e742e0
BLAKE2b-256 4018c77a207f7c3538ec981ed752ad07e9c2c3a1db18e955806ea0c553ab7077

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8c7dee5ea2901a1ea16d4561399c9b511f5fba513752357166ae7c0e217aabbd
MD5 80209dc2002466d359eaeae024b2b7da
BLAKE2b-256 e90c179c268c63765ac265aea124601c41d9a634e747bbc3b8dfcc0ce37d62b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b17dfec46d069a6a8168958fc440ab3ff9a6b022996af963244a41a5da3b87c0
MD5 e29f17429fb2e5cc9fcab1412191bd59
BLAKE2b-256 187eaa41981b5979d3a5765db5eb86a76a6cae069af5e16661c9ccb5f9f59b95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: irspack-0.2.3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 574.8 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.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5f2271482b228974fa7fe20abd6e054d967040f3463ec6d928a8f560cd49a2e7
MD5 a26246b1f74dbd8401775d980b486dfa
BLAKE2b-256 c7711556ef92b6239b9c75892fa8a2719a6b2cf05347410d6b9639a17998b418

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 19be0e32a9c3a1965b92a0115c168d34d8b9c6b183844918f04e317a04df4877
MD5 1da7cc04a9470084f8fbc1105ab69970
BLAKE2b-256 e4eb002c48e433a9300bd7244815f7a900a4ff1dc5e95c0d1a2ad9998a81bbdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 aab97dbafcac138d829663623f957692f351ccc6ba8a283ad1be821b3287b347
MD5 045ed043c1e5eae749b41b7cc0d64314
BLAKE2b-256 9a3e08420fd952dd0eaea8017c630c3f1b4636e9722eacf1d70d28395418f6ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for irspack-0.2.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9f9a97c1757e7d1b3184702ee62d4ad4c28ac550417abb2c5b09eeaf1436a301
MD5 c3dc3e229b4884752e37e6bca8389b50
BLAKE2b-256 e472cd9d0342b21960fbef827bc99acb8a8261619e1add0d85187195f26c4027

See more details on using hashes here.

File details

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

File metadata

  • Download URL: irspack-0.2.3-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 578.4 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.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 63cd5adaba8ce402c2e973ec1400956c02ae30110292f90f691386a3bf7a62cc
MD5 4440109958cef7e8e63d5708be2abe6b
BLAKE2b-256 0298921e149ceec554cbf02b1b87fc9ba5c0ce51a09927649e2d204d53878dc8

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