rb-libsvm 1.0.1 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,3 +4,6 @@ tmp/
4
4
  *.dylib
5
5
  *.model
6
6
  pkg/
7
+ Gemfile*
8
+ .rvmrc
9
+ tags
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm: 1.9.2
2
+
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # rb-libsvm -- Ruby language bindings for LIBSVM
2
+
3
+ Spec Status: [![Build Status](https://secure.travis-ci.org/febeling/rb-libsvm.png)](http://travis-ci.org/febeling/rb-libsvm)
4
+
5
+ This is a module which provides a Ruby API to the LIBSVM [1] library.
6
+ SVM is a machine learning and classification algorithm, and LIBSVM is
7
+ a popular free implementation of it, written by Chih-Chung Chang and
8
+ Chih-Jen Lin, of National Taiwan University, Taipei. See "Programming
9
+ Collective Intelligence," [2] among others, for a usage example.
10
+
11
+ Note: There exists another Ruby binding for LIBSVM, named Ruby SVM,
12
+ [3] written by Rudi Cilibrasi. (That's the one mentioned in the
13
+ libsvm documentation.) This package is not related but written
14
+ independently and from scratch.
15
+
16
+ ## Dependencies
17
+
18
+ None. Libsvm is bundled with the project. Just install and go!
19
+
20
+ Currently using libsvm version 3.1
21
+
22
+ ## Installation
23
+
24
+ gem install rb-libsvm
25
+
26
+ ## Usage
27
+
28
+ require 'libsvm'
29
+
30
+ # This library is namespaced.
31
+ problem = Libsvm::Problem.new
32
+ parameter = Libsvm::SvmParameter.new
33
+
34
+ parameter.cache_size = 1 # in megabytes
35
+
36
+ parameter.eps = 0.001
37
+ parameter.c = 10
38
+
39
+ examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
40
+ labels = [1, -1]
41
+
42
+ problem.set_examples(labels, examples)
43
+
44
+ model = Libsvm::Model.train(problem, parameter)
45
+
46
+ pred = model.predict(Libsvm::Node.features(1, 1, 1))
47
+ puts "Example [1, 1, 1] - Predicted #{pred}"
48
+
49
+
50
+ ## Author, License
51
+
52
+ Written by C. Florian Ebeling. This software can be freely used under
53
+ the terms of the MIT license, see file MIT-LICENSE.
54
+
55
+ ## Contributors
56
+
57
+ Rimas Silkaitis
58
+
59
+ ### Posts about SVMs and Ruby
60
+
61
+ [http://neovintage.blogspot.com/2011/11/text-classification-using-support.html](http://neovintage.blogspot.com/2011/11/text-classification-using-support.html)
62
+
63
+ [http://www.igvita.com/2008/01/07/support-vector-machines-svm-in-ruby/](http://www.igvita.com/2008/01/07/support-vector-machines-svm-in-ruby/)
64
+
65
+
66
+ ### Notes
67
+
68
+ [http://www.csie.ntu.edu.tw/~cjlin/libsvm/](http://www.csie.ntu.edu.tw/~cjlin/libsvm/)
69
+
70
+ [http://books.google.com/books?id=fEsZ3Ey-Hq4C](http://books.google.com/books?id=fEsZ3Ey-Hq4C)
71
+
72
+ [http://rubysvm.cilibrar.com/](http://rubysvm.cilibrar.com/)
73
+
74
+
data/Rakefile CHANGED
@@ -2,6 +2,8 @@ require "bundler/gem_tasks"
2
2
  require "rake/extensiontask"
3
3
  require 'rspec/core/rake_task'
4
4
 
5
+ task :default => :spec
6
+
5
7
  Rake::ExtensionTask.new('rb-libsvm') do |ext|
6
8
  ext.lib_dir = File.join('lib', 'rb-libsvm')
7
9
  ext.name = 'libsvm'
data/examples/toy.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'libsvm'
2
+
3
+ # This library is namespaced.
4
+ problem = Libsvm::Problem.new
5
+ parameter = Libsvm::SvmParameter.new
6
+
7
+ parameter.cache_size = 1 # in megabytes
8
+
9
+ parameter.eps = 0.001
10
+ parameter.c = 10
11
+
12
+ examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
13
+ labels = [1, -1]
14
+
15
+ problem.set_examples(labels, examples)
16
+
17
+ model = Libsvm::Model.train(problem, parameter)
18
+
19
+ pred = model.predict(Libsvm::Node.features(1, 1, 1))
20
+ puts "Example [1, 1, 1] - Predicted #{pred}"
21
+
@@ -1,7 +1,7 @@
1
1
  require 'mkmf'
2
2
 
3
3
  # $CFLAGS << " -save-temps -ggdb3 "
4
- # $LDFLAGS << " -lstdc++ "
4
+ $LDFLAGS << " -lstdc++ "
5
5
 
6
6
  # find_header('ruby.h')
7
7
  # find_library('svm', 'svm_train', '/opt/local/lib/', '/usr/lib/') || raise("svm library not found.")
@@ -15,6 +15,10 @@ VALUE mSvmType;
15
15
 
16
16
  const struct svm_node TERMINATOR = (struct svm_node) { -1, 0.0 };
17
17
 
18
+ static void model_free(struct svm_model *model) {
19
+ svm_free_and_destroy_model(&model);
20
+ }
21
+
18
22
  /* Libsvm::Node */
19
23
  static struct svm_node *node_new() {
20
24
  struct svm_node *n;
@@ -334,7 +338,7 @@ static VALUE cModel_class_train(VALUE obj,VALUE problem,VALUE parameter) {
334
338
  }
335
339
  model = svm_train(prob,param);
336
340
 
337
- return Data_Wrap_Struct(cModel, 0, svm_free_and_destroy_model, model);
341
+ return Data_Wrap_Struct(cModel, 0, model_free, model);
338
342
  }
339
343
 
340
344
  static VALUE cModel_predict(VALUE obj,VALUE example) {
@@ -385,7 +389,7 @@ static VALUE cModel_class_load(VALUE cls, VALUE filename)
385
389
  char *path;
386
390
  path = StringValueCStr(filename);
387
391
  model = svm_load_model(path);
388
- return Data_Wrap_Struct(cModel, 0, svm_free_and_destroy_model, model);
392
+ return Data_Wrap_Struct(cModel, 0, model_free, model);
389
393
  }
390
394
 
391
395
  static VALUE cModel_class_cross_validation(VALUE cls, VALUE problem, VALUE parameter, VALUE num_fold)
@@ -21,13 +21,13 @@
21
21
  rx_def_accessor0(CLASS,STRUCT,MEMBER_TYPE,ATTR,RNAME,)
22
22
 
23
23
  #define rx_def_accessor0(CLASS,STRUCT,MEMBER_TYPE,ATTR,RNAME,DEREF) \
24
- static VALUE CLASS ## _ ## RNAME(VALUE obj) { \
24
+ VALUE CLASS ## _ ## RNAME(VALUE obj) { \
25
25
  STRUCT *struct_var; \
26
26
  Data_Get_Struct(obj, STRUCT, struct_var); \
27
27
  return rx_from_ ## MEMBER_TYPE(DEREF struct_var->ATTR); \
28
28
  } \
29
29
  \
30
- static VALUE CLASS ## _ ## RNAME ## _set(VALUE obj,VALUE val) { \
30
+ VALUE CLASS ## _ ## RNAME ## _set(VALUE obj,VALUE val) { \
31
31
  STRUCT *struct_var; \
32
32
  Data_Get_Struct(obj, STRUCT, struct_var); \
33
33
  DEREF struct_var->ATTR = rx_to_ ## MEMBER_TYPE(val); \
@@ -1,3 +1,3 @@
1
1
  module Libsvm
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.5"
3
3
  end
data/rb-libsvm.gemspec CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
- s.add_development_dependency "rake-compiler"
23
- s.add_development_dependency "rspec", "2.7.0"
22
+ s.add_development_dependency('rake-compiler')
23
+ s.add_development_dependency('rspec', '>= 2.7.0')
24
24
 
25
25
  s.extensions << 'ext/rb-libsvm/extconf.rb'
26
26
  end
data/spec/usage_spec.rb CHANGED
@@ -34,4 +34,14 @@ describe "Basic usage" do
34
34
  pred = model.predict(Node.features(-1, 55, -1))
35
35
  pred.should == -1.0
36
36
  end
37
+
38
+ it "kernel parameter use" do
39
+ @parameter.kernel_type = SvmParameter::KernelType::RBF
40
+ examples = [[1, 2, 3], [-2, -2, -2]].map {|ary| Node.features(ary) }
41
+ @problem.set_examples([1, 2], examples)
42
+
43
+ model = Model.train(@problem, @parameter)
44
+
45
+ model.predict(Node.features(1, 2, 3)).should == 2
46
+ end
37
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-libsvm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-07 00:00:00.000000000Z
13
+ date: 2011-12-07 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake-compiler
17
- requirement: &2157654260 !ruby/object:Gem::Requirement
17
+ requirement: &2153848920 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,18 +22,18 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2157654260
25
+ version_requirements: *2153848920
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &2157653760 !ruby/object:Gem::Requirement
28
+ requirement: &2153848420 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
- - - =
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.7.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2157653760
36
+ version_requirements: *2153848420
37
37
  description: libsvm and ruby without using swig
38
38
  email:
39
39
  - neovintage@gmail.com
@@ -44,11 +44,12 @@ extra_rdoc_files: []
44
44
  files:
45
45
  - .gitignore
46
46
  - .rvmrc
47
+ - .travis.yml
47
48
  - Gemfile
48
- - Gemfile.lock
49
49
  - MIT-LICENSE
50
- - README.textile
50
+ - README.md
51
51
  - Rakefile
52
+ - examples/toy.rb
52
53
  - ext/rb-libsvm/extconf.rb
53
54
  - ext/rb-libsvm/libsvm.c
54
55
  - ext/rb-libsvm/ruby-ext.h
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  version: '0'
85
86
  requirements: []
86
87
  rubyforge_project: rb-libsvm
87
- rubygems_version: 1.8.9
88
+ rubygems_version: 1.8.11
88
89
  signing_key:
89
90
  specification_version: 3
90
91
  summary: Ruby language bindings for LIBSVM
data/Gemfile.lock DELETED
@@ -1,28 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rb-libsvm (1.0.0)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- diff-lcs (1.1.3)
10
- rake (0.9.2.2)
11
- rake-compiler (0.7.9)
12
- rake
13
- rspec (2.7.0)
14
- rspec-core (~> 2.7.0)
15
- rspec-expectations (~> 2.7.0)
16
- rspec-mocks (~> 2.7.0)
17
- rspec-core (2.7.1)
18
- rspec-expectations (2.7.0)
19
- diff-lcs (~> 1.1.2)
20
- rspec-mocks (2.7.0)
21
-
22
- PLATFORMS
23
- ruby
24
-
25
- DEPENDENCIES
26
- rake-compiler
27
- rb-libsvm!
28
- rspec (= 2.7.0)
data/README.textile DELETED
@@ -1,37 +0,0 @@
1
- h1. rb-libsvm -- Ruby language bindings for LIBSVM
2
-
3
- This is a module which provides a Ruby API to the LIBSVM [1] library.
4
- SVM is a machine learning and classification algorithm, and LIBSVM is
5
- a popular free implementation of it, written by Chih-Chung Chang and
6
- Chih-Jen Lin, of National Taiwan University, Taipei. See "Programming
7
- Collective Intelligence," [2] among others, for a usage example.
8
-
9
- Note: There exists another Ruby binding for LIBSVM, named Ruby SVM,
10
- [3] written by Rudi Cilibrasi. (That's the one mentioned in the
11
- libsvm documentation.) This package is not related but written
12
- independently and from scratch.
13
-
14
- h2. Dependencies
15
-
16
- None. Libsvm is bundled with the project. Just clone and go!
17
-
18
- Currently using libsvm version 3.1
19
-
20
- h2. Author, License
21
-
22
- Written by C. Florian Ebeling. This software can be freely used under
23
- the terms of the MIT license, see file MIT-LICENSE.
24
-
25
- h2. Contributors
26
-
27
- Rimas Silkaitis
28
-
29
- h3. Notes
30
-
31
- fn1. http://www.csie.ntu.edu.tw/~cjlin/libsvm/
32
-
33
- fn2. http://books.google.com/books?id=fEsZ3Ey-Hq4C
34
-
35
- fn3. http://rubysvm.cilibrar.com/
36
-
37
-