liblinear-ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +141 -10
  3. data/{liblinear-1.93/ruby → ext}/liblinear.i +0 -0
  4. data/ext/linear.cpp +30 -7
  5. data/ext/tron.cpp +4 -4
  6. data/lib/liblinear/model.rb +3 -3
  7. data/lib/liblinear/parameter.rb +2 -2
  8. data/lib/liblinear/problem.rb +4 -17
  9. data/lib/liblinear/version.rb +1 -1
  10. data/lib/liblinear.rb +46 -13
  11. data/{liblinear-1.93 → liblinear-1.94}/COPYRIGHT +0 -0
  12. data/{liblinear-1.93 → liblinear-1.94}/Makefile +0 -0
  13. data/{liblinear-1.93 → liblinear-1.94}/Makefile.win +0 -0
  14. data/{liblinear-1.93 → liblinear-1.94}/README +0 -0
  15. data/{liblinear-1.93 → liblinear-1.94}/blas/Makefile +0 -0
  16. data/{liblinear-1.93 → liblinear-1.94}/blas/blas.h +0 -0
  17. data/{liblinear-1.93 → liblinear-1.94}/blas/blasp.h +0 -0
  18. data/{liblinear-1.93 → liblinear-1.94}/blas/daxpy.c +0 -0
  19. data/{liblinear-1.93 → liblinear-1.94}/blas/ddot.c +0 -0
  20. data/{liblinear-1.93 → liblinear-1.94}/blas/dnrm2.c +0 -0
  21. data/{liblinear-1.93 → liblinear-1.94}/blas/dscal.c +0 -0
  22. data/{liblinear-1.93 → liblinear-1.94}/heart_scale +0 -0
  23. data/{liblinear-1.93 → liblinear-1.94}/linear.cpp +30 -7
  24. data/{liblinear-1.93 → liblinear-1.94}/linear.def +0 -0
  25. data/{liblinear-1.93 → liblinear-1.94}/linear.h +0 -0
  26. data/{liblinear-1.93 → liblinear-1.94}/matlab/Makefile +0 -0
  27. data/{liblinear-1.93 → liblinear-1.94}/matlab/README +2 -1
  28. data/{liblinear-1.93 → liblinear-1.94}/matlab/libsvmread.c +25 -24
  29. data/{liblinear-1.93 → liblinear-1.94}/matlab/libsvmwrite.c +15 -1
  30. data/{liblinear-1.93 → liblinear-1.94}/matlab/linear_model_matlab.c +0 -0
  31. data/{liblinear-1.93 → liblinear-1.94}/matlab/linear_model_matlab.h +0 -0
  32. data/{liblinear-1.93 → liblinear-1.94}/matlab/make.m +0 -0
  33. data/{liblinear-1.93 → liblinear-1.94}/matlab/predict.c +37 -25
  34. data/{liblinear-1.93 → liblinear-1.94}/matlab/train.c +16 -7
  35. data/{liblinear-1.93 → liblinear-1.94}/predict.c +0 -0
  36. data/{liblinear-1.93 → liblinear-1.94}/python/Makefile +0 -0
  37. data/{liblinear-1.93 → liblinear-1.94}/python/README +1 -1
  38. data/{liblinear-1.93 → liblinear-1.94}/python/liblinear.py +1 -1
  39. data/{liblinear-1.93 → liblinear-1.94}/python/liblinearutil.py +0 -0
  40. data/{liblinear-1.93 → liblinear-1.94}/train.c +0 -0
  41. data/{liblinear-1.93 → liblinear-1.94}/tron.cpp +4 -4
  42. data/{liblinear-1.93 → liblinear-1.94}/tron.h +0 -0
  43. data/{liblinear-1.93 → liblinear-1.94}/windows/liblinear.dll +0 -0
  44. data/{liblinear-1.93 → liblinear-1.94}/windows/libsvmread.mexw64 +0 -0
  45. data/{liblinear-1.93 → liblinear-1.94}/windows/libsvmwrite.mexw64 +0 -0
  46. data/{liblinear-1.93 → liblinear-1.94}/windows/predict.exe +0 -0
  47. data/liblinear-1.94/windows/predict.mexw64 +0 -0
  48. data/{liblinear-1.93 → liblinear-1.94}/windows/train.exe +0 -0
  49. data/liblinear-1.94/windows/train.mexw64 +0 -0
  50. data/spec/liblinear/model_spec.rb +56 -0
  51. data/spec/liblinear/parameter_spec.rb +57 -0
  52. data/spec/liblinear/problem_spec.rb +23 -0
  53. data/spec/liblinear_spec.rb +104 -0
  54. metadata +51 -56
  55. data/ext/linear.rb +0 -357
  56. data/liblinear-1.93/blas/blas.a +0 -0
  57. data/liblinear-1.93/blas/daxpy.o +0 -0
  58. data/liblinear-1.93/blas/ddot.o +0 -0
  59. data/liblinear-1.93/blas/dnrm2.o +0 -0
  60. data/liblinear-1.93/blas/dscal.o +0 -0
  61. data/liblinear-1.93/linear.o +0 -0
  62. data/liblinear-1.93/predict +0 -0
  63. data/liblinear-1.93/ruby/liblinear_wrap.cxx +0 -4646
  64. data/liblinear-1.93/ruby/linear.h +0 -74
  65. data/liblinear-1.93/ruby/linear.o +0 -0
  66. data/liblinear-1.93/train +0 -0
  67. data/liblinear-1.93/tron.o +0 -0
  68. data/liblinear-1.93/windows/predict.mexw64 +0 -0
  69. data/liblinear-1.93/windows/train.mexw64 +0 -0
@@ -0,0 +1,104 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', 'lib'))
2
+ require 'liblinear'
3
+
4
+ describe Liblinear do
5
+ include Liblinear
6
+ include Liblinearswig
7
+
8
+ 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]
17
+ end
18
+
19
+ describe 'solver type' do
20
+ it 'solver type equal integer value defined Liblinearswig' do
21
+ expect(Liblinear::L2R_LR).to eq(Liblinearswig::L2R_LR)
22
+ 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)
24
+ 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)
30
+ expect(Liblinear::L2R_L2LOSS_SVR_DUAL).to eq(Liblinearswig::L2R_L2LOSS_SVR_DUAL)
31
+ expect(Liblinear::L2R_L1LOSS_SVR_DUAL).to eq(Liblinearswig::L2R_L1LOSS_SVR_DUAL)
32
+ end
33
+ end
34
+
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)
66
+ end
67
+ end
68
+
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)
72
+ end
73
+ end
74
+
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)
82
+ end
83
+ end
84
+
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)
102
+ end
103
+ end
104
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liblinear-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kei Tsuchiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-19 00:00:00.000000000 Z
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,10 +58,10 @@ files:
58
58
  - ext/dnrm2.c
59
59
  - ext/dscal.c
60
60
  - ext/extconf.rb
61
+ - ext/liblinear.i
61
62
  - ext/liblinear_wrap.cxx
62
63
  - ext/linear.cpp
63
64
  - ext/linear.h
64
- - ext/linear.rb
65
65
  - ext/tron.cpp
66
66
  - ext/tron.h
67
67
  - lib/liblinear.rb
@@ -70,59 +70,50 @@ files:
70
70
  - lib/liblinear/parameter.rb
71
71
  - lib/liblinear/problem.rb
72
72
  - lib/liblinear/version.rb
73
- - liblinear-1.93/COPYRIGHT
74
- - liblinear-1.93/Makefile
75
- - liblinear-1.93/Makefile.win
76
- - liblinear-1.93/README
77
- - liblinear-1.93/blas/Makefile
78
- - liblinear-1.93/blas/blas.a
79
- - liblinear-1.93/blas/blas.h
80
- - liblinear-1.93/blas/blasp.h
81
- - liblinear-1.93/blas/daxpy.c
82
- - liblinear-1.93/blas/daxpy.o
83
- - liblinear-1.93/blas/ddot.c
84
- - liblinear-1.93/blas/ddot.o
85
- - liblinear-1.93/blas/dnrm2.c
86
- - liblinear-1.93/blas/dnrm2.o
87
- - liblinear-1.93/blas/dscal.c
88
- - liblinear-1.93/blas/dscal.o
89
- - liblinear-1.93/heart_scale
90
- - liblinear-1.93/linear.cpp
91
- - liblinear-1.93/linear.def
92
- - liblinear-1.93/linear.h
93
- - liblinear-1.93/linear.o
94
- - liblinear-1.93/matlab/Makefile
95
- - liblinear-1.93/matlab/README
96
- - liblinear-1.93/matlab/libsvmread.c
97
- - liblinear-1.93/matlab/libsvmwrite.c
98
- - liblinear-1.93/matlab/linear_model_matlab.c
99
- - liblinear-1.93/matlab/linear_model_matlab.h
100
- - liblinear-1.93/matlab/make.m
101
- - liblinear-1.93/matlab/predict.c
102
- - liblinear-1.93/matlab/train.c
103
- - liblinear-1.93/predict
104
- - liblinear-1.93/predict.c
105
- - liblinear-1.93/python/Makefile
106
- - liblinear-1.93/python/README
107
- - liblinear-1.93/python/liblinear.py
108
- - liblinear-1.93/python/liblinearutil.py
109
- - liblinear-1.93/ruby/liblinear.i
110
- - liblinear-1.93/ruby/liblinear_wrap.cxx
111
- - liblinear-1.93/ruby/linear.h
112
- - liblinear-1.93/ruby/linear.o
113
- - liblinear-1.93/train
114
- - liblinear-1.93/train.c
115
- - liblinear-1.93/tron.cpp
116
- - liblinear-1.93/tron.h
117
- - liblinear-1.93/tron.o
118
- - liblinear-1.93/windows/liblinear.dll
119
- - liblinear-1.93/windows/libsvmread.mexw64
120
- - liblinear-1.93/windows/libsvmwrite.mexw64
121
- - liblinear-1.93/windows/predict.exe
122
- - liblinear-1.93/windows/predict.mexw64
123
- - liblinear-1.93/windows/train.exe
124
- - liblinear-1.93/windows/train.mexw64
73
+ - liblinear-1.94/COPYRIGHT
74
+ - liblinear-1.94/Makefile
75
+ - liblinear-1.94/Makefile.win
76
+ - liblinear-1.94/README
77
+ - liblinear-1.94/blas/Makefile
78
+ - liblinear-1.94/blas/blas.h
79
+ - liblinear-1.94/blas/blasp.h
80
+ - liblinear-1.94/blas/daxpy.c
81
+ - liblinear-1.94/blas/ddot.c
82
+ - liblinear-1.94/blas/dnrm2.c
83
+ - liblinear-1.94/blas/dscal.c
84
+ - liblinear-1.94/heart_scale
85
+ - liblinear-1.94/linear.cpp
86
+ - liblinear-1.94/linear.def
87
+ - liblinear-1.94/linear.h
88
+ - liblinear-1.94/matlab/Makefile
89
+ - liblinear-1.94/matlab/README
90
+ - liblinear-1.94/matlab/libsvmread.c
91
+ - liblinear-1.94/matlab/libsvmwrite.c
92
+ - liblinear-1.94/matlab/linear_model_matlab.c
93
+ - liblinear-1.94/matlab/linear_model_matlab.h
94
+ - liblinear-1.94/matlab/make.m
95
+ - liblinear-1.94/matlab/predict.c
96
+ - liblinear-1.94/matlab/train.c
97
+ - liblinear-1.94/predict.c
98
+ - liblinear-1.94/python/Makefile
99
+ - liblinear-1.94/python/README
100
+ - liblinear-1.94/python/liblinear.py
101
+ - liblinear-1.94/python/liblinearutil.py
102
+ - liblinear-1.94/train.c
103
+ - liblinear-1.94/tron.cpp
104
+ - liblinear-1.94/tron.h
105
+ - liblinear-1.94/windows/liblinear.dll
106
+ - liblinear-1.94/windows/libsvmread.mexw64
107
+ - liblinear-1.94/windows/libsvmwrite.mexw64
108
+ - liblinear-1.94/windows/predict.exe
109
+ - liblinear-1.94/windows/predict.mexw64
110
+ - liblinear-1.94/windows/train.exe
111
+ - liblinear-1.94/windows/train.mexw64
125
112
  - liblinear-ruby.gemspec
113
+ - spec/liblinear/model_spec.rb
114
+ - spec/liblinear/parameter_spec.rb
115
+ - spec/liblinear/problem_spec.rb
116
+ - spec/liblinear_spec.rb
126
117
  homepage: https://github.com/kei500/liblinear-ruby
127
118
  licenses:
128
119
  - MIT
@@ -147,5 +138,9 @@ rubygems_version: 2.0.2
147
138
  signing_key:
148
139
  specification_version: 4
149
140
  summary: Ruby wrapper of LIBLINEAR using SWIG
150
- test_files: []
141
+ test_files:
142
+ - spec/liblinear/model_spec.rb
143
+ - spec/liblinear/parameter_spec.rb
144
+ - spec/liblinear/problem_spec.rb
145
+ - spec/liblinear_spec.rb
151
146
  has_rdoc:
data/ext/linear.rb DELETED
@@ -1,357 +0,0 @@
1
- require 'liblinear'
2
- include Liblinear
3
-
4
- def _int_array(seq)
5
- size = seq.size
6
- array = new_int(size)
7
- i = 0
8
- for item in seq
9
- int_setitem(array,i,item)
10
- i = i + 1
11
- end
12
- return array
13
- end
14
-
15
- def _double_array(seq)
16
- size = seq.size
17
- array = new_double(size)
18
- i = 0
19
- for item in seq
20
- double_setitem(array,i,item)
21
- i = i + 1
22
- end
23
- return array
24
- end
25
-
26
- def _free_int_array(x)
27
- if !x.nil? # and !x.empty?
28
- delete_int(x)
29
- end
30
- end
31
-
32
- def _free_double_array(x)
33
- if !x.nil? # and !x.empty?
34
- delete_double(x)
35
- end
36
- end
37
-
38
- def _int_array_to_list(x,n)
39
- list = []
40
- (0..n-1).each {|i| list << int_getitem(x,i) }
41
- return list
42
- end
43
-
44
- def _double_array_to_list(x,n)
45
- list = []
46
- (0..n-1).each {|i| list << double_getitem(x,i) }
47
- return list
48
- end
49
-
50
- class LParameter
51
- attr_accessor :param
52
-
53
- def initialize(*args)
54
- @param = Liblinear::Parameter.new
55
- @param.solver_type = L2R_LR
56
- @param.C = 1
57
- @param.eps = 0.01
58
- @param.nr_weight = 0
59
- @param.weight_label = _int_array([])
60
- @param.weight = _double_array([])
61
-
62
- args[0].each {|k,v|
63
- self.send("#{k}=",v)
64
- } if !args[0].nil?
65
- end
66
-
67
- def method_missing(m, *args)
68
- #print m.to_s
69
- #puts args.inspect
70
- if m.to_s == 'weight_label='
71
- @weight_label_len = args[0].size
72
- pargs = _int_array(args[0])
73
- _free_int_array(@param.weight_label)
74
- elsif m.to_s == 'weight='
75
- @weight_len = args[0].size
76
- pargs = _double_array(args[0])
77
- _free_double_array(@param.weight)
78
- else
79
- pargs = args[0]
80
- end
81
-
82
- if m.to_s.index('=')
83
- @param.send("#{m}",pargs)
84
- else
85
- @param.send("#{m}")
86
- end
87
-
88
- end
89
-
90
- def inspect
91
- "LParameter: solver_type=#{@param.solver_type} C=#{@param.C} eps=#{@param.eps}"
92
- end
93
-
94
- def destroy
95
- _free_int_array(@param.weight_label)
96
- _free_double_array(@param.weight)
97
- delete_parameter(@param)
98
- @param = nil
99
- end
100
- end
101
-
102
- def _convert_to_feature_node_array(x, maxlen, bias=-1)
103
- # convert a sequence or mapping to an feature_node array
104
-
105
- # Find non zero elements
106
- iter_range = []
107
- if x.class == Hash
108
- x.each {|k, v|
109
- # all zeros kept due to the precomputed kernel; no good solution yet
110
- iter_range << k #if v != 0
111
- }
112
- elsif x.class == Array
113
- x.each_index {|j|
114
- iter_range << j #if x[j] != 0
115
- }
116
- else
117
- raise TypeError,"data must be a hash or an array"
118
- end
119
-
120
- iter_range.sort!
121
- if bias >=0
122
- data = feature_node_array(iter_range.size+2)
123
- #puts "bias element (#{iter_range.size},#{bias})"
124
- feature_node_array_set(data,iter_range.size,maxlen+1,bias)
125
- feature_node_array_set(data,iter_range.size+1,-1,0)
126
- else
127
- data = feature_node_array(iter_range.size+1)
128
- feature_node_array_set(data,iter_range.size,-1,0)
129
- end
130
-
131
- j = 0
132
- for k in iter_range
133
- #puts "element #{j}= (#{k},#{x[k]})"
134
- feature_node_array_set(data,j,k,x[k])
135
- j = j + 1
136
- end
137
- return data
138
- end
139
-
140
-
141
- class LProblem
142
- attr_accessor :prob, :maxlen, :size
143
-
144
- def initialize(y,x,bias)
145
- # assert_equal(y.size, x.size)
146
- @prob = prob = Liblinear::Problem.new
147
- @size = size = y.size
148
-
149
- @y_array = y_array = new_double(size)
150
- for i in (0..size-1)
151
- double_setitem(@y_array,i,y[i])
152
- end
153
-
154
- @x_matrix = x_matrix = feature_node_matrix(size)
155
- @data = []
156
- @maxlen = 0 #max number of features
157
- len_array=[]
158
-
159
- for i in (0..size-1)
160
- data = _convert_to_feature_node_array(x[i], @maxlen, bias)
161
- @data << data
162
- feature_node_matrix_set(x_matrix,i,data)
163
-
164
- if x[i].class == Hash
165
- if x[i].size > 0
166
- @maxlen = [@maxlen,x[i].keys.max].max
167
- end
168
- else
169
- @maxlen = [@maxlen,x[i].size].max
170
- end
171
- len_array << x[i].size
172
- end
173
-
174
- if bias >= 0
175
- set_bias_index(x_matrix, size, @maxlen, _int_array(len_array))
176
- end
177
-
178
- prob.y = y_array
179
- prob.x = x_matrix
180
- prob.bias = bias
181
- prob.l = size
182
- prob.n = @maxlen
183
- if bias >= 0
184
- prob.n += 1
185
- end
186
- end
187
-
188
- def inspect
189
- "LProblem: size = #{size} n=#{prob.n} bias=#{prob.bias} maxlen=#{@maxlen}"
190
- end
191
-
192
- def destroy
193
- delete_problem(@prob)
194
- delete_int(@y_array)
195
- for i in (0..size-1)
196
- feature_node_array_destroy(@data[i])
197
- end
198
- feature_node_matrix_destroy(@x_matrix)
199
- end
200
- end
201
-
202
- class LModel
203
- attr_accessor :model, :probability
204
-
205
- def initialize(arg1,arg2=nil)
206
- if arg2 == nil
207
- # create model from file
208
- filename = arg1
209
- @model = load_model(filename)
210
- else
211
- # create model from problem and parameter
212
- prob,param = arg1,arg2
213
- @prob = prob
214
- msg = check_parameter(prob.prob,param.param)
215
- raise "ValueError", msg if msg
216
- @model = Liblinear::train(prob.prob,param.param)
217
- end
218
- #setup some classwide variables
219
- @nr_class = Liblinear::get_nr_class(@model)
220
- #create labels(classes)
221
- intarr = new_int(@nr_class)
222
- Liblinear::get_labels(@model,intarr)
223
- @labels = _int_array_to_list(intarr, @nr_class)
224
- delete_int(intarr)
225
- end
226
-
227
- def predict(x)
228
- data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
229
- ret = Liblinear::predict(@model,data)
230
- feature_node_array_destroy(data)
231
- return ret
232
- end
233
-
234
-
235
- def get_nr_class
236
- return @nr_class
237
- end
238
-
239
- def get_labels
240
- return @labels
241
- end
242
-
243
- def predict_values_raw(x)
244
- #convert x into feature_node, allocate a double array for return
245
- n = (@nr_class*(@nr_class-1)/2).floor
246
- data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
247
- dblarr = new_double(n)
248
- Liblinear::predict_values(@model, data, dblarr)
249
- ret = _double_array_to_list(dblarr, n)
250
- delete_double(dblarr)
251
- feature_node_array_destroy(data)
252
- return ret
253
- end
254
-
255
- def predict_values(x)
256
- v=predict_values_raw(x)
257
- #puts v.inspect
258
- if false
259
- #if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS
260
- return v[0]
261
- else #self.svm_type == C_SVC or self.svm_type == NU_SVC
262
- count = 0
263
-
264
- # create a width x height array
265
- width = @labels.size
266
- height = @labels.size
267
- d = Array.new(width)
268
- d.map! { Array.new(height) }
269
-
270
- for i in (0..@labels.size-1)
271
- for j in (i+1..@labels.size-1)
272
- d[@labels[i]][@labels[j]] = v[count]
273
- d[@labels[j]][@labels[i]] = -v[count]
274
- count += 1
275
- end
276
- end
277
- return d
278
- end
279
- end
280
-
281
- def predict_probability(x)
282
- # if not @probability
283
- # raise TypeError, "model does not support probabiliy estimates"
284
- # end
285
-
286
- #convert x into feature_node, alloc a double array to receive probabilities
287
- data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
288
- dblarr = new_double(@nr_class)
289
- pred = Liblinear::predict_probability(@model, data, dblarr)
290
- pv = _double_array_to_list(dblarr, @nr_class)
291
- delete_double(dblarr)
292
- feature_node_array_destroy(data)
293
- p = {}
294
- for i in (0..@labels.size-1)
295
- p[@labels[i]] = pv[i]
296
- end
297
- return pred, p
298
- end
299
-
300
- # def get_svr_probability
301
- # #leave the Error checking to svm.cpp code
302
- # ret = Liblinear::get_svr_probability(@model)
303
- # if ret == 0
304
- # raise TypeError, "not a regression model or probability information not available"
305
- # end
306
- # return ret
307
- # end
308
-
309
- # def get_svr_pdf
310
- # #get_svr_probability will handle error checking
311
- # sigma = get_svr_probability()
312
- # return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)} # TODO: verify this works
313
- # end
314
-
315
- def save(filename)
316
- save_model(filename,@model)
317
- end
318
-
319
- def destroy
320
- destroy_model(@model)
321
- end
322
- end
323
-
324
- def cross_validation(prob, param, fold)
325
- target = new_int(prob.size)
326
- Liblinear::cross_validation(prob.prob, param.param, fold, target)
327
- ret = _int_array_to_list(target, prob.size)
328
- delete_int(target)
329
- return ret
330
- end
331
-
332
- def read_file filename
333
- labels = []
334
- samples = []
335
- max_index = 0
336
-
337
- f = File.open(filename)
338
- f.each do |line|
339
- elems = line.split
340
- sample = {}
341
- for e in elems[1..-1]
342
- points = e.split(":")
343
- sample[points[0].to_i] = points[1].to_f
344
- if points[0].to_i < max_index
345
- max_index = points[0].to_i
346
- end
347
- end
348
- labels << elems[0].to_i
349
- samples << sample
350
- #print elems[0].to_i
351
- #print " - "
352
- #puts sample.inspect
353
- end
354
- puts "#{filename}: #{samples.size} samples loaded."
355
- return labels,samples
356
- end
357
-
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file