Giter VIP home page Giter VIP logo

rb-libsvm's Introduction

Gem Version

rb-libsvm -- Ruby language bindings for LIBSVM

This package provides Ruby bindings to the LIBSVM library. SVM is a machine learning and classification algorithm, and LIBSVM is a popular free implementation of it, written by Chih-Chung Chang and Chih-Jen Lin, of National Taiwan University, Taipei. See the book "Programming Collective Intelligence," among others, for a usage example.

There is a JRuby implementation of this gem named jrb-libsvm by Andreas Eger.

Note: There exist some other Ruby bindings for LIBSVM. One is named Ruby SVM, written by Rudi Cilibrasi. The other, more actively developed one is libsvm-ruby-swig by Tom Zeng, which is built using SWIG.

LIBSVM includes a number of command line tools for preprocessing training data and finding parameters. These tools are not included in this gem. You should install the original package if you need them.

It is helpful to consult the README of the LIBSVM package for reference when configuring the training parameters.

Currently this package includes libsvm version 3.24.

Dependencies

None. LIBSVM is bundled with the project. Just install and go!

Installation

For building this gem from source on OS X (which is the default packaging) you will need to have Xcode installed, and from within Xcode you need to install the command line tools. Those contain the compiler which is necessary for the native code, and similar tools.

To install the gem run this command

gem install rb-libsvm

Usage

This is a short example of how to use the gem.

require 'libsvm'

# This library is namespaced.
problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new

parameter.cache_size = 1 # in megabytes

parameter.eps = 0.001
parameter.c = 10

examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
labels = [1, -1]

problem.set_examples(labels, examples)

model = Libsvm::Model.train(problem, parameter)

pred = model.predict(Libsvm::Node.features(1, 1, 1))
puts "Example [1, 1, 1] - Predicted #{pred}"

If you want to rely on Bundler for loading dependencies in a project, (i.e. use Bundler.require or use an environment that relies on it, like Rails), then you will need to specify rb-libsvm in the Gemfile like this:

gem 'rb-libsvm', require: 'libsvm'

This is because the loadable name (libsvm) is different from the gem's name (rb-libsvm).

Release

The process to make a release of the gem package to rubygems.org has a number of steps.

  • manually change the version in lib/libsvm/version.rb
  • clean, build, and run tests successfully
  • update code and documentation
  • push
  • sign into https://rubygems.org/
  • save API token from https://rubygems.org/profile/edit and store in .gem/credentials by running gem signin
  • perform actual release: bundle exec rake release

Author

Written by C. Florian Ebeling.

Contributors

License

This software can be freely used under the terms of the MIT license, see file MIT-LICENSE.

This package includes the source of LIBSVM, which is free to use under the license in the file LIBSVM-LICENSE.

Posts about using SVMs with Ruby

https://www.practicalai.io/implementing-classification-using-a-svm-in-ruby/

http://neovintage.blogspot.com/2011/11/text-classification-using-support.html

http://www.igvita.com/2008/01/07/support-vector-machines-svm-in-ruby/

rb-libsvm's People

Contributors

andreaseger avatar ankane avatar apohllo avatar febeling avatar gogainda avatar neovintage avatar schwad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rb-libsvm's Issues

text example, line 52

I'm guessing that line 52 needs to be similar to the above ternary

pred = model.predict(Libsvm::Node.features(dictionary.map{|x| test_document.include?(x) ? 1 : 0 }))

otherwise, test_document.include?(x) returns [false, false] and the entire thing errors ( no method to_f for false:Falseclass, etc)

the result with the ternary, which means little right now is....

=> 543175208.0

require 'libsvm' fails (MS windows)

I've installed ruby, devkit and rubygems and sucessfully installed the rb-libsvm gem. When I try to run the examples or go require 'libsvm' in irb i get this:

C:\dev>ruby
require 'libsvm'
^Z
c:/dev/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': 193: %1 is not a valid Win32 application.   - c:/dev/ruby193/lib/ruby/gems/1.9.1/gems/rb-libsvm-1.0.8/lib/libsvm/libsvm_ext.so (LoadError)
        from c:/dev/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from c:/dev/ruby193/lib/ruby/gems/1.9.1/gems/rb-libsvm-1.0.8/lib/libsvm.rb:2:in `'
        from c:/dev/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
        from c:/dev/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
        from c:/dev/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
        from -:1:in `'

Recommend doing something when svm_load_model fails

It seems like it would be good to either throw an exception or return nil in the event that svm_load_model(path) (libsvm.c line 500) returns NULL.

/* throw exception if svm_load_model returns NULL */
if (!model) {
rb_raise(rb_eIOError, "Unable to read model file: %s", path);
}

The other approach would look like:

return model ? (Data_Wrap_Struct(cModel,0, model_free, model)) : Qnil;

Without any feedback from svm_load_model, Ruby segfaults when I call predict on the returned model.

Thanks,

Tom

predict_probability

I tried to get prediction probability, but even with the simple example provided in readme file, the return value of method is always 0.0 for each predicted class.

Example

require 'libsvm'

# This library is namespaced.
problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new

parameter.cache_size = 1 # in megabytes

parameter.eps = 0.001
parameter.c = 10

examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
labels = [1, -1]

problem.set_examples(labels, examples)

model = Libsvm::Model.train(problem, parameter)

test = [0,0,0]

pred = model.predict_probability(Libsvm::Node.features(test))
puts "Example #{test} - Predicted #{pred}"

>>> Example [0, 0, 0] - Predicted [-1.0, [0.0, 0.0]]

I'm using OS X 10.8.4 but I tried also on latest ubuntu with the same result.

Adjust paths to modules

In Ruby community there is a strong directory structure pattern, which makes the modules correspond to directories.

I know that the library name might not be changed to libsvm, since this a different implementation of the libsvm wrapper, but I think rb-libsvm would be less surprising for developers, if the paths inside the lib directory were corresponding the the module Libsvm.

So instead of

  lib/rb-libsvm

we would have

  lib/libsvm

This also applies to the rb-libsvm.so which is generated during library installation.

Decimal values for prediction

Hi.

I would like to make a ROC-curve for the results.
Are there any way to get the decimal values for each prediction?
Or probably some other ways to do it?

Thank you

ONE CLASS

Hey,
I am trying to use your gem to make one-class svm work. I was just trying to make it work with the modified example code:

problem = Libsvm::Problem.new

parameter = Libsvm::SvmParameter.new
parameter.cache_size = 1 # in megabytes
parameter.eps = 0.001
parameter.nu = 0.5
parameter.svm_type = Libsvm::SvmType::ONE_CLASS

examples = [ [1,1,1], [0,0,0] ].map {|ary| Libsvm::Node.features(ary) }
labels = [1,1]
problem.set_examples(labels, examples)

model = Libsvm::Model.train(problem, parameter)

puts model.predict(Libsvm::Node.features(1, 1, 1))
puts model.predict(Libsvm::Node.features(0, 0, 0))
puts model.predict(Libsvm::Node.features(100, 100, 100))

However the output is -1 -1 -1 insted of 1 1 -1 as I would expect. What am I doing wrong?

Multi Class

It does not have NU_CVC and C_CVC enabled?
Returns an error.

How to enable and use it?

RubyML

Dear Florian,

we've recently added rb-libsvm to our RubyML list: https://github.com/arbox/machine-learning-with-ruby

In order to connect all Ruby & ML related projects we introduced the rubyml topic on GitHub.

Please cosider adding the rubyml topic to this repository as well so the Ruby community can discover rb-libsvm faster ๐Ÿ˜ƒ

Thank you in advance!

Save Models

I haven't found any way to save a trained model, so I am making this a feature request.

text / language processing

I was connected to this project through neovintages blog articles I found in my search to understand machine learning and what I can do with it in ruby.

I'm interested doing text processing, and the one article specifically pointed this out for text processing -- but it seems to take only libsvm formats (which I'm still trying to learn) and to do text processing requires a bit more along the lines of sally(http://www.mlsec.org/sally) which I've also stumbled across and am integrating into what I'm trying to accomplish.

So, 2 question points:

1 - Is there some sort of text processing feature I'm unaware of? From what I saw there is not, but if there would be more examples would be appreciated.
2 - What is the exact data format(s)? I know it is libsvm data, but the example is sparse at best.

Setting kernel_type causes segfault

Hi,

Using rb-libsvm (1.0.4), ruby 1.9.2p136 (x86_64-darwin10.5.0) the following code causes a segfault:

require 'libsvm'

# This library is namespaced.
problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new

parameter.cache_size = 1 # in megabytes
parameter.eps = 0.001
parameter.c = 10
parameter.kernel_type = 2
parameter.gamma = 0.0025

examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
labels = [1, -1]

problem.set_examples(labels, examples)

model = Libsvm::Model.train(problem, parameter)

pred = model.predict(Libsvm::Node.features(1, 1, 1))
puts "Example [1, 1, 1] - Predicted #{pred}"

The segfault message reads

Example [1, 1, 1] - Predicted 1.0
lib/test_params.rb: [BUG] Segmentation fault
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.5.0]

-- control frame ----------
c:0001 p:0000 s:0002 b:0002 l:0014f8 d:0014f8 TOP   
---------------------------

-- C level backtrace information -------------------------------------------

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

Abort trap

The code runs fine if I comment out the parameter.kernel_type = 2 line.

Any ideas?

Invalid gemspec

Running ruby 1.9.2p136, rubygems 1.8.11 and rb-libsvm-1.0.1 I get the following error when attempting to install the gem:

Building native extensions. This could take a while...
Invalid gemspec in [$gemspec_location]: Illformed requirement ["#Syck::DefaultKey:0x000001020dac70 2.7.0"]
Successfully installed rb-libsvm-1.0.1

I don't think rb-libsvm has been installed however as 'require 'libsvm', as per the documentation, gives 'LoadError: no such file to load -- libsvm'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.