tomz-liblinear-ruby-swig 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +2 -0
- data/README.rdoc +7 -6
- data/Rakefile +3 -1
- data/ext/liblinear_wrap.cxx +53 -0
- data/lib/linear.rb +15 -7
- metadata +2 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -9,7 +9,7 @@ This is the Ruby LIBLINEAR SWIG (Simplified Wrapper and Interface Generator)
|
|
9
9
|
interface. LIBLINEAR is a high performance machine learning library for large
|
10
10
|
scale text mining(http://www.csie.ntu.edu.tw/~cjlin/liblinear).
|
11
11
|
|
12
|
-
A slightly modified version of LIBLINEAR 1.33 is included which
|
12
|
+
A slightly modified version of LIBLINEAR 1.33 is included which allows turning
|
13
13
|
on/off the default debuging/logging messages. You don't need your own copy of
|
14
14
|
SWIG to use this library - all needed files are generated using SWIG already.
|
15
15
|
|
@@ -25,8 +25,8 @@ Currently the gem is available on linux only(tested on Ubuntu 8 and Fedora 9/10,
|
|
25
25
|
and should work on OS X), and you will need g++ installed to compile the native
|
26
26
|
code.
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
sudo gem sources -a http://gems.github.com (you only have to do this once)
|
29
|
+
sudo gem install tomz-liblinear-ruby-swig
|
30
30
|
|
31
31
|
== SYNOPSIS:
|
32
32
|
|
@@ -60,6 +60,7 @@ Try the following multiclass problem in irb:
|
|
60
60
|
irb(main):021:0> pred = m.predict({1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1})
|
61
61
|
=> 3
|
62
62
|
irb(main):022:0> m.save("test.model")
|
63
|
+
irb(main):023:0> "your imagination " * 10
|
63
64
|
|
64
65
|
|
65
66
|
For more examples see test*.rb in the liblinear-ruby-swig/liblinear-1.33/ruby
|
@@ -68,7 +69,7 @@ directory
|
|
68
69
|
== AUTHOR:
|
69
70
|
|
70
71
|
Tom Zeng
|
71
|
-
http://www.tomzconsulting.com
|
72
|
-
http://www.linkedin.com/in/tomzeng
|
73
|
-
tom.z.zeng _at_ gmail _dot_ com
|
72
|
+
- http://www.tomzconsulting.com
|
73
|
+
- http://www.linkedin.com/in/tomzeng
|
74
|
+
- tom.z.zeng _at_ gmail _dot_ com
|
74
75
|
|
data/Rakefile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
gem 'hoe', '>=1.8.3','<= 1.12.2'
|
2
3
|
require 'hoe'
|
3
4
|
|
5
|
+
|
4
6
|
task :default => ["sync_files","make_gem"]
|
5
7
|
|
6
8
|
EXT = "ext/liblinear?.#{Hoe::DLEXT}"
|
7
9
|
|
8
|
-
Hoe.new('liblinear-ruby-swig', '0.1.
|
10
|
+
Hoe.new('liblinear-ruby-swig', '0.1.1') do |p|
|
9
11
|
p.author = 'Tom Zeng'
|
10
12
|
p.email = 'tom.z.zeng@gmail.com'
|
11
13
|
p.url = 'http://www.tomzconsulting.com'
|
data/ext/liblinear_wrap.cxx
CHANGED
@@ -2120,6 +2120,13 @@ void feature_node_matrix_set(struct feature_node **matrix, int i, struct feature
|
|
2120
2120
|
matrix[i] = array;
|
2121
2121
|
}
|
2122
2122
|
|
2123
|
+
void set_bias_index(struct feature_node **matrix, int size, int maxlen, int *len_array)
|
2124
|
+
{
|
2125
|
+
int i;
|
2126
|
+
for(i=0; i<size; i++)
|
2127
|
+
(matrix[i]+len_array[i])->index = maxlen+1;
|
2128
|
+
}
|
2129
|
+
|
2123
2130
|
void feature_node_matrix_destroy(struct feature_node **matrix)
|
2124
2131
|
{
|
2125
2132
|
free(matrix);
|
@@ -4095,6 +4102,51 @@ fail:
|
|
4095
4102
|
}
|
4096
4103
|
|
4097
4104
|
|
4105
|
+
SWIGINTERN VALUE
|
4106
|
+
_wrap_set_bias_index(int argc, VALUE *argv, VALUE self) {
|
4107
|
+
feature_node **arg1 = (feature_node **) 0 ;
|
4108
|
+
int arg2 ;
|
4109
|
+
int arg3 ;
|
4110
|
+
int *arg4 = (int *) 0 ;
|
4111
|
+
void *argp1 = 0 ;
|
4112
|
+
int res1 = 0 ;
|
4113
|
+
int val2 ;
|
4114
|
+
int ecode2 = 0 ;
|
4115
|
+
int val3 ;
|
4116
|
+
int ecode3 = 0 ;
|
4117
|
+
void *argp4 = 0 ;
|
4118
|
+
int res4 = 0 ;
|
4119
|
+
|
4120
|
+
if ((argc < 4) || (argc > 4)) {
|
4121
|
+
rb_raise(rb_eArgError, "wrong # of arguments(%d for 4)",argc); SWIG_fail;
|
4122
|
+
}
|
4123
|
+
res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_p_feature_node, 0 | 0 );
|
4124
|
+
if (!SWIG_IsOK(res1)) {
|
4125
|
+
SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "feature_node **","set_bias_index", 1, argv[0] ));
|
4126
|
+
}
|
4127
|
+
arg1 = reinterpret_cast< feature_node ** >(argp1);
|
4128
|
+
ecode2 = SWIG_AsVal_int(argv[1], &val2);
|
4129
|
+
if (!SWIG_IsOK(ecode2)) {
|
4130
|
+
SWIG_exception_fail(SWIG_ArgError(ecode2), Ruby_Format_TypeError( "", "int","set_bias_index", 2, argv[1] ));
|
4131
|
+
}
|
4132
|
+
arg2 = static_cast< int >(val2);
|
4133
|
+
ecode3 = SWIG_AsVal_int(argv[2], &val3);
|
4134
|
+
if (!SWIG_IsOK(ecode3)) {
|
4135
|
+
SWIG_exception_fail(SWIG_ArgError(ecode3), Ruby_Format_TypeError( "", "int","set_bias_index", 3, argv[2] ));
|
4136
|
+
}
|
4137
|
+
arg3 = static_cast< int >(val3);
|
4138
|
+
res4 = SWIG_ConvertPtr(argv[3], &argp4,SWIGTYPE_p_int, 0 | 0 );
|
4139
|
+
if (!SWIG_IsOK(res4)) {
|
4140
|
+
SWIG_exception_fail(SWIG_ArgError(res4), Ruby_Format_TypeError( "", "int *","set_bias_index", 4, argv[3] ));
|
4141
|
+
}
|
4142
|
+
arg4 = reinterpret_cast< int * >(argp4);
|
4143
|
+
set_bias_index(arg1,arg2,arg3,arg4);
|
4144
|
+
return Qnil;
|
4145
|
+
fail:
|
4146
|
+
return Qnil;
|
4147
|
+
}
|
4148
|
+
|
4149
|
+
|
4098
4150
|
SWIGINTERN VALUE
|
4099
4151
|
_wrap_feature_node_matrix_destroy(int argc, VALUE *argv, VALUE self) {
|
4100
4152
|
feature_node **arg1 = (feature_node **) 0 ;
|
@@ -4520,6 +4572,7 @@ SWIGEXPORT void Init_liblinear(void) {
|
|
4520
4572
|
rb_define_module_function(mLiblinear, "feature_node_array_destroy", VALUEFUNC(_wrap_feature_node_array_destroy), -1);
|
4521
4573
|
rb_define_module_function(mLiblinear, "feature_node_matrix", VALUEFUNC(_wrap_feature_node_matrix), -1);
|
4522
4574
|
rb_define_module_function(mLiblinear, "feature_node_matrix_set", VALUEFUNC(_wrap_feature_node_matrix_set), -1);
|
4575
|
+
rb_define_module_function(mLiblinear, "set_bias_index", VALUEFUNC(_wrap_set_bias_index), -1);
|
4523
4576
|
rb_define_module_function(mLiblinear, "feature_node_matrix_destroy", VALUEFUNC(_wrap_feature_node_matrix_destroy), -1);
|
4524
4577
|
}
|
4525
4578
|
|
data/lib/linear.rb
CHANGED
@@ -99,7 +99,7 @@ class LParameter
|
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
-
def _convert_to_feature_node_array(x, bias=-1)
|
102
|
+
def _convert_to_feature_node_array(x, maxlen, bias=-1)
|
103
103
|
# convert a sequence or mapping to an feature_node array
|
104
104
|
|
105
105
|
# Find non zero elements
|
@@ -121,7 +121,7 @@ def _convert_to_feature_node_array(x, bias=-1)
|
|
121
121
|
if bias >=0
|
122
122
|
data = feature_node_array(iter_range.size+2)
|
123
123
|
#puts "bias element (#{iter_range.size},#{bias})"
|
124
|
-
feature_node_array_set(data,iter_range.size,
|
124
|
+
feature_node_array_set(data,iter_range.size,maxlen+1,bias)
|
125
125
|
feature_node_array_set(data,iter_range.size+1,-1,0)
|
126
126
|
else
|
127
127
|
data = feature_node_array(iter_range.size+1)
|
@@ -154,17 +154,25 @@ class LProblem
|
|
154
154
|
@x_matrix = x_matrix = feature_node_matrix(size)
|
155
155
|
@data = []
|
156
156
|
@maxlen = 0 #max number of features
|
157
|
+
len_array=[]
|
158
|
+
|
157
159
|
for i in (0..size-1)
|
158
|
-
data = _convert_to_feature_node_array(x[i], bias)
|
160
|
+
data = _convert_to_feature_node_array(x[i], @maxlen, bias)
|
159
161
|
@data << data
|
160
162
|
feature_node_matrix_set(x_matrix,i,data)
|
163
|
+
|
161
164
|
if x[i].class == Hash
|
162
165
|
if x[i].size > 0
|
163
|
-
@maxlen = [@maxlen,x[i].keys.max].max
|
166
|
+
@maxlen = [@maxlen,x[i].keys.max].max
|
164
167
|
end
|
165
168
|
else
|
166
169
|
@maxlen = [@maxlen,x[i].size].max
|
167
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))
|
168
176
|
end
|
169
177
|
|
170
178
|
prob.y = y_array
|
@@ -217,7 +225,7 @@ class LModel
|
|
217
225
|
end
|
218
226
|
|
219
227
|
def predict(x)
|
220
|
-
data = _convert_to_feature_node_array(x, @model.bias)
|
228
|
+
data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
|
221
229
|
ret = Liblinear::predict(@model,data)
|
222
230
|
feature_node_array_destroy(data)
|
223
231
|
return ret
|
@@ -235,7 +243,7 @@ class LModel
|
|
235
243
|
def predict_values_raw(x)
|
236
244
|
#convert x into feature_node, allocate a double array for return
|
237
245
|
n = (@nr_class*(@nr_class-1)/2).floor
|
238
|
-
data = _convert_to_feature_node_array(x, @model.bias)
|
246
|
+
data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
|
239
247
|
dblarr = new_double(n)
|
240
248
|
Liblinear::predict_values(@model, data, dblarr)
|
241
249
|
ret = _double_array_to_list(dblarr, n)
|
@@ -276,7 +284,7 @@ class LModel
|
|
276
284
|
# end
|
277
285
|
|
278
286
|
#convert x into feature_node, alloc a double array to receive probabilities
|
279
|
-
data = _convert_to_feature_node_array(x
|
287
|
+
data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
|
280
288
|
dblarr = new_double(@nr_class)
|
281
289
|
pred = Liblinear::predict_probability(@model, data, dblarr)
|
282
290
|
pv = _double_array_to_list(dblarr, @nr_class)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tomz-liblinear-ruby-swig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Zeng
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-10 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|