lightfm/lightfm-php

PHP port of the LightFM Python library using FFI bindings to C implementation for hybrid recommender systems

Maintainers

Package info

github.com/DaikiOnodera/lightfm-php

pkg:composer/lightfm/lightfm-php

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2025-07-01 17:16 UTC

This package is auto-updated.

Last update: 2026-04-17 16:09:25 UTC


README

PHP port of the LightFM Python library using FFI bindings to C implementation for hybrid recommender systems.

Installation

Install via Composer:

composer require lightfm/lightfm-php

Requirements

  • PHP 8.0 or higher
  • FFI extension enabled
  • The bundled C library (liblightfm.so)

Usage

<?php

require_once 'vendor/autoload.php';

// Create a LightFM model
$model = new LightFM([
    'no_components' => 30,
    'loss' => 'warp',
    'learning_rate' => 0.05
]);

// Fit the model with interaction data
$interactions = new CSRMatrix($data, $rows, $cols);
$model->fit($interactions);

// Get recommendations
$predictions = $model->predict($userIds, $itemIds);

Running the Examples

The repository includes a comprehensive demo script that showcases all major features of the library.

Prerequisites

  1. Install dependencies:
composer install
  1. Ensure the C library is compiled and available:
# The liblightfm.so file should be in the project root
ls -la liblightfm.so

Run the Demo

Execute the demo script from the project root:

php test/main.php

The demo will demonstrate:

  • Loading synthetic MovieLens-like data
  • Training models with WARP loss function
  • Making predictions and generating recommendations
  • Evaluating model performance with precision metrics
  • Using user and item features
  • Working with custom interaction data

Expected Output

The demo will display:

  • Dataset statistics (users, items, interactions)
  • Training progress and timing
  • Model evaluation metrics (precision@5)
  • Top recommendations for sample users
  • Performance comparison with and without features
  • Custom dataset predictions

Note: The current C implementation is simplified, so prediction scores may appear as zeros. This is expected behavior for the initial implementation.

Features

  • Hybrid Recommender Systems: Supports both collaborative filtering and content-based filtering
  • Multiple Loss Functions: WARP, BPR, and logistic loss functions
  • Fast C Implementation: Uses FFI bindings to a compiled C library for performance
  • CSR Matrix Support: Efficient sparse matrix operations
  • Evaluation Metrics: Built-in precision, recall, and AUC metrics

Classes

  • LightFM: Main model class for training and prediction
  • CSRMatrix: Sparse matrix implementation for efficient data handling
  • Evaluation: Evaluation metrics for model performance
  • MovieLensDataset: Dataset loader for MovieLens data
  • LightFMFFI: FFI bindings to the C library

License

Apache-2.0