LiblinearRuby
LiblinearRuby is Ruby interface of LIBLINEAR using SWIG. Now, this interface is supporting LIBLINEAR 2.30.
Installation
Add this line to your application's Gemfile:
gem 'liblinearruby'
And then execute:
$ bundle
Or install it yourself as:
$ gem install liblinearruby
Quick Start
This sample code execute classification with L2regularized logistic regression.
require 'liblinear'
# train
model = Liblinear.train(
{ solver_type: Liblinear::L2R_LR }, # parameter
[1, 1, 1, 1], # labels (classes) of training data
[[2, 2], [1, 1], [1, 1], [2, 2]], # training data
)
# predict
puts Liblinear.predict(model, [0.5, 0.5]) # predicted class will be 1
Parameter
There are some parameters you can specify:
solver_type
cost
sensitive_loss
epsilon

weight_labels
andweights
solver_type
This parameter specifies a type of solver (default: Liblinear::L2R_L2LOSS_SVC_DUAL
).
This corresponds to s
option on command line.
Solver types you can set are shown below:
# for multiclass classification
Liblinear::L2R_LR # L2regularized logistic regression (primal)
Liblinear::L2R_L2LOSS_SVC_DUAL # L2regularized L2loss support vector classification (dual)
Liblinear::L2R_L2LOSS_SVC # L2regularized L2loss support vector classification (primal)
Liblinear::L2R_L1LOSS_SVC_DUAL # L2regularized L1loss support vector classification (dual)
Liblinear::MCSVM_CS # support vector classification by Crammer and Singer
Liblinear::L1R_L2LOSS_SVC # L1regularized L2loss support vector classification
Liblinear::L1R_LR # L1regularized logistic regression
Liblinear::L2R_LR_DUAL # L2regularized logistic regression (dual)
# for regression
Liblinear::L2R_L2LOSS_SVR # L2regularized L2loss support vector regression (primal)
Liblinear::L2R_L2LOSS_SVR_DUAL # L2regularized L2loss support vector regression (dual)
Liblinear::L2R_L1LOSS_SVR_DUAL # L2regularized L1loss support vector regression (dual)
cost
This parameter specifies the cost of constraints violation (default 1.0
).
This corresponds to c
option on command line.
sensitive_loss
This parameter specifies an epsilon in loss function of epsilonSVR (default 0.1
).
This corresponds to p
option on command line.
epsilon
This parameter specifies a tolerance of termination criterion.
This corresponds to e
option on command line.
The default value depends on a type of solver. See LIBLINEAR's README or Liblinear::Parameter.default_epsion
for more details.
weight_labels and weights
These parameters are used to change the penalty for some classes (default []
).
Each weights[i]
corresponds to weight_labels[i]
, meaning that the penalty of class weight_labels[i]
is scaled by a factor of weights[i]
.
Train
First, prepare training data.
# Define class of each training data:
labels = [1, 1, ...]
# Training data is Array of Array:
examples = [
[1, 0, 0, 1, 0],
[0, 0, 0, 1, 1],
...
]
# You can also use Array of Hash instead:
examples = [
{ 1 => 1, 4 => 1 },
{ 4 => 1, 5 => 1 },
...
]
Next, set the bias (this corresponds to B
option on command line):
bias = 0.5 # default 1
Then, specify parameters and execute Liblinear.train
to get the instance of Liblinear::Model
.
model = Liblinear.train(parameter, labels, examples, bias)
In this phase, you can save model as:
model.save(file_name)
If you have already had a model file, you can load it as:
model = Liblinear::Model.load(file_name)
Feature Weights
To get the feature weights of the model.
model.feature_weights
Predict
Prepare the data you want to predict its class and call Liblinear.predict
.
examples = [0, 0, 0, 1, 1]
Liblinear.predict(model, example)
Cross Validation
To get classes predicted by kfold cross validation, use Liblinear.cross_validation
.
For example, results[0]
is a class predicted by examples
excepts part including examples[0]
.
results = Liblinear.cross_validation(fold, parameter, labels, examples)
Quiet / Verbose Mode
You can supress output while executing Liblinear::Model.train
.
# Output is supressed by:
Liblinear.quiet_mode
# You can undo by:
Liblinear.verbose_mode