Skip to content
xLearn - the high performance machine learning library - for Ruby
Ruby Shell Python
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Don't save bin file by default [skip ci] Oct 15, 2019
test Renamed to latent_factors Oct 14, 2019
.gitignore First commit Oct 12, 2019
.travis.yml Added support for Numo Oct 12, 2019 Added dates to changelog [skip ci] Nov 14, 2019
LICENSE.txt First commit Oct 12, 2019 Added instructions for getting started with development [skip ci] Nov 14, 2019
xlearn.gemspec Added tests for matrix and Daru Oct 12, 2019


xLearn - the high performance machine learning library - for Ruby

🔥 Uses the C API for blazing performance


  • Linear models
  • Factorization machines
  • Field-aware factorization machines

Build Status


First, install xLearn. On Mac, copy build/lib/libxlearn_api.dylib to /usr/local/lib.

Add this line to your application’s Gemfile:

gem 'xlearn'

Getting Started

Prep your data

x = [[1, 2], [3, 4], [5, 6], [7, 8]]
y = [1, 2, 3, 4]

Train a model

model = "reg"), y)

Use XLearn::FM for factorization machines and XLearn::FFM for field-aware factorization machines

Make predictions


Save the model to a file


Load the model from a file


Save a text version of the model


Pass a validation set, y_train, eval_set: [x_val, y_val])

Train online

model.partial_fit(x_train, y_train)

Get the bias term, linear term, and latent factors

model.latent_factors # fm and ffm only


Specify parameters

model = 20, epoch: 50)

Supports the same parameters as Python


Cross-validation, y)

Specify the number of folds, y, folds: 5)


Data can be an array of arrays

[[1, 2, 3], [4, 5, 6]]

Or a Daru data frame


Or a Numo NArray, 2).seq


For large datasets, read data directly from files"train.txt", eval_set: "validate.txt")

For linear models and factorization machines, use CSV:


Or the libsvm format (better for sparse data):

label index_1:value_1 index_2:value_2 ... index_n:value_n

You can also use commas instead of spaces for separators

For field-aware factorization machines, use the libffm format:

label field_1:index_1:value_1 field_2:index_2:value_2 ...

You can also use commas instead of spaces for separators

You can also write predictions directly to a file

model.predict("test.txt", out_path: "predictions.txt")

xLearn Installation

There’s an experimental branch that includes xLearn with the gem for easiest installation.

gem 'xlearn', github: 'ankane/xlearn', branch: 'vendor', submodules: true

Please file an issue if it doesn’t work for you.

You can also specify the path to xLearn in an initializer:

XLearn.ffi_lib << "/path/to/xlearn/lib/"

Use libxlearn_api.dylib for Mac and xlearn_api.dll for Windows


This library is modeled after xLearn’s Scikit-learn API.


View the changelog


Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development and testing:

git clone
cd xlearn
bundle install
bundle exec rake test
You can’t perform that action at this time.