Skip to main content

A collaborative-filtering and content-based recommender system for both explicit and implicit datasets.

Project description

LibRecommender

Build CI codecov pypi Downloads python versions Codacy Badge Code style: black platform visitors License

Overview

LibRecommender is an easy-to-use recommender system focused on end-to-end recommendation. The main features are:

  • Implemented a number of popular recommendation algorithms such as FM, DIN, LightGCN etc, see full algorithm list.
  • A hybrid recommender system, which allows user to use either collaborative-filtering or content-based features. New features can be added on the fly.
  • Low memory usage, automatically convert categorical and multi-value categorical features to sparse representation.
  • Support training for both explicit and implicit datasets, and negative sampling can be used for implicit dataset.
  • Provide end-to-end workflow, i.e. data handling / preprocessing -> model training -> evaluate -> serving.
  • Support cold-start prediction and recommendation.
  • Provide unified and friendly API for all algorithms. Easy to retrain model with new users/items.

Usage

pure collaborative-filtering example :

import numpy as np
import pandas as pd
from libreco.data import random_split, DatasetPure
from libreco.algorithms import SVDpp  # pure data, algorithm SVD++
from libreco.evaluation import evaluate

data = pd.read_csv("examples/sample_data/sample_movielens_rating.dat", sep="::",
                   names=["user", "item", "label", "time"])

# split whole data into three folds for training, evaluating and testing
train_data, eval_data, test_data = random_split(data, multi_ratios=[0.8, 0.1, 0.1])

train_data, data_info = DatasetPure.build_trainset(train_data)
eval_data = DatasetPure.build_evalset(eval_data)
test_data = DatasetPure.build_testset(test_data)
print(data_info)   # n_users: 5894, n_items: 3253, data sparsity: 0.4172 %

svdpp = SVDpp(task="rating", data_info=data_info, embed_size=16, n_epochs=3, lr=0.001,
              reg=None, batch_size=256)
# monitor metrics on eval_data during training
svdpp.fit(train_data, verbose=2, eval_data=eval_data, metrics=["rmse", "mae", "r2"])

# do final evaluation on test data
print("evaluate_result: ", evaluate(model=svdpp, data=test_data,
                                    metrics=["rmse", "mae"]))
# predict preference of user 2211 to item 110
print("prediction: ", svdpp.predict(user=2211, item=110))
# recommend 7 items for user 2211
print("recommendation: ", svdpp.recommend_user(user=2211, n_rec=7))

# cold-start prediction
print("cold prediction: ", svdpp.predict(user="ccc", item="not item",
                                         cold_start="average"))
# cold-start recommendation
print("cold recommendation: ", svdpp.recommend_user(user="are we good?",
                                                    n_rec=7,
                                                    cold_start="popular"))

include features example :

import numpy as np
import pandas as pd
from libreco.data import split_by_ratio_chrono, DatasetFeat
from libreco.algorithms import YouTubeRanking  # feat data, algorithm YouTubeRanking

data = pd.read_csv("examples/sample_data/sample_movielens_merged.csv", sep=",", header=0)
data["label"] = 1  # convert to implicit data and do negative sampling afterwards

# split into train and test data based on time
train_data, test_data = split_by_ratio_chrono(data, test_size=0.2)

# specify complete columns information
sparse_col = ["sex", "occupation", "genre1", "genre2", "genre3"]
dense_col = ["age"]
user_col = ["sex", "age", "occupation"]
item_col = ["genre1", "genre2", "genre3"]

train_data, data_info = DatasetFeat.build_trainset(
    train_data, user_col, item_col, sparse_col, dense_col
)
test_data = DatasetFeat.build_testset(test_data)
train_data.build_negative_samples(data_info)  # sample negative items for each record
test_data.build_negative_samples(data_info)
print(data_info)  # n_users: 5962, n_items: 3226, data sparsity: 0.4185 %

ytb_ranking = YouTubeRanking(task="ranking", data_info=data_info, embed_size=16,
                             n_epochs=3, lr=1e-4, batch_size=512, use_bn=True,
                             hidden_units="128,64,32")
ytb_ranking.fit(train_data, verbose=2, shuffle=True, eval_data=test_data,
                metrics=["loss", "roc_auc", "precision", "recall", "map", "ndcg"])

# predict preference of user 2211 to item 110
print("prediction: ", ytb_ranking.predict(user=2211, item=110))
# recommend 7 items for user 2211
print("recommendation(id, probability): ", ytb_ranking.recommend_user(user=2211, n_rec=7))

# cold-start prediction
print("cold prediction: ", ytb_ranking.predict(user="ccc", item="not item",
                                               cold_start="average"))
# cold-start recommendation
print("cold recommendation: ", ytb_ranking.recommend_user(user="are we good?",
                                                          n_rec=7,
                                                          cold_start="popular"))

For more examples and usages, see User Guide

Data Format

JUST normal data format, each line represents a sample. One thing is important, the model assumes that user, item, and label column index are 0, 1, and 2, respectively. You may wish to change the column order if that's not the case. Take for Example, the movielens-1m dataset:

1::1193::5::978300760
1::661::3::978302109
1::914::3::978301968
1::3408::4::978300275

Besides, if you want to use some other meta features (e.g., age, sex, category etc.), you need to tell the model which columns are [sparse_col, dense_col, user_col, item_col], which means all features must be in a same table. See above YouTubeRanking for example.

Also note that your data should not contain missing values.

Serving

For how to serve a trained model in LibRecommender, see Serving Guide .

Installation & Dependencies

From pypi :  

$ pip install LibRecommender

To build from source, you 'll first need Cython and Numpy:

$ # pip install numpy cython
$ git clone https://github.com/massquantity/LibRecommender.git
$ cd LibRecommender
$ python setup.py install

Basic Dependencies for libreco:

  • Python >= 3.6
  • TensorFlow >= 1.15
  • PyTorch >= 1.10
  • Numpy >= 1.19.5
  • Cython >= 0.29.0
  • Pandas >= 1.0.0
  • Scipy >= 1.2.1
  • scikit-learn >= 0.20.0
  • gensim >= 4.0.0
  • tqdm
  • nmslib (optional)

LibRecommender is tested under TensorFlow 1.15, 2.5, 2.8 and 2.10. If you encounter any problem during running, feel free to open an issue. If you are using Python 3.6, you also need to install dataclasses, since it is first included in Python 3.7.

Known issue: Sometimes one may encounter errors like ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject. In this case try upgrading numpy, and version 1.22.0 or higher is probably a safe option.

The table below shows some compatible version combinations:

Python Numpy TensorFlow OS
3.6 1.19.5 1.15, 2.5 linux, windows, macos
3.7 1.20.3, 1.21.6 1.15, 2.5, 2.8, 2.10 linux, windows, macos
3.8 1.22.4, 1.23.2 2.5, 2.8, 2.10 linux, windows, macos
3.9 1.22.4, 1.23.2 2.5, 2.8, 2.10 linux, windows, macos
3.10 1.22.4, 1.23.2 2.8, 2.10 linux, windows, macos

Optional Dependencies for libserving:

Docker

One can also use the library in a docker container without installing dependencies, see Docker.

References

Algorithm Category1 Backend Sequence2 Graph3 Embedding4 Paper
userCF / itemCF pure Cython Item-Based Collaborative Filtering Recommendation Algorithms
SVD pure TensorFlow1 :heavy_check_mark: Matrix Factorization Techniques for Recommender Systems
SVD++ pure TensorFlow1 :heavy_check_mark: Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model
ALS pure Cython :heavy_check_mark: 1. Matrix Completion via Alternating Least Square(ALS)
2. Collaborative Filtering for Implicit Feedback Datasets
3. Applications of the Conjugate Gradient Method for Implicit Feedback Collaborative Filtering
NCF pure TensorFlow1 Neural Collaborative Filtering
BPR pure Cython, TensorFlow1 :heavy_check_mark: BPR: Bayesian Personalized Ranking from Implicit Feedback
Wide & Deep feat TensorFlow1 Wide & Deep Learning for Recommender Systems
FM feat TensorFlow1 Factorization Machines
DeepFM feat TensorFlow1 DeepFM: A Factorization-Machine based Neural Network for CTR Prediction
YouTubeRetrieval feat TensorFlow1 :heavy_check_mark: :heavy_check_mark: Deep Neural Networks for YouTube Recommendations
YouTubeRanking feat TensorFlow1 :heavy_check_mark: Deep Neural Networks for YouTube Recommendations
AutoInt feat TensorFlow1 AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks
DIN feat TensorFlow1 :heavy_check_mark: Deep Interest Network for Click-Through Rate Prediction
Item2Vec pure / :heavy_check_mark: :heavy_check_mark: Item2Vec: Neural Item Embedding for Collaborative Filtering
RNN4Rec / GRU4Rec pure TensorFlow1 :heavy_check_mark: :heavy_check_mark: Session-based Recommendations with Recurrent Neural Networks
Caser pure TensorFlow1 :heavy_check_mark: :heavy_check_mark: Personalized Top-N Sequential Recommendation via Convolutional Sequence Embedding
WaveNet pure TensorFlow1 :heavy_check_mark: :heavy_check_mark: WaveNet: A Generative Model for Raw Audio
DeepWalk pure / :heavy_check_mark: :heavy_check_mark: DeepWalk: Online Learning of Social Representations
NGCF pure PyTorch :heavy_check_mark: :heavy_check_mark: Neural Graph Collaborative Filtering
LightGCN pure PyTorch :heavy_check_mark: :heavy_check_mark: LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation
GraphSage feat DGL, PyTorch :heavy_check_mark: :heavy_check_mark: Inductive Representation Learning on Large Graphs
PinSage feat DGL, PyTorch :heavy_check_mark: :heavy_check_mark: Graph Convolutional Neural Networks for Web-Scale Recommender Systems

[1] Category: pure means collaborative-filtering algorithms which only use behavior data, feat means other side-features can be included.

[2] Sequence: Algorithms that leverage user behavior sequence.

[3] Graph: Algorithms that leverage graph information, including Graph Embedding (GE) and Graph Neural Network (GNN) .

[4] Embedding: Algorithms that can generate final user and item embeddings.

Powered by

JetBrains Logo

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

LibRecommender-0.12.0.tar.gz (489.6 kB view details)

Uploaded Source

Built Distributions

LibRecommender-0.12.0-cp310-cp310-win_amd64.whl (766.2 kB view details)

Uploaded CPython 3.10Windows x86-64

LibRecommender-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

LibRecommender-0.12.0-cp310-cp310-macosx_10_9_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

LibRecommender-0.12.0-cp39-cp39-win_amd64.whl (769.7 kB view details)

Uploaded CPython 3.9Windows x86-64

LibRecommender-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

LibRecommender-0.12.0-cp39-cp39-macosx_10_9_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

LibRecommender-0.12.0-cp38-cp38-win_amd64.whl (769.3 kB view details)

Uploaded CPython 3.8Windows x86-64

LibRecommender-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

LibRecommender-0.12.0-cp38-cp38-macosx_10_9_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

LibRecommender-0.12.0-cp37-cp37m-win_amd64.whl (766.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

LibRecommender-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

LibRecommender-0.12.0-cp37-cp37m-macosx_10_9_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

LibRecommender-0.12.0-cp36-cp36m-win_amd64.whl (764.3 kB view details)

Uploaded CPython 3.6mWindows x86-64

LibRecommender-0.12.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

LibRecommender-0.12.0-cp36-cp36m-macosx_10_9_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

Details for the file LibRecommender-0.12.0.tar.gz.

File metadata

  • Download URL: LibRecommender-0.12.0.tar.gz
  • Upload date:
  • Size: 489.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for LibRecommender-0.12.0.tar.gz
Algorithm Hash digest
SHA256 286bc67c3725da59d206d0ed58e3dcffa6762e560e9b37b0f37d2d815c7b4c23
MD5 c43ee3d133aa763c8ce6addbfdf614d9
BLAKE2b-256 402f624eee8b619b3bb1020dbfe050d4212caba443da3cfcf6f261d1c1a3105c

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7cd5220cf5f7ce288facbb56bdfa82d448efe50cd9493d2871f448885429d150
MD5 4469ce1a75326bacc890c4e381ef431a
BLAKE2b-256 f64c82eb14f88dfd5f1bed6b59cdb41e13337b91c64bc1c614e394aea647aab9

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea70a152ad5a7097b9fac11e660898dcadc5776a4969c06e0ad9c2652e48a23d
MD5 7cee33f504f51cacf135556b737df064
BLAKE2b-256 7f1d4ccfb3974da527e98435d67edf334445e642f1700f4a36abe9859b5189d7

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 92841f8387b2124d096a482c386931571a9413f214a7b6bbf9907bfedce3b539
MD5 6106b511be2fa3369c1b8319cb8d642c
BLAKE2b-256 529cbf875bcd5a052d7eebed2cf59640d1e8e806bd2b5cea34a371457eeca8dc

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 21381858b1fc57c6934bd438a523eb175f476db32aa3c245d77dca27e4458634
MD5 7c43530b4b95f5cc10df9bafa556faea
BLAKE2b-256 8981640e8f96266435d4fbc4dbf5c849efd33cb04280b4874093d6b66d09c7e9

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 506bc3c738562680c19943485e91c1ebd4ddb1dda43a52411a8ee32e7f661a18
MD5 d4405d4353c3942d76bebed7c27cb719
BLAKE2b-256 049bc46f7e660eee01091a5f362ce82967331caf4dcfa56620248833dcc7a535

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1b58ecbeb06b460b331fc4c5b9af30380386ae1a730ba9d8d76f3af79383188c
MD5 b5ff81da128e2b000298a46629f29a41
BLAKE2b-256 8c740cfa0ee36b033b2b4b1dd04fd1cb0335154ab6fa2e75f2a781ff74ba9a25

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f836907ed82da789ea37020fb5c69c2d1a7220411cea34a86f3839b33a334037
MD5 bc6c3245d43b99bcf0f5e2d6ca6749e8
BLAKE2b-256 8ddc44df50a21cd87d867f5337bb4f2328173eb58d66c74d015241f16bfb3b86

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fb9bab16e23c1ad47f83b55be46f30769dce99393561c5fd2f2f562f22afb59
MD5 195bf26848394586ee20cf56cb3f981d
BLAKE2b-256 9d5aafd53802f821abf3347972ef2bf7a2c488bca14c51a7dc4073e0bc998872

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ed3136149347338a541d2b632ac9a8d649ed607ddd025e730aa9e80971329d21
MD5 ab01935207f200e24d3a918e7e9462f1
BLAKE2b-256 366cc34995dc1664857903ff2fdb4a70844f9ff4b3504076f34cce257630650a

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 4d57d675809abe412d69a28d9ae7f4d0b7a45502a800a7f2afd1378139794bf4
MD5 a2e326a9015b0d1298cbef0bd67f1e2e
BLAKE2b-256 7c4a4795759ae1e344e23a4e5c3c696c9ba9a0c3ef5aa9fd329f221323c28a86

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b76ecf4ca6903c4d986e8f6c3ab3be144e071f28ed9655212667abb4965bca5
MD5 0453422f7c4cb53d35cffda22b36d545
BLAKE2b-256 4287b194d4f33f41413a908211f607df2d9e126b8cd84e20be355fc381c60124

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cbfd0ad67753e738e6c93a065ab0d9aa297b681806a45d8b6870d70ea8f40286
MD5 7b7cca60d9f444792f8e1ba515987b0c
BLAKE2b-256 afc17c9c998fe830b8ffa49c4fad0bd893d12a8c6c94f0bb4695c655af1f5d37

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 c239d95c36896d786117e27c831a30dc6c866d9fc31b5ad6cb18037ef5fc7abb
MD5 bcf11594f4049c14e1effd7ba95192f7
BLAKE2b-256 c2779b17c3f337fb2fc392907b6e6869fcc1d518f5fbfd46c258a59e74dd6ec1

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d78e01cd35aa047454da8d83de13b81d0a03444a6432a6747658cc5fe21c1e9
MD5 89ff7487fbfd4acecc238306de677758
BLAKE2b-256 467ce2166d9d1b28a4fa7fe436e03148aeb66fa9f8b94a15285c50e609ec8c08

See more details on using hashes here.

File details

Details for the file LibRecommender-0.12.0-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for LibRecommender-0.12.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6cccd78ec0ff9e5bb7053045fc6e069277fee7b9e54aeb20f0f166d42977bf71
MD5 c5b56c90fcd8582665622e020e65b1de
BLAKE2b-256 57b0c677d40208b589f8bc83a3dbee69e53f6be27f0e2c7adb8358a70de974e4

See more details on using hashes here.

Supported by

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