liblinear-ruby 0.0.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -115
- data/lib/liblinear/array/double.rb +26 -0
- data/lib/liblinear/array/integer.rb +26 -0
- data/lib/liblinear/array.rb +15 -0
- data/lib/liblinear/error.rb +1 -1
- data/lib/liblinear/example.rb +29 -0
- data/lib/liblinear/feature_node.rb +40 -0
- data/lib/liblinear/feature_node_matrix.rb +23 -0
- data/lib/liblinear/model.rb +48 -83
- data/lib/liblinear/parameter.rb +72 -31
- data/lib/liblinear/problem.rb +40 -35
- data/lib/liblinear/version.rb +2 -2
- data/lib/liblinear.rb +98 -93
- data/spec/liblinear/array/double_spec.rb +21 -0
- data/spec/liblinear/example_spec.rb +17 -0
- data/spec/liblinear/feature_node_matrix_spec.rb +14 -0
- data/spec/liblinear/feature_node_spec.rb +14 -0
- data/spec/liblinear/model_spec.rb +23 -66
- data/spec/liblinear/parameter_spec.rb +46 -36
- data/spec/liblinear/problem_spec.rb +30 -8
- data/spec/liblinear_spec.rb +36 -76
- metadata +16 -3
- data/lib/liblinear/cross_validator.rb +0 -58
@@ -0,0 +1,17 @@
|
|
1
|
+
$: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
|
2
|
+
require 'liblinear'
|
3
|
+
|
4
|
+
describe Liblinear::Example do
|
5
|
+
describe '#max_feature_id' do
|
6
|
+
it 'returns max feature id' do
|
7
|
+
expect(Liblinear::Example.max_feature_id([[1, 4], [1, 2, 3, 100]])).to eq(4)
|
8
|
+
expect(Liblinear::Example.max_feature_id([{1 => 1, 2 => 2}, {100 => 100}])).to eq(100)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#array_to_hash' do
|
13
|
+
it 'returns hash' do
|
14
|
+
expect(Liblinear::Example.array_to_hash([1, 2, 3])).to eq({1 => 1, 2 => 2, 3 => 3})
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
|
2
|
+
require 'liblinear'
|
3
|
+
|
4
|
+
describe Liblinear::FeatureNodeMatrix do
|
5
|
+
before do
|
6
|
+
@feature_node_matrix = Liblinear::FeatureNodeMatrix.new([[1], [2]], -1)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#swig' do
|
10
|
+
it 'returns SWIG::TYPE_p_p_feature_node' do
|
11
|
+
expect(@feature_node_matrix.swig.class).to eq(SWIG::TYPE_p_p_feature_node)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
|
2
|
+
require 'liblinear'
|
3
|
+
|
4
|
+
describe Liblinear::FeatureNode do
|
5
|
+
before do
|
6
|
+
@feature_node = Liblinear::FeatureNode.new([1, 2], 2)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#swig' do
|
10
|
+
it 'returns Liblinearswig::Feature_node' do
|
11
|
+
expect(@feature_node.swig.class).to eq(Liblinearswig::Feature_node)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -3,99 +3,56 @@ require 'liblinear'
|
|
3
3
|
|
4
4
|
describe Liblinear::Model do
|
5
5
|
before do
|
6
|
-
@
|
7
|
-
@
|
8
|
-
@
|
9
|
-
|
10
|
-
@param_2 = Liblinear::Parameter.new({ solver_type: Liblinear::L2R_L2LOSS_SVR })
|
11
|
-
@model_regression = Liblinear::Model.new(@prob, @param_2)
|
6
|
+
@problem = Liblinear::Problem.new([1, 2], [[1],[2]])
|
7
|
+
@parameter = Liblinear::Parameter.new()
|
8
|
+
@model = Liblinear::Model.train(@problem, @parameter)
|
12
9
|
end
|
13
10
|
|
14
|
-
describe '#
|
15
|
-
it '
|
16
|
-
expect
|
17
|
-
Liblinear::Model.new(1, 2)
|
18
|
-
}.to raise_error(ArgumentError, 'arguments must be [Liblinear::Problem] and [Liblinear::Parameter]')
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'raise Liblinear::InvalidParameter when parameter is invalid' do
|
22
|
-
param = Liblinear::Parameter.new
|
23
|
-
param.C = -1
|
24
|
-
expect{
|
25
|
-
Liblinear::Model.new(@prob, param)
|
26
|
-
}.to raise_error(Liblinear::InvalidParameter, 'C <= 0')
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'raise ArgumentError when argument is not [String]' do
|
30
|
-
expect{
|
31
|
-
Liblinear::Model.new(1)
|
32
|
-
}.to raise_error(ArgumentError, 'argument must be [String]')
|
11
|
+
describe '#swig' do
|
12
|
+
it 'returns [Liblinearswig::Model]' do
|
13
|
+
expect(@model.swig.class).to eq(Liblinearswig::Model)
|
33
14
|
end
|
34
15
|
end
|
35
16
|
|
36
17
|
describe '#class_size' do
|
37
|
-
it 'returns
|
38
|
-
expect(@
|
18
|
+
it 'returns class size' do
|
19
|
+
expect(@model.class_size).to eq(2)
|
39
20
|
end
|
40
21
|
end
|
41
22
|
|
42
23
|
describe '#feature_size' do
|
43
|
-
it 'returns
|
44
|
-
expect(@
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe '#labels' do
|
49
|
-
it 'returns labels' do
|
50
|
-
expect(@model_classification.labels).to eq([1, 2])
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe '#predict' do
|
55
|
-
it 'returns predicted class' do
|
56
|
-
expect(@model_classification.predict([1]).class).to eq(Float)
|
24
|
+
it 'returns feature size' do
|
25
|
+
expect(@model.feature_size).to eq(1)
|
57
26
|
end
|
58
27
|
end
|
59
28
|
|
60
|
-
describe '#
|
61
|
-
it 'returns
|
62
|
-
expect(@
|
29
|
+
describe '#feature_weights' do
|
30
|
+
it 'returns feature weights' do
|
31
|
+
expect(@model.feature_weights.first.class).to eq(Float)
|
63
32
|
end
|
64
33
|
end
|
65
34
|
|
66
|
-
describe '#
|
67
|
-
it 'returns
|
68
|
-
expect(@
|
35
|
+
describe '#bias' do
|
36
|
+
it 'returns bias' do
|
37
|
+
expect(@model.bias).to eq(-1)
|
69
38
|
end
|
70
39
|
end
|
71
40
|
|
72
|
-
describe '#
|
73
|
-
it 'returns
|
74
|
-
expect(@
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'returns a coefficient' do
|
78
|
-
expect(@model_classification.coefficient(1).class).to eq(Float)
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'returns all coefficients' do
|
82
|
-
expect(@model_classification.coefficient.class).to eq(Array)
|
41
|
+
describe '#labels' do
|
42
|
+
it 'returns labels' do
|
43
|
+
expect(@model.labels).to eq([1, 2])
|
83
44
|
end
|
84
45
|
end
|
85
46
|
|
86
|
-
describe '#
|
87
|
-
it '
|
88
|
-
expect(@
|
47
|
+
describe '#probability_model?' do
|
48
|
+
it 'returns false' do
|
49
|
+
expect(@model.probability_model?).to eq(false)
|
89
50
|
end
|
90
51
|
end
|
91
52
|
|
92
53
|
describe '#regression_model?' do
|
93
|
-
it 'returns true' do
|
94
|
-
expect(@model_regression.regression_model?).to eq(true)
|
95
|
-
end
|
96
|
-
|
97
54
|
it 'returns false' do
|
98
|
-
expect(@
|
55
|
+
expect(@model.regression_model?).to eq(false)
|
99
56
|
end
|
100
57
|
end
|
101
58
|
end
|
@@ -2,56 +2,66 @@ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
|
|
2
2
|
require 'liblinear'
|
3
3
|
|
4
4
|
describe Liblinear::Parameter do
|
5
|
-
|
6
|
-
|
5
|
+
describe 'class method' do
|
6
|
+
describe '#default_epsilon' do
|
7
|
+
it 'returns default value of epsilon' do
|
8
|
+
expect(Liblinear::Parameter.default_epsilon(Liblinear::L2R_L2LOSS_SVR)).to eq(0.001)
|
9
|
+
end
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
|
-
describe '
|
10
|
-
|
11
|
-
@
|
12
|
-
|
13
|
+
describe 'instance method' do
|
14
|
+
before do
|
15
|
+
@parameter = Liblinear::Parameter.new({
|
16
|
+
solver_type: Liblinear::L2R_LR,
|
17
|
+
cost: 0.5,
|
18
|
+
sensitive_loss: 0.2,
|
19
|
+
epsilon: 0.5,
|
20
|
+
weight_labels: [1, 2],
|
21
|
+
weights: [0.1, 0.2],
|
22
|
+
})
|
13
23
|
end
|
14
|
-
end
|
15
24
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
25
|
+
describe '#swig' do
|
26
|
+
it 'returns [Liblinearswig::Parameter]' do
|
27
|
+
expect(@parameter.swig.class).to eq(Liblinearswig::Parameter)
|
28
|
+
end
|
20
29
|
end
|
21
|
-
end
|
22
30
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
describe '#solver_type' do
|
32
|
+
it 'returns solver type' do
|
33
|
+
expect(@parameter.solver_type).to eq(Liblinear::L2R_LR)
|
34
|
+
end
|
27
35
|
end
|
28
|
-
end
|
29
36
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
37
|
+
describe '#cost' do
|
38
|
+
it 'returns cost' do
|
39
|
+
expect(@parameter.cost).to eq(0.5)
|
40
|
+
end
|
34
41
|
end
|
35
|
-
end
|
36
42
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
43
|
+
describe '#sensitive_loss' do
|
44
|
+
it 'returns sensitive loss' do
|
45
|
+
expect(@parameter.sensitive_loss).to eq(0.2)
|
46
|
+
end
|
41
47
|
end
|
42
|
-
end
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
49
|
+
describe '#epsilon' do
|
50
|
+
it 'returns epsilon' do
|
51
|
+
expect(@parameter.epsilon).to eq(0.5)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#weight_labels' do
|
56
|
+
it 'returns weight labels' do
|
57
|
+
expect(@parameter.weight_labels).to eq([1, 2])
|
58
|
+
end
|
48
59
|
end
|
49
|
-
end
|
50
60
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
61
|
+
describe '#weights' do
|
62
|
+
it 'returns weights' do
|
63
|
+
expect(@parameter.weights).to eq([0.1, 0.2])
|
64
|
+
end
|
55
65
|
end
|
56
66
|
end
|
57
67
|
end
|
@@ -7,17 +7,39 @@ describe Liblinear::Problem do
|
|
7
7
|
@problem = Liblinear::Problem.new([1, 2], [[1],[2]])
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '#
|
11
|
-
it '
|
12
|
-
expect
|
13
|
-
Liblinear::Problem.new([1, 2], [[1]])
|
14
|
-
}.to raise_error(ArgumentError, 'labels and examples must be same size')
|
10
|
+
describe '#swig' do
|
11
|
+
it 'returns [Liblinearswig::Problem]' do
|
12
|
+
expect(@problem.swig.class).to eq(Liblinearswig::Problem)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
describe '#
|
19
|
-
it 'returns
|
20
|
-
expect(@problem.
|
16
|
+
describe '#example_size' do
|
17
|
+
it 'returns example size' do
|
18
|
+
expect(@problem.example_size).to eq(2)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#max_feature_id' do
|
23
|
+
it 'returns max feature id' do
|
24
|
+
expect(@problem.max_feature_id).to eq(1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#labels' do
|
29
|
+
it 'returns labels' do
|
30
|
+
expect(@problem.labels).to eq([1, 2])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#example_matrix' do
|
35
|
+
it 'returns example matrix' do
|
36
|
+
expect(@problem.example_matrix.class).to eq(Liblinear::FeatureNodeMatrix)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#bias' do
|
41
|
+
it 'returns bias' do
|
42
|
+
expect(@problem.bias).to eq(-1)
|
21
43
|
end
|
22
44
|
end
|
23
45
|
end
|
data/spec/liblinear_spec.rb
CHANGED
@@ -2,103 +2,63 @@ $: << File.expand_path(File.join(__FILE__, '..', '..', 'lib'))
|
|
2
2
|
require 'liblinear'
|
3
3
|
|
4
4
|
describe Liblinear do
|
5
|
-
include Liblinear
|
6
|
-
include Liblinearswig
|
7
|
-
|
8
5
|
before do
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@examples_array = [[1, 2], [3, 4, 5]]
|
15
|
-
@example_hash = {1=>1, 2=>2, 3=>3}
|
16
|
-
@example_array = [1, 2, 3]
|
6
|
+
@model = Liblinear.train(
|
7
|
+
{ solver_type: Liblinear::L2R_LR },
|
8
|
+
[1, 2],
|
9
|
+
[[-1, -1], [1, 1]],
|
10
|
+
)
|
17
11
|
end
|
18
12
|
|
19
13
|
describe 'solver type' do
|
20
14
|
it 'solver type equal integer value defined Liblinearswig' do
|
21
|
-
expect(Liblinear::L2R_LR).to
|
15
|
+
expect(Liblinear::L2R_LR).to eq(Liblinearswig::L2R_LR)
|
22
16
|
expect(Liblinear::L2R_L2LOSS_SVC_DUAL).to eq(Liblinearswig::L2R_L2LOSS_SVC_DUAL)
|
23
|
-
expect(Liblinear::L2R_L2LOSS_SVC).to
|
17
|
+
expect(Liblinear::L2R_L2LOSS_SVC).to eq(Liblinearswig::L2R_L2LOSS_SVC)
|
24
18
|
expect(Liblinear::L2R_L1LOSS_SVC_DUAL).to eq(Liblinearswig::L2R_L1LOSS_SVC_DUAL)
|
25
|
-
expect(Liblinear::MCSVM_CS).to
|
26
|
-
expect(Liblinear::L1R_L2LOSS_SVC).to
|
27
|
-
expect(Liblinear::L1R_LR).to
|
28
|
-
expect(Liblinear::L2R_LR_DUAL).to
|
29
|
-
expect(Liblinear::L2R_L2LOSS_SVR).to
|
19
|
+
expect(Liblinear::MCSVM_CS).to eq(Liblinearswig::MCSVM_CS)
|
20
|
+
expect(Liblinear::L1R_L2LOSS_SVC).to eq(Liblinearswig::L1R_L2LOSS_SVC)
|
21
|
+
expect(Liblinear::L1R_LR).to eq(Liblinearswig::L1R_LR)
|
22
|
+
expect(Liblinear::L2R_LR_DUAL).to eq(Liblinearswig::L2R_LR_DUAL)
|
23
|
+
expect(Liblinear::L2R_L2LOSS_SVR).to eq(Liblinearswig::L2R_L2LOSS_SVR)
|
30
24
|
expect(Liblinear::L2R_L2LOSS_SVR_DUAL).to eq(Liblinearswig::L2R_L2LOSS_SVR_DUAL)
|
31
25
|
expect(Liblinear::L2R_L1LOSS_SVR_DUAL).to eq(Liblinearswig::L2R_L1LOSS_SVR_DUAL)
|
32
26
|
end
|
33
27
|
end
|
34
28
|
|
35
|
-
describe '#
|
36
|
-
it 'returns
|
37
|
-
expect(
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
expect(int_array_c_to_ruby(@int_c_array, 3)).to eq(@int_ruby_array)
|
44
|
-
free_int_array(@int_c_array)
|
45
|
-
expect(int_array_c_to_ruby(@int_c_array, 3)).not_to eq(@int_ruby_array)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe '#new_double_array' do
|
50
|
-
it 'returns [SWIG::TYPE_p_double]' do
|
51
|
-
expect(new_double_array(@double_ruby_array).class).to eq(SWIG::TYPE_p_double)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe '#free_double_array' do
|
56
|
-
it 'returns different array when free c array' do
|
57
|
-
expect(double_array_c_to_ruby(@double_c_array, 3)).to eq(@double_ruby_array)
|
58
|
-
free_double_array(@double_c_array)
|
59
|
-
expect(double_array_c_to_ruby(@double_c_array, 3)).not_to eq(@double_ruby_array)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe '#int_array_c_to_ruby' do
|
64
|
-
it 'returns [Array<Integer>]' do
|
65
|
-
expect(int_array_c_to_ruby(@int_c_array, 3)).to eq(@int_ruby_array)
|
29
|
+
describe '#check_parameter' do
|
30
|
+
it 'returns error message' do
|
31
|
+
expect(
|
32
|
+
Liblinear.check_parameter(
|
33
|
+
Liblinear::Problem.new([1, 2], [[1], [2]]),
|
34
|
+
Liblinear::Parameter.new({cost: -1})
|
35
|
+
)
|
36
|
+
).to eq('C <= 0')
|
66
37
|
end
|
67
38
|
end
|
68
39
|
|
69
|
-
describe '#
|
70
|
-
it 'returns
|
71
|
-
expect(
|
40
|
+
describe '#cross_validation' do
|
41
|
+
it 'returns cross validation result' do
|
42
|
+
expect(
|
43
|
+
Liblinear.cross_validation(
|
44
|
+
2,
|
45
|
+
{},
|
46
|
+
[1, 2],
|
47
|
+
[[1], [-1]]
|
48
|
+
).class
|
49
|
+
).to eq(Array)
|
72
50
|
end
|
73
51
|
end
|
74
52
|
|
75
|
-
describe '#
|
76
|
-
it 'returns
|
77
|
-
expect(
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'returns max index + 1 when example is [Array]' do
|
81
|
-
expect(max_index(@examples_array)).to eq(3)
|
53
|
+
describe '#predict' do
|
54
|
+
it 'returns prediction' do
|
55
|
+
expect(Liblinear.predict(@model, [0.5, 0.5])).to eq(2.0)
|
82
56
|
end
|
83
57
|
end
|
84
58
|
|
85
|
-
describe '
|
86
|
-
it 'returns
|
87
|
-
expect(
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'raise ArgumentError when array is not [Array]' do
|
91
|
-
expect{array_to_hash(1)}.to raise_error(ArgumentError)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe '#convert_to_feature_node_array' do
|
96
|
-
it 'returns [Liblinearswig::Feature_node] when example is [Hash]' do
|
97
|
-
expect(convert_to_feature_node_array(@example_hash, 3).class).to eq(Liblinearswig::Feature_node)
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'returns [Liblinearswig::Feature_node] when examples is [Array]' do
|
101
|
-
expect(convert_to_feature_node_array(@example_array, 3).class).to eq(Liblinearswig::Feature_node)
|
59
|
+
describe 'labels' do
|
60
|
+
it 'returns labels' do
|
61
|
+
expect(Liblinear.labels(@model)).to eq([1, 2])
|
102
62
|
end
|
103
63
|
end
|
104
64
|
end
|