Skip to content


Subversion checkout URL

You can clone with
Download ZIP
This is the Ruby interface to LIBLINEAR (much more efficient than LIBSVM for text classification and other large linear classifications)
C++ Perl C Ruby Python Objective-C
Branch: master

Merge pull request #8 from mrgordon/no_STR2CSTR

Remove calls to deprecated STR2CSTR
latest commit 719b3076aa
@tomz authored




This is the Ruby LIBLINEAR SWIG (Simplified Wrapper and Interface Generator) interface. LIBLINEAR is a high performance machine learning library for large scale text mining(

A slightly modified version of LIBLINEAR 1.8 is included which allows turning on/off the default debuging/logging messages. You don't need your own copy of SWIG to use this library - all needed files are generated using SWIG already.

LIBLINEAR is in use at - A Twitter / Tweet sentiment analysis application


Currently the gem is available on linux and OS X, and you will need g++ installed to compile the native code.

sudo gem sources -a   (you only have to do this once)
sudo gem install tomz-liblinear-ruby-swig


Try the following multiclass problem in irb:

irb(main):001:0> require 'rubygems'
irb(main):002:0> require 'linear'
irb(main):003:0> pa =
irb(main):004:0> pa.solver_type = MCSVM_CS 
irb(main):005:0> pa.eps = 0.1
irb(main):006:0> bias = 1
irb(main):007:0> labels = [1, 2, 1, 2, 3]
irb(main):008:0> samples = [
irb(main):009:1*            {1=>0,2=>0.1,3=>0.2,4=>0,5=>0},
irb(main):010:1*            {1=>0,2=>0.1,3=>0.3,4=>-1.2,5=>0},
irb(main):011:1*            {1=>0.4,2=>0,3=>0,4=>0,5=>0},
irb(main):012:1*            {1=>0,2=>0.1,3=>0,4=>1.4,5=>0.5},
irb(main):013:1*            {1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1}
irb(main):014:1>           ]
irb(main):016:0> sp =,samples,bias)
irb(main):017:0> m =, pa)
irb(main):018:0>  pred = m.predict({1=>1,2=>0.1,3=>0.2,4=>0,5=>0})
=> 1
irb(main):019:0>  pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0})
=> 2
irb(main):020:0>  pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0})
=> 2
irb(main):025:0>  pred = m.predict({1=>0.4,2=>0,3=>0,4=>0,5=>0})
=> 1
irb(main):021:0>  pred = m.predict({1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1})
=> 3

For more examples see test*.rb in the liblinear-ruby-swig/liblinear-1.8/ruby directory


Tom Zeng

Something went wrong with that request. Please try again.