Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
Mar 29, 2019
Mar 29, 2019
Nov 19, 2013
Nov 19, 2013
Mar 29, 2019
Nov 19, 2013


Gem Version

Liblinear-Ruby is Ruby interface of LIBLINEAR using SWIG. Now, this interface is supporting LIBLINEAR 2.30.


Add this line to your application's Gemfile:

gem 'liblinear-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install liblinear-ruby

Quick Start

This sample code execute classification with L2-regularized 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


There are some parameters you can specify:

  • solver_type
  • cost
  • sensitive_loss
  • epsilon
  • weight_labels and weights


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 multi-class classification
Liblinear::L2R_LR              # L2-regularized logistic regression (primal)
Liblinear::L2R_L2LOSS_SVC_DUAL # L2-regularized L2-loss support vector classification (dual)
Liblinear::L2R_L2LOSS_SVC      # L2-regularized L2-loss support vector classification (primal)
Liblinear::L2R_L1LOSS_SVC_DUAL # L2-regularized L1-loss support vector classification (dual)
Liblinear::MCSVM_CS            # support vector classification by Crammer and Singer
Liblinear::L1R_L2LOSS_SVC      # L1-regularized L2-loss support vector classification
Liblinear::L1R_LR              # L1-regularized logistic regression
Liblinear::L2R_LR_DUAL         # L2-regularized logistic regression (dual)

# for regression
Liblinear::L2R_L2LOSS_SVR      # L2-regularized L2-loss support vector regression (primal)
Liblinear::L2R_L2LOSS_SVR_DUAL # L2-regularized L2-loss support vector regression (dual)
Liblinear::L2R_L1LOSS_SVR_DUAL # L2-regularized L1-loss support vector regression (dual)


This parameter specifies the cost of constraints violation (default 1.0).
This corresponds to -c option on command line.


This parameter specifies an epsilon in loss function of epsilon-SVR (default 0.1).
This corresponds to -p option on command line.


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].


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:

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.



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 k-fold 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:

# You can undo by:



No description or website provided.





No releases published


No packages published

Contributors 4

You can’t perform that action at this time.