Skip to main content

a python implementation of the generic factorization machines model class adapted for collaborative filtering recommendation problems with implicit feedback user-item interaction data and (optionally) additional user/item side features

Project description

RankFM

PyPI version CircleCI Documentation Status License: GPL v3

[original author]


Improvement points

Due to the author's cessation of maintaining Rankfm, only a new project can be created for maintenance.

  1. Remove incompatible prints.
  2. Adding Compilation of Different Python Versions for Different Systems.
  3. When github create release, rankfm automatic compilation and release to pipy.
  4. Add README.md to pipy.
  5. Add clear explanations based on the issue.
  6. Removing large C memory view conversion, training speed increased by 52 times.
  7. Parallel recommendation.
  8. Remove useless global variables to reduce memory.

Describe

RankFM is a python implementation of the general Factorization Machines model class adapted for collaborative filtering recommendation/ranking problems with implicit feedback user/item interaction data. It uses Bayesian Personalized Ranking (BPR) and a variant of Weighted Approximate-Rank Pairwise (WARP) loss to learn model weights via Stochastic Gradient Descent (SGD). It can (optionally) incorporate sample weights and user/item auxiliary features to augment the main interaction data.

The core (training, prediction, recommendation) methods are written in Cython, making it possible to scale to millions of user/item interactions. Designed for ease-of-use, RankFM accepts both pd.DataFrame and np.ndarray inputs - you do not have to convert your data to scipy.sparse matrices or re-map user/item identifiers prior to use. RankFM internally maps all user/item identifiers to zero-based integer indexes, but always converts its output back to the original user/item identifiers from your data, which can be arbitrary (non-zero-based, non-consecutive) integers or even strings.

In addition to the familiar fit(), predict(), recommend() methods, RankFM includes additional utilities similiar_users() and similar_items() to find the most similar users/items to a given user/item based on latent factor space embeddings. A number of popular recommendation/ranking evaluation metric functions have been included in the separate evaluation module to streamline model tuning and validation.

  • see the Quickstart section below to get started with the basic functionality
  • see the /examples folder for more in-depth jupyter notebook walkthroughs with several popular open-source data sets
  • see the Online Documentation for more comprehensive documentation on the main model class and separate evaluation module
  • see the Medium Article for contextual motivation and a detailed mathematical description of the algorithm

Dependencies

  • Python 3.8+
  • numpy >= 1.15
  • pandas >= 0.24

Installation

You can install the latest published version from PyPI using pip:

pip install rankfmc

Or alternatively install the current development build directly from GitHub:

pip install git+https://github.com/ErraticO/rankfmc.git

Quickstart

Let's work through a simple example of fitting a model, generating recommendations, evaluating performance, and assessing some item-item similarities. The data we'll be using here may already be somewhat familiar: you know it, you love it, it's the MovieLens 1M!

Let's first look at the required shape of the interaction data:

user_id item_id
3 233
5 377
8 610

It has just two columns: a user_id and an item_id (you can name these fields whatever you want or use a numpy array instead). Notice that there is no rating column - this library is for implicit feedback data (e.g. watches, page views, purchases, clicks) as opposed to explicit feedback data (e.g. 1-5 ratings, thumbs up/down). Implicit feedback is far more common in real-world recommendation contexts and doesn't suffer from the missing-not-at-random problem of pure explicit feedback approaches.

Now let's import the library, initialize our model, and fit on the training data:

from rankfmc import RankFM
model = RankFM(factors=20, loss='warp', max_samples=20, alpha=0.01, sigma=0.1, learning_rate=0.1, learning_schedule='invscaling')
model.fit(interactions_train, epochs=20, verbose=True)
# NOTE: this takes about 30 seconds for 750,000 interactions on my 2.3 GHz i5 8GB RAM MacBook

If you set verbose=True the model will print the current epoch number as well as the epoch's log-likelihood during training. This can be useful to gauge both computational speed and training gains by epoch. If the log likelihood is not increasing then try upping the learning_rate or lowering the (alpha, beta) regularization strength terms. If the log likelihood is starting to bounce up and down try lowering the learning_rate or using learning_schedule='invscaling' to decrease the learning rate over time. If you run into overflow errors then decrease the feature and/or sample-weight magnitudes and try upping beta, especially if you have a small number of dense user-features and/or item-features. Selecting BPR loss will lead to faster training times, but WARP loss typically yields superior model performance.

Now let's generate some user-item model scores from the validation data:

valid_scores = model.predict(interactions_valid, cold_start='nan')

this will produce an array of real-valued model scores generated using the Factorization Machines model equation. You can interpret it as a measure of the predicted utility of item (i) for user (u). The cold_start='nan' option can be used to set scores to np.nan for user/item pairs not found in the training data, or cold_start='drop' can be specified to drop those pairs so the results contain no missing values.

Now let's generate our topN recommended movies for each user:

valid_recs = model.recommend(valid_users, n_items=10, filter_previous=True, cold_start='drop')

The input should be a pd.Series, np.ndarray or list of user_id values. You can use filter_previous=True to prevent generating recommendations that include any items observed by the user in the training data, which could be useful depending on your application context. The result will be a pd.DataFrame where user_id values will be the index and the rows will be each user's top recommended items in descending order (best item is in column 0):

0 1 2 3 4 5 6 7 8 9
3 2396 1265 357 34 2858 3175 1 2028 17 356
5 608 1617 1610 3418 590 474 858 377 924 1036
8 589 1036 2571 2028 2000 1220 1197 110 780 1954

Now let's see how the model is performing wrt the included validation metrics evaluated on the hold-out data:

from rankfmc.evaluation import hit_rate, reciprocal_rank, discounted_cumulative_gain, precision, recall

valid_hit_rate = hit_rate(model, interactions_valid, k=10)
valid_reciprocal_rank = reciprocal_rank(model, interactions_valid, k=10)
valid_dcg = discounted_cumulative_gain(model, interactions_valid, k=10)
valid_precision = precision(model, interactions_valid, k=10)
valid_recall = recall(model, interactions_valid, k=10)
hit_rate: 0.796
reciprocal_rank: 0.339
dcg: 0.734
precision: 0.159
recall: 0.077

That's a Bingo!

Now let's find the most similar other movies for a few movies based on their embedding representations in latent factor space:

# Terminator 2: Judgment Day (1991)
model.similar_items(589, n_items=10)
2571                       Matrix, The (1999)
1527                Fifth Element, The (1997)
2916                      Total Recall (1990)
3527                          Predator (1987)
780             Independence Day (ID4) (1996)
1909    X-Files: Fight the Future, The (1998)
733                          Rock, The (1996)
1376     Star Trek IV: The Voyage Home (1986)
480                      Jurassic Park (1993)
1200                            Aliens (1986)

I hope you like explosions...

# Being John Malkovich (1999)
model.similar_items(2997, n_items=10)
2599           Election (1999)
3174    Man on the Moon (1999)
2858    American Beauty (1999)
3317        Wonder Boys (2000)
223              Clerks (1994)
3897      Almost Famous (2000)
2395           Rushmore (1998)
2502       Office Space (1999)
2908     Boys Don't Cry (1999)
3481      High Fidelity (2000)

Let's get weird...

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

rankfmc-0.3.1.tar.gz (209.1 kB view details)

Uploaded Source

Built Distributions

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

rankfmc-0.3.1-cp312-cp312-macosx_15_0_arm64.whl (264.6 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

rankfmc-0.3.1-cp311-cp311-win_amd64.whl (135.0 kB view details)

Uploaded CPython 3.11Windows x86-64

rankfmc-0.3.1-cp311-cp311-manylinux1_x86_64.whl (607.5 kB view details)

Uploaded CPython 3.11

rankfmc-0.3.1-cp311-cp311-macosx_15_0_arm64.whl (261.1 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

rankfmc-0.3.1-cp310-cp310-win_amd64.whl (135.0 kB view details)

Uploaded CPython 3.10Windows x86-64

rankfmc-0.3.1-cp310-cp310-manylinux1_x86_64.whl (578.0 kB view details)

Uploaded CPython 3.10

rankfmc-0.3.1-cp39-cp39-win_amd64.whl (135.5 kB view details)

Uploaded CPython 3.9Windows x86-64

rankfmc-0.3.1-cp39-cp39-manylinux1_x86_64.whl (579.5 kB view details)

Uploaded CPython 3.9

rankfmc-0.3.1-cp39-cp39-macosx_15_0_arm64.whl (262.4 kB view details)

Uploaded CPython 3.9macOS 15.0+ ARM64

rankfmc-0.3.1-cp38-cp38-win_amd64.whl (135.4 kB view details)

Uploaded CPython 3.8Windows x86-64

rankfmc-0.3.1-cp38-cp38-manylinux1_x86_64.whl (620.2 kB view details)

Uploaded CPython 3.8

rankfmc-0.3.1-cp38-cp38-macosx_15_0_arm64.whl (261.4 kB view details)

Uploaded CPython 3.8macOS 15.0+ ARM64

File details

Details for the file rankfmc-0.3.1.tar.gz.

File metadata

  • Download URL: rankfmc-0.3.1.tar.gz
  • Upload date:
  • Size: 209.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rankfmc-0.3.1.tar.gz
Algorithm Hash digest
SHA256 10e9982585055089172362179ae9e84082076192fbf5cc72d7f559109b2e021b
MD5 c0daa5f8622edbf12004caaf594b70b0
BLAKE2b-256 7d2cf7ab041450209d39a9f587acb32652c8607b55ec6c41605315319d6fd8e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1.tar.gz:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fa7260a8b8de771d3dc6e2af57a8f25132400aec63d1dc9605b90dcb1d5f67bc
MD5 3613461fa1082c936c68c7905245a997
BLAKE2b-256 9547c1921ec8951e25158e7c05b6552b45b5e7bbb24d8f252dbd9c79a06ad958

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: rankfmc-0.3.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 135.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rankfmc-0.3.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc74238ddfabc8a2036a575d7f8a1e2488d3cfb5bbda6c5c800526a367f204bb
MD5 6bcd102f71404d93c2ffcc7b10b4c7c7
BLAKE2b-256 b3cf39164828d2112fdc797d58d03e3d2d1740ecec57eee13538a55257c2a7fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp311-cp311-win_amd64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp311-cp311-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c07c3fca103741bf68a3d0203f471dc63a6e2bb2e0b52ef1f9c8c3e7032a1874
MD5 16f979c9985415530c8de1ef1e7aafc0
BLAKE2b-256 992a7ad5ea56a8255d12a8b5996bb5b42b09732d6f7c6a41b8695080e34ce81a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp311-cp311-manylinux1_x86_64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7891c95a3650bd2d776d78f96b10bc9be6c7d9763e5e9abfa7fa81358388f1b2
MD5 381c8e3513cf53cf927d1cc59d962b08
BLAKE2b-256 3d74064df9775db9aaab615b452f06ee641796e313a37a93987f64be71c8eafa

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: rankfmc-0.3.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 135.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rankfmc-0.3.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4a1ab42bae42543a46d8195e476b1894b6ce0f6de077db38a0f17a9972ed3b1f
MD5 7b97fe5e7ed27ac931bb9cdf45a511e8
BLAKE2b-256 00486d44d9e46dca9f29b7dfc024e511eaefa416d75039135cfe02a98422d070

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp310-cp310-win_amd64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp310-cp310-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0d8326238deee495a63fd07b218c679bf2a58cc453334a40f34a68f59fc774ba
MD5 4475fe8d021cd544abf4761a5b28e626
BLAKE2b-256 3c80dd3bae46d2e5bb08e89bb1d651798100518ce9f621652c3ebf214ffcf02e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp310-cp310-manylinux1_x86_64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: rankfmc-0.3.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 135.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rankfmc-0.3.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ce4cf7ebc68264bfb6447ea91fe891ed4d47047640835cd49c64e9e70ac0e56b
MD5 79de7c47c149822f8b263a5a16007880
BLAKE2b-256 a8103776f0e0288b1bf5384e1f73516fcced532819cc029e291c8c2526402ccf

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp39-cp39-win_amd64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 357de05c2d6e7e6472ab1ed4b0e5dd61a21522b2307c9400ea2f59f2cd1f52eb
MD5 f2bc56633a8dc75ec0417a2f597d9140
BLAKE2b-256 23a6b24bdf3d076bf547df4e030ad7ee7ece7f3044b9186ba0a2159aff27622c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp39-cp39-manylinux1_x86_64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp39-cp39-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp39-cp39-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 4490150d8c578662cefe2776ebafd355441e02dfeab83dfc7a87053e8e99fcad
MD5 9157dde4c410dad3a10a0988f5278853
BLAKE2b-256 d6e808864e76465b5962e5dfad999897deedc4b1454b3331996bc428bbebd126

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp39-cp39-macosx_15_0_arm64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: rankfmc-0.3.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 135.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rankfmc-0.3.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1c50049f57dfcc9d167de87f4ea8d547cbc9eef223a736dfbc54bfd945261d6c
MD5 92b2f09b37a902bc3cdfcb4d4ee187db
BLAKE2b-256 0c72dc463dacba10e1123834b7a5407bdfd7661901aadb95af8676ef72bba8bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp38-cp38-win_amd64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d86dbab73f6cf67c223a2b168b3b4057c976b0d28de84366feac562629793254
MD5 6a29547c6abec11396beb421deb59056
BLAKE2b-256 bc667c4cffb42787a7288e40886e777fc8e4c2e4f8ed4af361e3410984ad5ce5

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp38-cp38-manylinux1_x86_64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rankfmc-0.3.1-cp38-cp38-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for rankfmc-0.3.1-cp38-cp38-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 652e56d3716b5d180eeebc40071e8b82ac9f2205e555fe717fab163aa5e6a26a
MD5 e9a02e33207fc3ce8284b89df3be1068
BLAKE2b-256 58602c21132b0f70f5a7ab05dc2fe0aa2ae804bac90d0e4ae8fc4b30ae30777a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rankfmc-0.3.1-cp38-cp38-macosx_15_0_arm64.whl:

Publisher: pipy-publish.yml on ErraticO/rankfmc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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