xLearn
xLearn - the high performance machine learning library - for Ruby
Supports:
- Linear models
- Factorization machines
- Field-aware factorization machines
Installation
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 = XLearn::Linear.new(task: "reg")
model.fit(x, y)
Use XLearn::FM
for factorization machines and XLearn::FFM
for field-aware factorization machines
Make predictions
model.predict(x)
Save the model to a file
model.save_model("model.bin")
Load the model from a file
model.load_model("model.bin")
Save a text version of the model
model.save_txt("model.txt")
Pass a validation set
model.fit(x_train, 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.bias_term
model.linear_term
model.latent_factors # fm and ffm only
Parameters
Specify parameters
model = XLearn::Linear.new(k: 20, epoch: 50)
Supports the same parameters as Python
Cross-Validation
Cross-validation
model.cv(x, y)
Specify the number of folds
model.cv(x, y, folds: 5)
Data
Data can be an array of arrays
[[1, 2, 3], [4, 5, 6]]
Or a Daru data frame
Daru::DataFrame.from_csv("houses.csv")
Or a Numo NArray
Numo::DFloat.new(3, 2).seq
Performance
For large datasets, read data directly from files
model.fit("train.txt", eval_set: "validate.txt")
model.predict("test.txt")
model.cv("train.txt")
For linear models and factorization machines, use CSV:
label,value_1,value_2,...,value_n
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/libxlearn_api.so"
Use
libxlearn_api.dylib
for Mac andxlearn_api.dll
for Windows
Credits
This library is modeled after xLearn’s Scikit-learn API.
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features