liblinear-ruby 0.0.7 → 1.0.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.
@@ -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
- @prob = Liblinear::Problem.new([1, 2], [[1],[2]])
7
- @param_1 = Liblinear::Parameter.new
8
- @model_classification = Liblinear::Model.new(@prob, @param_1)
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 '#initialize' do
15
- it 'raise ArgumentError when arg_1 not equal [Liblinear::Problem] or arg_2 not equal [Liblinear::Parameter]' do
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 the number of classes' do
38
- expect(@model_classification.class_size).to eq(2)
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 the number of features' do
44
- expect(@model_classification.feature_size).to eq(1)
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 '#predict_probability' do
61
- it 'returns predict_probability' do
62
- expect(@model_classification.predict_probability([1]).class).to eq(Hash)
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 '#predict_values' do
67
- it 'returns predict_values' do
68
- expect(@model_classification.predict_values([1]).class).to eq(Hash)
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 '#coefficient' do
73
- it 'returns a coefficient' do
74
- expect(@model_classification.coefficient(1, 0).class).to eq(Float)
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 '#bias' do
87
- it 'return a bias' do
88
- expect(@model_classification.bias.class).to eq(Float)
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(@model_classification.regression_model?).to eq (false)
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
- before do
6
- @param = Liblinear::Parameter.new
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 '#solver_type' do
10
- it 'returns solver type' do
11
- @param.solver_type = 0
12
- expect(@param.solver_type).to eq(Liblinear::L2R_LR)
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
- describe '#c' do
17
- it 'returns C' do
18
- @param.C = 2
19
- expect(@param.C).to eq(2)
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
- describe '#eps' do
24
- it 'returns eps' do
25
- @param.eps = 0.0
26
- expect(@param.eps).to eq(0.0)
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
- describe '#p' do
31
- it 'returns p' do
32
- @param.p = 0.0
33
- expect(@param.p).to eq(0.0)
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
- describe '#nr_weight' do
38
- it 'returns nr_weight' do
39
- @param.nr_weight = 3
40
- expect(@param.nr_weight).to eq(3)
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
- describe '#weight_label' do
45
- it 'returns weight_label' do
46
- @param.weight_label = [1, 2, 3]
47
- expect(@param.weight_label.class).to eq(SWIG::TYPE_p_int)
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
- describe '#weight' do
52
- it 'returns weight' do
53
- @param.weight = [1.0, 2.0, 3.0]
54
- expect(@param.weight.class).to eq(SWIG::TYPE_p_double)
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 '#initialize' do
11
- it 'raise ArgumentError when size of labels is different from that of examples' do
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 '#prob' do
19
- it 'returns an instance of [Liblinearswig::Problem]' do
20
- expect(@problem.prob.class).to eq(Liblinearswig::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
@@ -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
- @int_ruby_array = [1, 2, 3]
10
- @double_ruby_array = [1.0, 2.0, 3.0]
11
- @int_c_array = new_int_array(@int_ruby_array)
12
- @double_c_array = new_double_array(@double_ruby_array)
13
- @examples_hash = [{1=>1, 2=>2}, {3=>3, 4=>4}]
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 eq(Liblinearswig::L2R_LR)
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 eq(Liblinearswig::L2R_L2LOSS_SVC)
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 eq(Liblinearswig::MCSVM_CS)
26
- expect(Liblinear::L1R_L2LOSS_SVC).to eq(Liblinearswig::L1R_L2LOSS_SVC)
27
- expect(Liblinear::L1R_LR).to eq(Liblinearswig::L1R_LR)
28
- expect(Liblinear::L2R_LR_DUAL).to eq(Liblinearswig::L2R_LR_DUAL)
29
- expect(Liblinear::L2R_L2LOSS_SVR).to eq(Liblinearswig::L2R_L2LOSS_SVR)
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 '#new_int_array' do
36
- it 'returns [SWIG::TYPE_p_int]' do
37
- expect(new_int_array(@int_ruby_array).class).to eq(SWIG::TYPE_p_int)
38
- end
39
- end
40
-
41
- describe '#free_int_array' do
42
- it 'returns different array when free c array' do
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 '#double_array_c_to_ruby' do
70
- it 'returns [Array<Double>]' do
71
- expect(double_array_c_to_ruby(@double_c_array, 3)).to eq(@double_ruby_array)
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 '#max_index' do
76
- it 'returns max key when example is [Hash]' do
77
- expect(max_index(@examples_hash)).to eq(4)
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 '#array_to_hash' do
86
- it 'returns hash whose key is index + 1 of array' do
87
- expect(array_to_hash(@example_array)).to eq(@example_hash)
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