rb-libsvm 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28c8bcde167de58a135e78ebfb547d930adf2ddf
4
+ data.tar.gz: 5894971f3815bdc838ff68cd940e88008930bca0
5
+ SHA512:
6
+ metadata.gz: 7d14745db75f09d07c1c61b35221805abf3f51bbf2aef31e295423474913011961fd5849e69cbf01e0127a2bb5cc628387229675ec92bfc07665bd2355a56705
7
+ data.tar.gz: 73e039abd412ff291514c559499ee5508684390f6ee335889c2a93bd4dd7b3a1a07ef9729f6d9348f039945d25f92de94dc20e2afb82210fddbdccfc6cbac77d
data/README.md CHANGED
@@ -21,7 +21,7 @@ this gem. You should install the original package if you need them.
21
21
  It is helpful to consult the [README][] of the LIBSVM package for
22
22
  reference when configuring the training parameters.
23
23
 
24
- Currently this package includes libsvm version 3.16.
24
+ Currently this package includes libsvm version 3.17.
25
25
 
26
26
  ## Dependencies
27
27
 
@@ -77,6 +77,9 @@ gem 'rb-libsvm', require: 'libsvm'
77
77
  This is because the loadable name (`libsvm`) is different from the
78
78
  gem's name (`rb-libsvm`).
79
79
 
80
+ There is a JRuby variant of this gem named
81
+ [jrb-libsvm](https://github.com/sch1zo/jrb-libsvm) by Andreas Eger.
82
+
80
83
  ## Author
81
84
 
82
85
  Written by [C. Florian Ebeling](https://github.com/febeling).
data/ext/libsvm/libsvm.c CHANGED
@@ -27,7 +27,7 @@ static struct svm_node *node_new() {
27
27
  return NULL;
28
28
  return n;
29
29
  }
30
-
30
+
31
31
  static void node_free(struct svm_node *n) {
32
32
  free(n);
33
33
  }
@@ -37,13 +37,13 @@ static VALUE node_alloc(VALUE cls) {
37
37
  n = node_new();
38
38
  if(n == NULL)
39
39
  rb_raise(rb_eNoMemError, "Not enough memory for allocating Node.");
40
-
40
+
41
41
  return Data_Wrap_Struct(cls, 0, node_free, n);
42
42
  }
43
43
 
44
44
  rx_def_accessor(cNode,struct svm_node,int,index);
45
45
  rx_def_accessor(cNode,struct svm_node,double,value);
46
-
46
+
47
47
  /* Libsvm::Problem */
48
48
  static struct svm_problem *problem_new() {
49
49
  struct svm_problem *n;
@@ -54,7 +54,7 @@ static struct svm_problem *problem_new() {
54
54
  }
55
55
 
56
56
  static void problem_free(struct svm_problem *n) {
57
- /*
57
+ /*
58
58
  Deliberate no-op, because of this note from the README:
59
59
 
60
60
  `*NOTE* Because svm_model contains pointers to svm_problem, you can
@@ -109,7 +109,7 @@ static struct svm_node **examples_ary_to_internal(VALUE examples_ary)
109
109
  if(x == 0) {
110
110
  rb_raise(rb_eNoMemError, "%s:%i", __FILE__,__LINE__);
111
111
  }
112
-
112
+
113
113
  for(i = 0; i < num; ++i) {
114
114
  nodes_ary = rb_ary_entry(examples_ary,i);
115
115
  *(x+i) = example_to_internal(nodes_ary);
@@ -118,7 +118,7 @@ static struct svm_node **examples_ary_to_internal(VALUE examples_ary)
118
118
  return x;
119
119
  }
120
120
 
121
- /*
121
+ /*
122
122
  call-seq:
123
123
  problem.set_examples(labels, examples_array)
124
124
 
@@ -129,7 +129,7 @@ static struct svm_node **examples_ary_to_internal(VALUE examples_ary)
129
129
  of lables (or classifications) and examples (or feature vectors).
130
130
  If those 2 don't match in length and ArgumentError is raised.
131
131
  */
132
- static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
132
+ static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary)
133
133
  {
134
134
  struct svm_problem *prob;
135
135
  int i;
@@ -140,8 +140,8 @@ static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary
140
140
  rb_raise(rb_eArgError, "Number of labels (%i) does not match number of features (%i).", num, rx_ary_size(examples_ary));
141
141
  }
142
142
 
143
- Data_Get_Struct(obj, struct svm_problem, prob);
144
-
143
+ Data_Get_Struct(obj, struct svm_problem, prob);
144
+
145
145
  if(prob->l > 0) {
146
146
  free(prob->y);
147
147
  for(i = 0; i < num; ++i) {
@@ -165,12 +165,12 @@ static VALUE cProblem_examples_set(VALUE obj,VALUE labels_ary,VALUE examples_ary
165
165
  return INT2FIX(num);
166
166
  }
167
167
 
168
- /*
168
+ /*
169
169
  call-seq:
170
170
  labels, array_of_arrays = problem.examples
171
171
 
172
172
  double *y; // class/label of the example
173
- struct svm_node **x;
173
+ struct svm_node **x;
174
174
  */
175
175
  static VALUE cProblem_examples(VALUE problem) {
176
176
  struct svm_problem *prob;
@@ -180,11 +180,11 @@ static VALUE cProblem_examples(VALUE problem) {
180
180
  VALUE labels_ary, examples_ary, example_ary, v_node, result;
181
181
  int i;
182
182
 
183
- Data_Get_Struct(problem, struct svm_problem, prob);
183
+ Data_Get_Struct(problem, struct svm_problem, prob);
184
184
 
185
185
  labels_ary = rb_ary_new2(prob->l);
186
186
  examples_ary = rb_ary_new2(prob->l);
187
-
187
+
188
188
  features = calloc(prob->l, sizeof(struct svm_node));
189
189
  if(features == 0) {
190
190
  rb_raise(rb_eNoMemError, "on allocating Libsvm::Node" " %s:%i", __FILE__,__LINE__);
@@ -225,7 +225,7 @@ static struct svm_parameter *parameter_new() {
225
225
  return NULL;
226
226
  return n;
227
227
  }
228
-
228
+
229
229
  static void parameter_free(struct svm_parameter *n) {
230
230
  // svm_destroy_param(n);
231
231
  free(n);
@@ -236,7 +236,7 @@ static VALUE parameter_alloc(VALUE cls) {
236
236
  n = parameter_new();
237
237
  if(n == NULL)
238
238
  rb_raise(rb_eNoMemError, "Not enough memory for allocating SvmParameter.");
239
-
239
+
240
240
  return Data_Wrap_Struct(cls, 0, parameter_free, n);
241
241
  }
242
242
 
@@ -259,7 +259,7 @@ rx_def_accessor_as(cSvmParameter,struct svm_parameter,double,C,c);
259
259
  nr_weight is the number of elements in the array weight_label and
260
260
  weight. Each weight[i] corresponds to weight_label[i], meaning that
261
261
  the penalty of class weight_label[i] is scaled by a factor of weight[i].
262
-
262
+
263
263
  If you do not want to change penalty for any of the classes,
264
264
  just set nr_weight to 0.
265
265
 
@@ -285,7 +285,7 @@ static VALUE cSvmParameter_label_weights_set(VALUE obj,VALUE weight_hash) {
285
285
  for(i = 0; i < param->nr_weight; ++i) {
286
286
  key = rb_ary_entry(keys,i);
287
287
  val = rb_hash_aref(weight_hash,key);
288
-
288
+
289
289
  param->weight_label[i] = NUM2INT(key);
290
290
  param->weight[i] = NUM2DBL(val);
291
291
  }
@@ -294,12 +294,12 @@ static VALUE cSvmParameter_label_weights_set(VALUE obj,VALUE weight_hash) {
294
294
  }
295
295
 
296
296
  static VALUE cSvmParameter_label_weights(VALUE obj) {
297
- struct svm_parameter *param;
297
+ struct svm_parameter *param;
298
298
  int i;
299
299
  VALUE hash,key,val;
300
300
 
301
301
  Data_Get_Struct(obj,struct svm_parameter,param);
302
-
302
+
303
303
  hash = rb_hash_new();
304
304
 
305
305
  for(i = 0; i < param->nr_weight; ++i) {
@@ -326,7 +326,7 @@ static VALUE cModel_class_train(VALUE obj,VALUE problem,VALUE parameter) {
326
326
 
327
327
  Data_Get_Struct(problem, struct svm_problem, prob);
328
328
  Data_Get_Struct(parameter, struct svm_parameter, param);
329
-
329
+
330
330
  check_error = svm_check_parameter(prob, param);
331
331
  if(check_error != NULL) {
332
332
  rb_raise(rb_eArgError, "Parameters not valid for Problem: '%s'", check_error);
@@ -385,15 +385,15 @@ static VALUE cModel_save(VALUE obj, VALUE filename)
385
385
  {
386
386
  const struct svm_model *model;
387
387
  const char *path;
388
- int rc;
388
+ int rc;
389
389
 
390
390
  Data_Get_Struct(obj, struct svm_model, model);
391
391
  path = StringValueCStr(filename);
392
-
393
- if(rc = svm_save_model(path, model)) {
392
+
393
+ if((rc = svm_save_model(path, model))) {
394
394
  rb_raise(rb_eStandardError, "Error on saving model, code: %i", rc);
395
395
  }
396
-
396
+
397
397
  return Qnil;
398
398
  }
399
399
 
@@ -404,7 +404,7 @@ static VALUE cModel_svm_type(VALUE obj)
404
404
  return INT2NUM(svm_get_svm_type(model));
405
405
  }
406
406
 
407
- static VALUE cModel_classes(VALUE obj)
407
+ static VALUE cModel_classes(VALUE obj)
408
408
  {
409
409
  const struct svm_model *model;
410
410
  Data_Get_Struct(obj, struct svm_model, model);
@@ -430,7 +430,7 @@ static VALUE cModel_class_cross_validation(VALUE cls, VALUE problem, VALUE param
430
430
 
431
431
  Data_Get_Struct(problem, struct svm_problem, prob);
432
432
  Data_Get_Struct(parameter, struct svm_parameter, param);
433
-
433
+
434
434
  nr_fold = NUM2INT(num_fold);
435
435
 
436
436
  target = rb_ary_new2(prob->l);
@@ -38,5 +38,3 @@
38
38
  NUM2INT(rb_funcall((ary), rb_intern("size"), 0))
39
39
  #define rx_hash_size(hsh) \
40
40
  NUM2INT(rb_funcall((hsh), rb_intern("size"), 0))
41
-
42
-
data/ext/libsvm/svm.cpp CHANGED
@@ -2048,6 +2048,24 @@ static void svm_group_classes(const svm_problem *prob, int *nr_class_ret, int **
2048
2048
  }
2049
2049
  }
2050
2050
 
2051
+ //
2052
+ // Labels are ordered by their first occurrence in the training set.
2053
+ // However, for two-class sets with -1/+1 labels and -1 appears first,
2054
+ // we swap labels to ensure that internally the binary SVM has positive data corresponding to the +1 instances.
2055
+ //
2056
+ if (nr_class == 2 && label[0] == -1 && label[1] == 1)
2057
+ {
2058
+ swap(label[0],label[1]);
2059
+ swap(count[0],count[1]);
2060
+ for(i=0;i<l;i++)
2061
+ {
2062
+ if(data_label[i] == 0)
2063
+ data_label[i] = 1;
2064
+ else
2065
+ data_label[i] = 0;
2066
+ }
2067
+ }
2068
+
2051
2069
  int *start = Malloc(int,nr_class);
2052
2070
  start[0] = 0;
2053
2071
  for(i=1;i<nr_class;i++)
@@ -2321,11 +2339,16 @@ svm_model *svm_train(const svm_problem *prob, const svm_parameter *param)
2321
2339
  void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
2322
2340
  {
2323
2341
  int i;
2324
- int *fold_start = Malloc(int,nr_fold+1);
2342
+ int *fold_start;
2325
2343
  int l = prob->l;
2326
2344
  int *perm = Malloc(int,l);
2327
2345
  int nr_class;
2328
-
2346
+ if (nr_fold > l)
2347
+ {
2348
+ nr_fold = l;
2349
+ fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n");
2350
+ }
2351
+ fold_start = Malloc(int,nr_fold+1);
2329
2352
  // stratified cv may not give leave-one-out rate
2330
2353
  // Each class to l folds -> some folds may have zero elements
2331
2354
  if((param->svm_type == C_SVC ||
@@ -2745,9 +2768,9 @@ svm_model *svm_load_model(const char *model_file_name)
2745
2768
  model->rho = NULL;
2746
2769
  model->probA = NULL;
2747
2770
  model->probB = NULL;
2771
+ model->sv_indices = NULL;
2748
2772
  model->label = NULL;
2749
2773
  model->nSV = NULL;
2750
- model->sv_indices = NULL;
2751
2774
 
2752
2775
  char cmd[81];
2753
2776
  while(1)
data/ext/libsvm/svm.h CHANGED
@@ -1,7 +1,7 @@
1
1
  #ifndef _LIBSVM_H
2
2
  #define _LIBSVM_H
3
3
 
4
- #define LIBSVM_VERSION 316
4
+ #define LIBSVM_VERSION 317
5
5
 
6
6
  #ifdef __cplusplus
7
7
  extern "C" {
@@ -1,3 +1,3 @@
1
1
  module Libsvm
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-libsvm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
5
- prerelease:
4
+ version: 1.1.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - C. Florian Ebeling
@@ -10,38 +9,34 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-03-20 00:00:00.000000000 Z
12
+ date: 2014-03-20 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rake-compiler
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
20
  version: '0'
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - ">="
29
26
  - !ruby/object:Gem::Version
30
27
  version: '0'
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: rspec
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - ">="
37
33
  - !ruby/object:Gem::Version
38
34
  version: 2.7.0
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - ">="
45
40
  - !ruby/object:Gem::Version
46
41
  version: 2.7.0
47
42
  description: libsvm and ruby without using swig
@@ -53,9 +48,9 @@ extensions:
53
48
  - ext/libsvm/extconf.rb
54
49
  extra_rdoc_files: []
55
50
  files:
56
- - .gitignore
57
- - .rvmrc
58
- - .travis.yml
51
+ - ".gitignore"
52
+ - ".rvmrc"
53
+ - ".travis.yml"
59
54
  - Gemfile
60
55
  - LIBSVM-LICENSE
61
56
  - MIT-LICENSE
@@ -81,33 +76,26 @@ files:
81
76
  - spec/usage_spec.rb
82
77
  homepage: https://github.com/febeling/rb-libsvm
83
78
  licenses: []
79
+ metadata: {}
84
80
  post_install_message:
85
81
  rdoc_options: []
86
82
  require_paths:
87
83
  - lib
88
84
  required_ruby_version: !ruby/object:Gem::Requirement
89
- none: false
90
85
  requirements:
91
- - - ! '>='
86
+ - - ">="
92
87
  - !ruby/object:Gem::Version
93
88
  version: '0'
94
- segments:
95
- - 0
96
- hash: 4124111436623898286
97
89
  required_rubygems_version: !ruby/object:Gem::Requirement
98
- none: false
99
90
  requirements:
100
- - - ! '>='
91
+ - - ">="
101
92
  - !ruby/object:Gem::Version
102
93
  version: '0'
103
- segments:
104
- - 0
105
- hash: 4124111436623898286
106
94
  requirements: []
107
95
  rubyforge_project: rb-libsvm
108
- rubygems_version: 1.8.23
96
+ rubygems_version: 2.2.2
109
97
  signing_key:
110
- specification_version: 3
98
+ specification_version: 4
111
99
  summary: Ruby language bindings for LIBSVM
112
100
  test_files:
113
101
  - spec/model_spec.rb