Skip to main content

Acf

A lightweight recommender engine for implicit feedback datasets

PyPI Test Publish

The package implements an algorithm described in Collaborative Filtering for Implicit Feedback Datasets paper. The algorithm is based on the following ideas:

  • using collaborative filtering with latent factors
  • transforming feedback observations into binary preferences with associated confidence levels
  • using alternating least sqaures to compute the matrix factorization

Install

The package requires Python 3.7 or newer, the only dependencies are numpy and pandas. To install it, run

pip install acf

Usage

The following example shows how to train a model and compute predictions.

import acf
import pandas as pd

# assuming the data are in the following format:
# | user_id | item_id | feedback |
# |---------|---------|----------|
# | 2491    | 129     | 2        |

interactions = pd.read_csv('interactions.csv')

engine = acf.Engine(reg_lambda=1, alpha=35, n_factors=2, random_state=0)

engine.fit(interactions,
           user_column='user_id',
           item_column='item_id',
           feedback_column='feedback',
           n_iter=20,
           n_jobs=4)

# get the best 20 recommendations
prediction = engine.predict(user=2491, top_n=20)

# to print training loss value at every iteration
print(engine.loss)

Model Evaluation

For performance evaluation, the package offers metrics.mean_rank function that implements "mean rank" metric as defined by equation 8 in the paper.

The metric is a weighted mean of percentile-ranked recommendations (rank_ui = 0 says that item i is the first to be recommended for user u and item j with rank_uj = 1 is the last to be recommended) where the weights are the actual feedback values from R user-item matrix.

interactions_test = pd.read_csv('intercations_test.csv')

print(acf.metrics.mean_rank(interactions=interactions_test,
                            user_column='user_id',
                            item_column='item_id'
                            feedback_column='feedback',
                            engine=engine))

Model Persistence

Trained model can be serialized and stored using joblib or pickle.

To store a model:

with open('engine.joblib', 'wb') as f:
    joblib.dump(engine, f)

To load a model:

with open('engine.joblib', 'rb') as f:
    engine = joblib.load(f)

Public API

acf.Engine

acf.core.computation.Engine(reg_lambda=0.1, alpha=40,
                            n_factors=10, random_state=None):

Class exposing the recommender.

  • reg_lambda: regularization strength
  • alpha: gain parameter in feedback-confidence transformation c_ui = 1 + alpha * r_ui
  • n_factors: number of latent factors
  • random_state: initial RNG state

Properties:

  • user_factors: user factor matrix
  • item_factors: item factor matrix
  • loss: training loss history

Methods:

Engine.fit(interactions, user_column, item_column,
           feedback_column, n_iter=20, n_jobs=1)

Trains the model.

  • interactions: dataframe containing user-item feedbacks
  • user_column: name of the column containing user ids
  • item_column: name of the column containing item ids
  • feedback_column: name of the column containing feedback values
  • n_iter: number of alternating least squares iteration
  • n_jobs: number of parallel jobs
Engine.predict(user, top_n=None)

Predicts the recommendation.

  • user: user identification for whom the prediction is computed
  • top_n: if not None, only the besr n items are included in the result

Returns: predicted recommendation score for each item as pandas.Series

acf.metrics.mean_rank

acf.core.metrics.mean_rank(interactions, user_column, item_column,
                           feedback_column, engine)

Computes mean rank evaluation.

  • interactions: dataframe containing user-item feedbacks
  • user_column: name of the column containing user ids
  • item_column: name of the column containing item ids
  • feedback_column: name of the column containing feedback values
  • engine: trained acf.Engine instance

Returns: computed value

Tests

Tests can be executed by pytest as

python -m pytest acf/tests

Metadata

Release files for acf 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for acf 0.2.3
File Size Uploaded Provenance
acf-0.2.3.tar.gz 13.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for acf 0.2.3
File Interpreter ABI Platform
acf-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 30.0 kB

Release files / acf-0.2.3.tar.gz

Download URL acf-0.2.3.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
60ca033e988736728c2d95051f42b09ae4625030fec1aab7d8c32eb27a0d7880
BLAKE2b-256 checksum
How to use checksums
7c1e07a8e5fddc23aad5b7d735ded9a937bbe7a694ad70602bfc65a8e266c012
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / acf-0.2.3-py3-none-any.whl

Download URL acf-0.2.3-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b752efd531ea636344183b8c631e558040413a72950f48e587727e6c8a6f405
BLAKE2b-256 checksum
How to use checksums
c4741a4e70c2e9a5e08f1e731c8f8580c1101af23845b2e295f4ed4ff2b985d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Security for acf 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page