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 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.

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

[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.

License

MIT


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

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

LibRecommender-0.10.2-cp310-cp310-win_amd64.whl (738.4 kB view details)

Uploaded CPython 3.10Windows x86-64

LibRecommender-0.10.2-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.10.2-cp310-cp310-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

LibRecommender-0.10.2-cp39-cp39-win_amd64.whl (741.9 kB view details)

Uploaded CPython 3.9Windows x86-64

LibRecommender-0.10.2-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.10.2-cp39-cp39-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

LibRecommender-0.10.2-cp38-cp38-win_amd64.whl (741.6 kB view details)

Uploaded CPython 3.8Windows x86-64

LibRecommender-0.10.2-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.10.2-cp38-cp38-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

LibRecommender-0.10.2-cp37-cp37m-win_amd64.whl (738.9 kB view details)

Uploaded CPython 3.7mWindows x86-64

LibRecommender-0.10.2-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.10.2-cp37-cp37m-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

LibRecommender-0.10.2-cp36-cp36m-win_amd64.whl (736.6 kB view details)

Uploaded CPython 3.6mWindows x86-64

LibRecommender-0.10.2-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.10.2-cp36-cp36m-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for LibRecommender-0.10.2.tar.gz
Algorithm Hash digest
SHA256 c96dc64ca2e7ecf42236d80676b26e8ce2050fee793638ce2002e0993ffe3f47
MD5 e6830976880020cd11b3a7c6df0cbbb5
BLAKE2b-256 5033dbb19337b502a5be8b290be53ccb6fd82b36dbe216f72896c8965786ebac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6080be5222c35bc92ccca0e827c36f32a9ae5bb415cb2e7e457681fa60feee6e
MD5 9f401ec27db422d669834d443b40b7ff
BLAKE2b-256 17a9f511361f662166487571e6240f5e1bc2b22b5e97707ae659a7d2c9d501ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6d3e07d12ee7b36cd46aceebac9dac40a30e1aa9e4ad71a4d8a61c4d2b728e5a
MD5 e533d158caef2b5bfc9a142fc964d7cf
BLAKE2b-256 fcfde689dae64923ec912d2c3f22c47325e1199323cd45c5c04d6ca6f9669672

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 be0ba03337c2507ded0671e60966aa806aa7bb5a935e26c0de383976427ec743
MD5 7ac1e0bfc13fbd3b1692ec494f6756a6
BLAKE2b-256 6e12df44bea27d2ca1e6bbd3e79594a1f99fe7eb9587f3a8eca1a596b694839a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3ecfcaa320c7a951b247fc56e7bb4f97bcc8203ff59dad3bf2851fb80da96836
MD5 ea211e1c665bc6c74699c0afe488d263
BLAKE2b-256 c3e4169c3438418f24439b4eebfa7187f16524e5b59631d08ce8cbda89828e3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 38f453194c98ff0f254d563ec5a0bb040926ad1c15e26d715045242851be6c78
MD5 3c91ec2753edf6f4ee5fe4fded1b4a18
BLAKE2b-256 95302c9784e687b663e4cbaf2aed187b9c4cd5c7879e89513f3a396bd3837a30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e67e13535fb8c3136dff116baef1df7628e29706f2b205e0493935b10c918ee8
MD5 e7fa625fa7a826e4490f38322ee34b2f
BLAKE2b-256 4118ef2a491c321baa223f3b605db9b6a6c646d402eba71ac23bb3ed6a5cdc56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 da3bf5ba4f89c0805790666fef28eab84777253a0d1366d76a68a429529a60fb
MD5 7a78f6c2628b874051355c08a8ffec30
BLAKE2b-256 b6ed567b32653c9246bc46769b4624788aaa5bb51b6764ab01f091a8e64f2e41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 845a022d40b005792b072e06c2cc376dae642d809d9be6417a5648c755fba811
MD5 e31f86e209c38b5de2776cad9c04d3bd
BLAKE2b-256 04638a22dad534893410d83f7ad1c43c710123f00d716bc33bf226623b912d7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ff37478b453ee32899e2cc03009eaa243d0203edaba27e7aea2498a1ba5fd791
MD5 d6337e8095a8bd278647d471436f0f83
BLAKE2b-256 9d2556a41f69460709ecd3a61e9f79e051494b27ec7f518af13796e0fdefe4e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 64f4fa595a28c3182dea1653e79ebd555c679f057c463c104027b3cde6392c29
MD5 5d1f95081bbf30e5bd285d4097e92622
BLAKE2b-256 c5ce26b8faa58e0aef804aa938848c359d4af887e9879c52a48bd4ece9176d68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e9572715ee1cf139401df02f15a5aa1e98f428388ca1e5d5d1312bff1e074c9
MD5 c806332260ec9a7f3ced6c3fdf7fb581
BLAKE2b-256 1c312beed2a37b9514aa907ef24bb7f8e41ecdec992a8485404e769a45687598

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c2f9cbaf4c42c5058bcee3ba746b3f9f414b860e07de3d6867f2c06e94bb8ff8
MD5 2e28aa1c856b8780c0aa573b06e304e7
BLAKE2b-256 2ef47c836b48e978560cd760c1def37959a881d2815d969fcf5105c12c7b9bfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 d62ca22558477ba4b32bcb3a4abe172a7e8beb189fb69e34bba3d4cd25c3aee6
MD5 79ec0d24a421e9cfed064265b72bb3be
BLAKE2b-256 713d4ea33acbbf546496df3192778abd038ebe7d7ab8ac6f9410d73e93a0629d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 681828d96b80ab9ef8025a0a71b07c035050b37e1bcd152eef8f2077bd0a5b24
MD5 47632299f95aee49ca144322a456b804
BLAKE2b-256 cec59bd9f1f91812fabe7eb8b6f66eca381b3c03bf92067c5410979728f8ef25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for LibRecommender-0.10.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 03f769e7e75fad317dab58f20741fb7780352119f6ea436722a8f4bf33be8488
MD5 764d6d8225e72f77fd28f16cb848c528
BLAKE2b-256 6cc301206cf6fe85fbcae16f2c6ad7adae7fad333d62b302296460512f078cd4

See more details on using hashes here.

Supported by

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