Skip to content
Browse files

incorporated the _convert_to_feature_node_array fix from Chen-Tse Tsa…

…i/the LIBSVM development group
  • Loading branch information...
1 parent 1ed4425 commit 0a8f7bc0a42987f336b08ffb32d5627524b75c74 @tomz committed
View
2 History.txt
@@ -1,3 +1,5 @@
2009-05-10 Tom Zeng (tom.z.zeng@gmail.com)
* initial check-in into github
+2009-07-10 Tom Zeng (tom.z.zeng@gmail.com)
+ * incorporated the _convert_to_feature_node_array fix from the LIBSVM development group
View
4 Rakefile
@@ -1,11 +1,13 @@
require 'rubygems'
+gem 'hoe', '>=1.8.3','<= 1.12.2'
require 'hoe'
+
task :default => ["sync_files","make_gem"]
EXT = "ext/liblinear?.#{Hoe::DLEXT}"
-Hoe.new('liblinear-ruby-swig', '0.1.0') do |p|
+Hoe.new('liblinear-ruby-swig', '0.1.1') do |p|
p.author = 'Tom Zeng'
p.email = 'tom.z.zeng@gmail.com'
p.url = 'http://www.tomzconsulting.com'
View
BIN ext/daxpy.o
Binary file not shown.
View
BIN ext/ddot.o
Binary file not shown.
View
BIN ext/dnrm2.o
Binary file not shown.
View
BIN ext/dscal.o
Binary file not shown.
View
BIN ext/liblinear.so
Binary file not shown.
View
53 ext/liblinear_wrap.cxx
@@ -2120,6 +2120,13 @@ void feature_node_matrix_set(struct feature_node **matrix, int i, struct feature
matrix[i] = array;
}
+void set_bias_index(struct feature_node **matrix, int size, int maxlen, int *len_array)
+{
+ int i;
+ for(i=0; i<size; i++)
+ (matrix[i]+len_array[i])->index = maxlen+1;
+}
+
void feature_node_matrix_destroy(struct feature_node **matrix)
{
free(matrix);
@@ -4096,6 +4103,51 @@ _wrap_feature_node_matrix_set(int argc, VALUE *argv, VALUE self) {
SWIGINTERN VALUE
+_wrap_set_bias_index(int argc, VALUE *argv, VALUE self) {
+ feature_node **arg1 = (feature_node **) 0 ;
+ int arg2 ;
+ int arg3 ;
+ int *arg4 = (int *) 0 ;
+ void *argp1 = 0 ;
+ int res1 = 0 ;
+ int val2 ;
+ int ecode2 = 0 ;
+ int val3 ;
+ int ecode3 = 0 ;
+ void *argp4 = 0 ;
+ int res4 = 0 ;
+
+ if ((argc < 4) || (argc > 4)) {
+ rb_raise(rb_eArgError, "wrong # of arguments(%d for 4)",argc); SWIG_fail;
+ }
+ res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_p_feature_node, 0 | 0 );
+ if (!SWIG_IsOK(res1)) {
+ SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "feature_node **","set_bias_index", 1, argv[0] ));
+ }
+ arg1 = reinterpret_cast< feature_node ** >(argp1);
+ ecode2 = SWIG_AsVal_int(argv[1], &val2);
+ if (!SWIG_IsOK(ecode2)) {
+ SWIG_exception_fail(SWIG_ArgError(ecode2), Ruby_Format_TypeError( "", "int","set_bias_index", 2, argv[1] ));
+ }
+ arg2 = static_cast< int >(val2);
+ ecode3 = SWIG_AsVal_int(argv[2], &val3);
+ if (!SWIG_IsOK(ecode3)) {
+ SWIG_exception_fail(SWIG_ArgError(ecode3), Ruby_Format_TypeError( "", "int","set_bias_index", 3, argv[2] ));
+ }
+ arg3 = static_cast< int >(val3);
+ res4 = SWIG_ConvertPtr(argv[3], &argp4,SWIGTYPE_p_int, 0 | 0 );
+ if (!SWIG_IsOK(res4)) {
+ SWIG_exception_fail(SWIG_ArgError(res4), Ruby_Format_TypeError( "", "int *","set_bias_index", 4, argv[3] ));
+ }
+ arg4 = reinterpret_cast< int * >(argp4);
+ set_bias_index(arg1,arg2,arg3,arg4);
+ return Qnil;
+fail:
+ return Qnil;
+}
+
+
+SWIGINTERN VALUE
_wrap_feature_node_matrix_destroy(int argc, VALUE *argv, VALUE self) {
feature_node **arg1 = (feature_node **) 0 ;
void *argp1 = 0 ;
@@ -4520,6 +4572,7 @@ SWIGEXPORT void Init_liblinear(void) {
rb_define_module_function(mLiblinear, "feature_node_array_destroy", VALUEFUNC(_wrap_feature_node_array_destroy), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix", VALUEFUNC(_wrap_feature_node_matrix), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix_set", VALUEFUNC(_wrap_feature_node_matrix_set), -1);
+ rb_define_module_function(mLiblinear, "set_bias_index", VALUEFUNC(_wrap_set_bias_index), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix_destroy", VALUEFUNC(_wrap_feature_node_matrix_destroy), -1);
}
View
BIN ext/liblinear_wrap.o
Binary file not shown.
View
BIN ext/linear.o
Binary file not shown.
View
BIN ext/tron.o
Binary file not shown.
View
22 lib/linear.rb
@@ -99,7 +99,7 @@ def destroy
end
end
-def _convert_to_feature_node_array(x, bias=-1)
+def _convert_to_feature_node_array(x, maxlen, bias=-1)
# convert a sequence or mapping to an feature_node array
# Find non zero elements
@@ -121,7 +121,7 @@ def _convert_to_feature_node_array(x, bias=-1)
if bias >=0
data = feature_node_array(iter_range.size+2)
#puts "bias element (#{iter_range.size},#{bias})"
- feature_node_array_set(data,iter_range.size,iter_range.size,bias)
+ feature_node_array_set(data,iter_range.size,maxlen+1,bias)
feature_node_array_set(data,iter_range.size+1,-1,0)
else
data = feature_node_array(iter_range.size+1)
@@ -154,17 +154,25 @@ def initialize(y,x,bias)
@x_matrix = x_matrix = feature_node_matrix(size)
@data = []
@maxlen = 0 #max number of features
+ len_array=[]
+
for i in (0..size-1)
- data = _convert_to_feature_node_array(x[i], bias)
+ data = _convert_to_feature_node_array(x[i], @maxlen, bias)
@data << data
feature_node_matrix_set(x_matrix,i,data)
+
if x[i].class == Hash
if x[i].size > 0
- @maxlen = [@maxlen,x[i].keys.max].max
+ @maxlen = [@maxlen,x[i].keys.max].max
end
else
@maxlen = [@maxlen,x[i].size].max
end
+ len_array << x[i].size
+ end
+
+ if bias >= 0
+ set_bias_index(x_matrix, size, @maxlen, _int_array(len_array))
end
prob.y = y_array
@@ -217,7 +225,7 @@ def initialize(arg1,arg2=nil)
end
def predict(x)
- data = _convert_to_feature_node_array(x, @model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
ret = Liblinear::predict(@model,data)
feature_node_array_destroy(data)
return ret
@@ -235,7 +243,7 @@ def get_labels
def predict_values_raw(x)
#convert x into feature_node, allocate a double array for return
n = (@nr_class*(@nr_class-1)/2).floor
- data = _convert_to_feature_node_array(x, @model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
dblarr = new_double(n)
Liblinear::predict_values(@model, data, dblarr)
ret = _double_array_to_list(dblarr, n)
@@ -276,7 +284,7 @@ def predict_probability(x)
# end
#convert x into feature_node, alloc a double array to receive probabilities
- data = _convert_to_feature_node_array(x,@model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
dblarr = new_double(@nr_class)
pred = Liblinear::predict_probability(@model, data, dblarr)
pv = _double_array_to_list(dblarr, @nr_class)
View
7 liblinear-1.33/ruby/liblinear.i
@@ -93,6 +93,13 @@ void feature_node_matrix_set(struct feature_node **matrix, int i, struct feature
matrix[i] = array;
}
+void set_bias_index(struct feature_node **matrix, int size, int maxlen, int *len_array)
+{
+ int i;
+ for(i=0; i<size; i++)
+ (matrix[i]+len_array[i])->index = maxlen+1;
+}
+
void feature_node_matrix_destroy(struct feature_node **matrix)
{
free(matrix);
View
BIN liblinear-1.33/ruby/liblinear.so
Binary file not shown.
View
53 liblinear-1.33/ruby/liblinear_wrap.cxx
@@ -2120,6 +2120,13 @@ void feature_node_matrix_set(struct feature_node **matrix, int i, struct feature
matrix[i] = array;
}
+void set_bias_index(struct feature_node **matrix, int size, int maxlen, int *len_array)
+{
+ int i;
+ for(i=0; i<size; i++)
+ (matrix[i]+len_array[i])->index = maxlen+1;
+}
+
void feature_node_matrix_destroy(struct feature_node **matrix)
{
free(matrix);
@@ -4096,6 +4103,51 @@ _wrap_feature_node_matrix_set(int argc, VALUE *argv, VALUE self) {
SWIGINTERN VALUE
+_wrap_set_bias_index(int argc, VALUE *argv, VALUE self) {
+ feature_node **arg1 = (feature_node **) 0 ;
+ int arg2 ;
+ int arg3 ;
+ int *arg4 = (int *) 0 ;
+ void *argp1 = 0 ;
+ int res1 = 0 ;
+ int val2 ;
+ int ecode2 = 0 ;
+ int val3 ;
+ int ecode3 = 0 ;
+ void *argp4 = 0 ;
+ int res4 = 0 ;
+
+ if ((argc < 4) || (argc > 4)) {
+ rb_raise(rb_eArgError, "wrong # of arguments(%d for 4)",argc); SWIG_fail;
+ }
+ res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_p_feature_node, 0 | 0 );
+ if (!SWIG_IsOK(res1)) {
+ SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "feature_node **","set_bias_index", 1, argv[0] ));
+ }
+ arg1 = reinterpret_cast< feature_node ** >(argp1);
+ ecode2 = SWIG_AsVal_int(argv[1], &val2);
+ if (!SWIG_IsOK(ecode2)) {
+ SWIG_exception_fail(SWIG_ArgError(ecode2), Ruby_Format_TypeError( "", "int","set_bias_index", 2, argv[1] ));
+ }
+ arg2 = static_cast< int >(val2);
+ ecode3 = SWIG_AsVal_int(argv[2], &val3);
+ if (!SWIG_IsOK(ecode3)) {
+ SWIG_exception_fail(SWIG_ArgError(ecode3), Ruby_Format_TypeError( "", "int","set_bias_index", 3, argv[2] ));
+ }
+ arg3 = static_cast< int >(val3);
+ res4 = SWIG_ConvertPtr(argv[3], &argp4,SWIGTYPE_p_int, 0 | 0 );
+ if (!SWIG_IsOK(res4)) {
+ SWIG_exception_fail(SWIG_ArgError(res4), Ruby_Format_TypeError( "", "int *","set_bias_index", 4, argv[3] ));
+ }
+ arg4 = reinterpret_cast< int * >(argp4);
+ set_bias_index(arg1,arg2,arg3,arg4);
+ return Qnil;
+fail:
+ return Qnil;
+}
+
+
+SWIGINTERN VALUE
_wrap_feature_node_matrix_destroy(int argc, VALUE *argv, VALUE self) {
feature_node **arg1 = (feature_node **) 0 ;
void *argp1 = 0 ;
@@ -4520,6 +4572,7 @@ SWIGEXPORT void Init_liblinear(void) {
rb_define_module_function(mLiblinear, "feature_node_array_destroy", VALUEFUNC(_wrap_feature_node_array_destroy), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix", VALUEFUNC(_wrap_feature_node_matrix), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix_set", VALUEFUNC(_wrap_feature_node_matrix_set), -1);
+ rb_define_module_function(mLiblinear, "set_bias_index", VALUEFUNC(_wrap_set_bias_index), -1);
rb_define_module_function(mLiblinear, "feature_node_matrix_destroy", VALUEFUNC(_wrap_feature_node_matrix_destroy), -1);
}
View
BIN liblinear-1.33/ruby/liblinear_wrap.o
Binary file not shown.
View
BIN liblinear-1.33/ruby/linear.o
Binary file not shown.
View
22 liblinear-1.33/ruby/linear.rb
@@ -99,7 +99,7 @@ def destroy
end
end
-def _convert_to_feature_node_array(x, bias=-1)
+def _convert_to_feature_node_array(x, maxlen, bias=-1)
# convert a sequence or mapping to an feature_node array
# Find non zero elements
@@ -121,7 +121,7 @@ def _convert_to_feature_node_array(x, bias=-1)
if bias >=0
data = feature_node_array(iter_range.size+2)
#puts "bias element (#{iter_range.size},#{bias})"
- feature_node_array_set(data,iter_range.size,iter_range.size,bias)
+ feature_node_array_set(data,iter_range.size,maxlen+1,bias)
feature_node_array_set(data,iter_range.size+1,-1,0)
else
data = feature_node_array(iter_range.size+1)
@@ -154,17 +154,25 @@ def initialize(y,x,bias)
@x_matrix = x_matrix = feature_node_matrix(size)
@data = []
@maxlen = 0 #max number of features
+ len_array=[]
+
for i in (0..size-1)
- data = _convert_to_feature_node_array(x[i], bias)
+ data = _convert_to_feature_node_array(x[i], @maxlen, bias)
@data << data
feature_node_matrix_set(x_matrix,i,data)
+
if x[i].class == Hash
if x[i].size > 0
- @maxlen = [@maxlen,x[i].keys.max].max
+ @maxlen = [@maxlen,x[i].keys.max].max
end
else
@maxlen = [@maxlen,x[i].size].max
end
+ len_array << x[i].size
+ end
+
+ if bias >= 0
+ set_bias_index(x_matrix, size, @maxlen, _int_array(len_array))
end
prob.y = y_array
@@ -217,7 +225,7 @@ def initialize(arg1,arg2=nil)
end
def predict(x)
- data = _convert_to_feature_node_array(x, @model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
ret = Liblinear::predict(@model,data)
feature_node_array_destroy(data)
return ret
@@ -235,7 +243,7 @@ def get_labels
def predict_values_raw(x)
#convert x into feature_node, allocate a double array for return
n = (@nr_class*(@nr_class-1)/2).floor
- data = _convert_to_feature_node_array(x, @model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
dblarr = new_double(n)
Liblinear::predict_values(@model, data, dblarr)
ret = _double_array_to_list(dblarr, n)
@@ -276,7 +284,7 @@ def predict_probability(x)
# end
#convert x into feature_node, alloc a double array to receive probabilities
- data = _convert_to_feature_node_array(x,@model.bias)
+ data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias)
dblarr = new_double(@nr_class)
pred = Liblinear::predict_probability(@model, data, dblarr)
pv = _double_array_to_list(dblarr, @nr_class)
View
4 liblinear-ruby-swig.gemspec
@@ -1,10 +1,10 @@
Gem::Specification.new do |s|
s.name = %q{liblinear-ruby-swig}
- s.version = "0.1.0"
+ s.version = "0.1.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Tom Zeng"]
- s.date = %q{2009-05-09}
+ s.date = %q{2009-07-10}
s.description = %q{Ruby wrapper of LIBLINEAR using SWIG}
s.email = %q{tom.z.zeng@gmail.com}
s.extensions = ["ext/extconf.rb"]

0 comments on commit 0a8f7bc

Please sign in to comment.
Something went wrong with that request. Please try again.