rb-libsvm 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +7 -0
- data/README.md +1 -1
- data/ext/libsvm/libsvm.c +5 -0
- data/lib/libsvm/node.rb +5 -3
- data/lib/libsvm/version.rb +1 -1
- data/spec/core_ext_spec.rb +3 -3
- data/spec/model_spec.rb +4 -0
- data/spec/node_spec.rb +24 -17
- data/spec/parameter_spec.rb +19 -20
- data/spec/problem_spec.rb +19 -15
- data/spec/usage_spec.rb +5 -5
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b45728dc2dd8a43e475299b21ca321ac440ec240
|
4
|
+
data.tar.gz: bb1e9e59c680fa2bff83e37b636f8354327e63fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04f4b3a390f705f41e78e1f6531af1117bfb944a1901fc17cee3e12a668d0ff58761ee6cbb69c59c474a66fc9549fdb1f9e1339b663e73b7d2e535232b8998ec
|
7
|
+
data.tar.gz: 27d948de88f60491aa3ff226f074411dd168d821193e9f1b43db5a1f78f5aa75b1ffa01c6021e846f34fa3022199fd369bb9a8f397920250537f51125e138838
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
|
+
## [1.4.0] - 2015-09-18
|
6
|
+
### Changed
|
7
|
+
- Complete transition to `expect` specs.
|
8
|
+
- Raise exception in `Libsvm::Model.load` if model can't be loaded (fixes #21).
|
9
|
+
### Fixed
|
10
|
+
- Fix a bug in `Libsvm::Node#==` when comparing with `nil`.
|
11
|
+
|
5
12
|
## [1.3.1] - 2015-03-21
|
6
13
|
### Fixed
|
7
14
|
- Spelling and punctuation in documentation comments
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ LIBSVM includes a number of command line tools for preprocessing
|
|
22
22
|
training data and finding parameters. These tools are not included in
|
23
23
|
this gem. You should install the original package if you need them.
|
24
24
|
|
25
|
-
It is helpful to consult the [README
|
25
|
+
It is helpful to consult the [README of the LIBSVM][README] package for
|
26
26
|
reference when configuring the training parameters.
|
27
27
|
|
28
28
|
Currently this package includes libsvm version 3.20.
|
data/ext/libsvm/libsvm.c
CHANGED
@@ -490,6 +490,8 @@ static VALUE cModel_support_vectors_count(VALUE obj)
|
|
490
490
|
* internal file representation of the model.
|
491
491
|
*
|
492
492
|
* @param filename [String] name of the model file
|
493
|
+
* @raise [IOError] if the model can't be loaded,
|
494
|
+
* e.g. because the model path doesn't point to a model
|
493
495
|
* @return [Libsvm::Model] the model
|
494
496
|
*/
|
495
497
|
static VALUE cModel_class_load(VALUE cls, VALUE filename)
|
@@ -498,6 +500,9 @@ static VALUE cModel_class_load(VALUE cls, VALUE filename)
|
|
498
500
|
char *path;
|
499
501
|
path = StringValueCStr(filename);
|
500
502
|
model = svm_load_model(path);
|
503
|
+
if(model == NULL) {
|
504
|
+
rb_raise(rb_eIOError, "unable to load model from file: \"%s\"", path);
|
505
|
+
}
|
501
506
|
return Data_Wrap_Struct(cModel, 0, model_free, model);
|
502
507
|
}
|
503
508
|
|
data/lib/libsvm/node.rb
CHANGED
@@ -90,13 +90,15 @@ module Libsvm
|
|
90
90
|
self.value = value
|
91
91
|
end
|
92
92
|
|
93
|
-
# Compare
|
93
|
+
# Compare feature node for equality.
|
94
94
|
#
|
95
95
|
# Nodes with equal index and value are equal.
|
96
96
|
#
|
97
97
|
# @return [Boolean]
|
98
|
-
def ==(other)
|
99
|
-
|
98
|
+
def == (other)
|
99
|
+
other.class == self.class &&
|
100
|
+
index == other.index &&
|
101
|
+
value == other.value
|
100
102
|
end
|
101
103
|
|
102
104
|
def inspect
|
data/lib/libsvm/version.rb
CHANGED
data/spec/core_ext_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Create examples" do
|
4
|
-
|
4
|
+
it "from a hash" do
|
5
5
|
example = { 11 => 0.11, 21 => 0.21, 101 => 0.99 }.to_example.sort_by(&:index)
|
6
|
-
example.
|
6
|
+
expect(example).to eq(Node.features({11 => 0.11, 21 => 0.21, 101 => 0.99 }).sort_by(&:index))
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "from an array of tuples" do
|
10
10
|
it "can create example from array of pairs" do
|
11
11
|
example = [ [11, 0.11], [21, 0.21], [101, 0.99] ].to_example
|
12
|
-
example.
|
12
|
+
expect(example).to eq(Node.features({11 => 0.11, 21 => 0.21, 101 => 0.99 }).sort_by(&:index))
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/spec/model_spec.rb
CHANGED
@@ -70,6 +70,10 @@ describe "A saved model" do
|
|
70
70
|
expect(Model.load(@filename)).to be_an_instance_of(Model)
|
71
71
|
end
|
72
72
|
|
73
|
+
it "missing model raises exception" do
|
74
|
+
expect{Model.load("missing.model")}.to raise_error(IOError, /unable to load model from file: "missing.model"/)
|
75
|
+
end
|
76
|
+
|
73
77
|
after(:each) do
|
74
78
|
File.delete(@filename) rescue nil
|
75
79
|
end
|
data/spec/node_spec.rb
CHANGED
@@ -28,43 +28,50 @@ describe "A Node" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "can be created" do
|
31
|
-
@node.
|
31
|
+
expect(@node).not_to be_nil
|
32
32
|
end
|
33
33
|
|
34
|
-
it "
|
34
|
+
it "has in :index property" do
|
35
35
|
@node.index = 99
|
36
|
-
@node.index.
|
36
|
+
expect(@node.index).to eq(99)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a :value porperty" do
|
37
40
|
@node.value = 3.141
|
38
|
-
@node.value.
|
41
|
+
expect(@node.value).to be_within(0.00001).of(3.141)
|
39
42
|
end
|
40
43
|
|
41
44
|
it "has inited properties" do
|
42
|
-
@node.index.
|
43
|
-
@node.value.
|
45
|
+
expect(@node.index).to eq(0)
|
46
|
+
expect(@node.value).to be_within(0.00001).of(0)
|
44
47
|
end
|
45
48
|
|
46
49
|
it "class can create nodes from an array" do
|
47
50
|
ary = Node.features([0.1, 0.2, 0.3, 0.4, 0.5])
|
48
|
-
ary.
|
49
|
-
ary.map
|
50
|
-
ary.map
|
51
|
+
expect(ary).to all(be_a(Node))
|
52
|
+
expect(ary.map(&:value).sort).to eq([0.1, 0.2, 0.3, 0.4, 0.5])
|
53
|
+
expect(ary.map(&:index).sort).to eq([0, 1, 2, 3, 4])
|
51
54
|
end
|
52
55
|
|
53
56
|
it "class can create nodes from variable parameters" do
|
54
57
|
ary = Node.features(0.1, 0.2, 0.3, 0.4, 0.5)
|
55
|
-
ary.
|
56
|
-
ary.map
|
57
|
-
ary.map
|
58
|
+
expect(ary).to all(be_a(Node))
|
59
|
+
expect(ary.map(&:value).sort).to eq([0.1, 0.2, 0.3, 0.4, 0.5])
|
60
|
+
expect(ary.map(&:index).sort).to eq([0, 1, 2, 3, 4])
|
58
61
|
end
|
59
62
|
|
60
63
|
it "class can create nodes from hash" do
|
61
64
|
ary = Node.features(3=>0.3, 5=>0.5, 6=>0.6, 10=>1.0)
|
62
|
-
ary.
|
63
|
-
ary.map
|
64
|
-
ary.map
|
65
|
+
expect(ary).to all(be_a(Node))
|
66
|
+
expect(ary.map(&:value).sort).to eq([0.3, 0.5, 0.6, 1.0])
|
67
|
+
expect(ary.map(&:index).sort).to eq([3, 5, 6, 10])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "compares with nil" do
|
71
|
+
expect(Node.new(1,2)).to_not eq(nil)
|
65
72
|
end
|
66
73
|
|
67
|
-
it "implements
|
68
|
-
Node[1, 0.1].
|
74
|
+
it "implements value equality" do
|
75
|
+
expect(Node[1, 0.1]).to eq(Node[1, 0.1])
|
69
76
|
end
|
70
77
|
end
|
data/spec/parameter_spec.rb
CHANGED
@@ -5,72 +5,71 @@ describe "A Parameter has accessors for" do
|
|
5
5
|
@p = Libsvm::SvmParameter.new
|
6
6
|
end
|
7
7
|
it "int svm_type" do
|
8
|
-
SvmType::C_SVC.
|
8
|
+
expect(SvmType::C_SVC).to eq(0)
|
9
9
|
@p.svm_type = SvmType::C_SVC
|
10
|
-
@p.svm_type.
|
10
|
+
expect(@p.svm_type).to eq(SvmType::C_SVC)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "int kernel_type" do
|
14
|
-
KernelType::RBF.
|
14
|
+
expect(KernelType::RBF).to eq(2)
|
15
15
|
@p.kernel_type = KernelType::RBF
|
16
|
-
@p.kernel_type.
|
16
|
+
expect(@p.kernel_type).to eq(KernelType::RBF)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "int degree" do
|
20
20
|
@p.degree = 99
|
21
|
-
@p.degree.
|
21
|
+
expect(@p.degree).to eq(99)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "double gamma" do
|
25
25
|
@p.gamma = 0.33
|
26
|
-
@p.gamma.
|
26
|
+
expect(@p.gamma).to eq(0.33)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "double coef0" do
|
30
30
|
@p.coef0 = 0.99
|
31
|
-
@p.coef0.
|
31
|
+
expect(@p.coef0).to eq(0.99)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "double cache_size" do
|
35
35
|
@p.cache_size = 0.77
|
36
|
-
@p.cache_size.
|
36
|
+
expect(@p.cache_size).to eq(0.77)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "double eps" do
|
40
40
|
@p.eps = 0.111
|
41
|
-
@p.eps.
|
41
|
+
expect(@p.eps).to eq(0.111)
|
42
42
|
@p.eps = 0.112
|
43
|
-
@p.eps.
|
43
|
+
expect(@p.eps).to eq(0.112)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "double C" do
|
47
47
|
@p.c = 3.141
|
48
|
-
@p.c.
|
48
|
+
expect(@p.c).to eq(3.141)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "can set and read weights (weight, weight_label, nr_weight members from struct)" do
|
52
|
-
@p.label_weights = {1=> 1.2, 3=>0.2, 5=>0.888}
|
53
|
-
@p.label_weights.
|
52
|
+
@p.label_weights = {1 => 1.2, 3 => 0.2, 5 => 0.888}
|
53
|
+
expect(@p.label_weights).to eq({1 => 1.2, 3 => 0.2, 5 => 0.888})
|
54
54
|
end
|
55
55
|
|
56
|
-
|
57
56
|
it "double nu" do
|
58
57
|
@p.nu = 1.1
|
59
|
-
@p.nu.
|
58
|
+
expect(@p.nu).to eq(1.1)
|
60
59
|
end
|
61
60
|
|
62
61
|
it "double p" do
|
63
62
|
@p.p = 0.123
|
64
|
-
@p.p.
|
63
|
+
expect(@p.p).to eq(0.123)
|
65
64
|
end
|
66
65
|
|
67
|
-
it "int shrinking" do
|
66
|
+
it "boolean-as-int shrinking" do
|
68
67
|
@p.shrinking = 22
|
69
|
-
@p.shrinking.
|
68
|
+
expect(@p.shrinking).to eq(22)
|
70
69
|
end
|
71
70
|
|
72
|
-
it "int probability" do
|
71
|
+
it "boolean-as-int probability" do
|
73
72
|
@p.probability = 35
|
74
|
-
@p.probability.
|
73
|
+
expect(@p.probability).to eq(35)
|
75
74
|
end
|
76
75
|
end
|
data/spec/problem_spec.rb
CHANGED
@@ -12,26 +12,30 @@ describe "A Problem" do
|
|
12
12
|
it "examples get stored and retrieved" do
|
13
13
|
@problem.set_examples([1,2,1,2], @features)
|
14
14
|
labels, examples = @problem.examples
|
15
|
-
labels.size.
|
16
|
-
examples.size.
|
17
|
-
examples.map {|x|x.size}.
|
18
|
-
examples.first.map {|node| node.index}.
|
19
|
-
examples.first.map {|node| node.value}.
|
15
|
+
expect(labels.size).to eq(4)
|
16
|
+
expect(examples.size).to eq(4)
|
17
|
+
expect(examples.map {|x|x.size}).to eq([4,4,4,4])
|
18
|
+
expect(examples.first.map {|node| node.index}).to eq([0,1,2,3])
|
19
|
+
expect(examples.first.map {|node| node.value}).to eq([0.2,0.3,0.4,0.4])
|
20
20
|
end
|
21
21
|
|
22
22
|
it "can be populated" do
|
23
|
-
examples = [Node.features(0.2,0.3,0.4,0.4),
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
examples = [ Node.features(0.2,0.3,0.4,0.4),
|
24
|
+
Node.features(0.1,0.5,0.1,0.9),
|
25
|
+
Node.features(0.2,0.2,0.6,0.5),
|
26
|
+
Node.features(0.3,0.1,0.5,0.9) ]
|
27
|
+
expect {
|
28
|
+
@problem.set_examples([1,2,1,2], examples)
|
29
|
+
}.to_not raise_error
|
28
30
|
end
|
29
31
|
|
30
|
-
it "can be set twice
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
it "can be set twice" do
|
33
|
+
expect {
|
34
|
+
features = [Node.features(0.2, 0.3, 0.4, 0.4), Node.features(0.3,0.1,0.5,0.9)]
|
35
|
+
@problem.set_examples([1,2], features)
|
36
|
+
features = [Node.features(0.2, 0.3, 0.4, 0.4), Node.features(0.3,0.1,0.5,0.9)]
|
37
|
+
@problem.set_examples([8,2], features)
|
38
|
+
}.to_not raise_error
|
35
39
|
end
|
36
40
|
|
37
41
|
end
|
data/spec/usage_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe "Basic usage" do
|
|
15
15
|
|
16
16
|
it "has a nice API" do
|
17
17
|
example = {11 => 0.11, 21 => 0.21, 101 => 0.99 }.to_example
|
18
|
-
example.
|
18
|
+
expect(example).to eq(Node.features({11 => 0.11, 21 => 0.21, 101 => 0.99 }))
|
19
19
|
end
|
20
20
|
|
21
21
|
it "is as in [PCI,217]" do
|
@@ -26,13 +26,13 @@ describe "Basic usage" do
|
|
26
26
|
model = Model.train(@problem, @parameter)
|
27
27
|
|
28
28
|
pred = model.predict(Node.features(1, 1, 1))
|
29
|
-
pred.
|
29
|
+
expect(pred).to eq(1.0)
|
30
30
|
|
31
31
|
pred = model.predict(Node.features(-1, 1, -1))
|
32
|
-
pred.
|
32
|
+
expect(pred).to eq(-1.0)
|
33
33
|
|
34
34
|
pred = model.predict(Node.features(-1, 55, -1))
|
35
|
-
pred.
|
35
|
+
expect(pred).to eq(-1.0)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "kernel parameter use" do
|
@@ -42,6 +42,6 @@ describe "Basic usage" do
|
|
42
42
|
|
43
43
|
model = Model.train(@problem, @parameter)
|
44
44
|
|
45
|
-
model.predict(Node.features(1, 2, 3)).
|
45
|
+
expect(model.predict(Node.features(1, 2, 3))).to eq(2)
|
46
46
|
end
|
47
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.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- C. Florian Ebeling
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-09-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
requirements: []
|
102
102
|
rubyforge_project: rb-libsvm
|
103
|
-
rubygems_version: 2.
|
103
|
+
rubygems_version: 2.4.5
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: Ruby bindings for LIBSVM
|
@@ -112,4 +112,3 @@ test_files:
|
|
112
112
|
- spec/problem_spec.rb
|
113
113
|
- spec/spec_helper.rb
|
114
114
|
- spec/usage_spec.rb
|
115
|
-
has_rdoc:
|