tomz-libsvm-ruby-swig 0.3.1 → 0.3.2

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.
Files changed (5) hide show
  1. data/Rakefile +1 -1
  2. data/ext/libsvm_wrap.cxx +0 -143
  3. data/ext/svm.cpp +90 -116
  4. data/ext/svm.h +4 -9
  5. metadata +1 -1
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ task :default => ["sync_files","make_gem"]
5
5
 
6
6
  EXT = "ext/svm?.#{Hoe::DLEXT}"
7
7
 
8
- Hoe.new('libsvm-ruby-swig', '0.3.1') do |p|
8
+ Hoe.new('libsvm-ruby-swig', '0.3.2') do |p|
9
9
  p.author = 'Tom Zeng'
10
10
  p.email = 'tom.z.zeng@gmail.com'
11
11
  p.url = 'http://www.tomzconsulting.com'
data/ext/libsvm_wrap.cxx CHANGED
@@ -3296,38 +3296,6 @@ fail:
3296
3296
  }
3297
3297
 
3298
3298
 
3299
- SWIGINTERN VALUE
3300
- _wrap_svm_get_obj(int argc, VALUE *argv, VALUE self) {
3301
- svm_model *arg1 = (svm_model *) 0 ;
3302
- int arg2 ;
3303
- void *argp1 = 0 ;
3304
- int res1 = 0 ;
3305
- int val2 ;
3306
- int ecode2 = 0 ;
3307
- double result;
3308
- VALUE vresult = Qnil;
3309
-
3310
- if ((argc < 2) || (argc > 2)) {
3311
- rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)",argc); SWIG_fail;
3312
- }
3313
- res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_svm_model, 0 | 0 );
3314
- if (!SWIG_IsOK(res1)) {
3315
- SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "svm_model const *","svm_get_obj", 1, argv[0] ));
3316
- }
3317
- arg1 = reinterpret_cast< svm_model * >(argp1);
3318
- ecode2 = SWIG_AsVal_int(argv[1], &val2);
3319
- if (!SWIG_IsOK(ecode2)) {
3320
- SWIG_exception_fail(SWIG_ArgError(ecode2), Ruby_Format_TypeError( "", "int","svm_get_obj", 2, argv[1] ));
3321
- }
3322
- arg2 = static_cast< int >(val2);
3323
- result = (double)svm_get_obj((svm_model const *)arg1,arg2);
3324
- vresult = SWIG_From_double(static_cast< double >(result));
3325
- return vresult;
3326
- fail:
3327
- return Qnil;
3328
- }
3329
-
3330
-
3331
3299
  SWIGINTERN VALUE
3332
3300
  _wrap_svm_get_svm_type(int argc, VALUE *argv, VALUE self) {
3333
3301
  svm_model *arg1 = (svm_model *) 0 ;
@@ -3615,112 +3583,6 @@ fail:
3615
3583
  }
3616
3584
 
3617
3585
 
3618
- SWIGINTERN VALUE
3619
- _wrap_svm_get_model_rho(int argc, VALUE *argv, VALUE self) {
3620
- svm_model *arg1 = (svm_model *) 0 ;
3621
- void *argp1 = 0 ;
3622
- int res1 = 0 ;
3623
- double result;
3624
- VALUE vresult = Qnil;
3625
-
3626
- if ((argc < 1) || (argc > 1)) {
3627
- rb_raise(rb_eArgError, "wrong # of arguments(%d for 1)",argc); SWIG_fail;
3628
- }
3629
- res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_svm_model, 0 | 0 );
3630
- if (!SWIG_IsOK(res1)) {
3631
- SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "svm_model *","svm_get_model_rho", 1, argv[0] ));
3632
- }
3633
- arg1 = reinterpret_cast< svm_model * >(argp1);
3634
- result = (double)svm_get_model_rho(arg1);
3635
- vresult = SWIG_From_double(static_cast< double >(result));
3636
- return vresult;
3637
- fail:
3638
- return Qnil;
3639
- }
3640
-
3641
-
3642
- SWIGINTERN VALUE
3643
- _wrap_svm_get_model_num_coefs(int argc, VALUE *argv, VALUE self) {
3644
- svm_model *arg1 = (svm_model *) 0 ;
3645
- void *argp1 = 0 ;
3646
- int res1 = 0 ;
3647
- int result;
3648
- VALUE vresult = Qnil;
3649
-
3650
- if ((argc < 1) || (argc > 1)) {
3651
- rb_raise(rb_eArgError, "wrong # of arguments(%d for 1)",argc); SWIG_fail;
3652
- }
3653
- res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_svm_model, 0 | 0 );
3654
- if (!SWIG_IsOK(res1)) {
3655
- SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "svm_model *","svm_get_model_num_coefs", 1, argv[0] ));
3656
- }
3657
- arg1 = reinterpret_cast< svm_model * >(argp1);
3658
- result = (int)svm_get_model_num_coefs(arg1);
3659
- vresult = SWIG_From_int(static_cast< int >(result));
3660
- return vresult;
3661
- fail:
3662
- return Qnil;
3663
- }
3664
-
3665
-
3666
- SWIGINTERN VALUE
3667
- _wrap_svm_get_model_coefs(int argc, VALUE *argv, VALUE self) {
3668
- svm_model *arg1 = (svm_model *) 0 ;
3669
- double *arg2 = (double *) 0 ;
3670
- void *argp1 = 0 ;
3671
- int res1 = 0 ;
3672
- void *argp2 = 0 ;
3673
- int res2 = 0 ;
3674
-
3675
- if ((argc < 2) || (argc > 2)) {
3676
- rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)",argc); SWIG_fail;
3677
- }
3678
- res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_svm_model, 0 | 0 );
3679
- if (!SWIG_IsOK(res1)) {
3680
- SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "svm_model *","svm_get_model_coefs", 1, argv[0] ));
3681
- }
3682
- arg1 = reinterpret_cast< svm_model * >(argp1);
3683
- res2 = SWIG_ConvertPtr(argv[1], &argp2,SWIGTYPE_p_double, 0 | 0 );
3684
- if (!SWIG_IsOK(res2)) {
3685
- SWIG_exception_fail(SWIG_ArgError(res2), Ruby_Format_TypeError( "", "double *","svm_get_model_coefs", 2, argv[1] ));
3686
- }
3687
- arg2 = reinterpret_cast< double * >(argp2);
3688
- svm_get_model_coefs(arg1,arg2);
3689
- return Qnil;
3690
- fail:
3691
- return Qnil;
3692
- }
3693
-
3694
-
3695
- SWIGINTERN VALUE
3696
- _wrap_svm_get_model_perm(int argc, VALUE *argv, VALUE self) {
3697
- svm_model *arg1 = (svm_model *) 0 ;
3698
- int *arg2 = (int *) 0 ;
3699
- void *argp1 = 0 ;
3700
- int res1 = 0 ;
3701
- void *argp2 = 0 ;
3702
- int res2 = 0 ;
3703
-
3704
- if ((argc < 2) || (argc > 2)) {
3705
- rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)",argc); SWIG_fail;
3706
- }
3707
- res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_svm_model, 0 | 0 );
3708
- if (!SWIG_IsOK(res1)) {
3709
- SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "svm_model *","svm_get_model_perm", 1, argv[0] ));
3710
- }
3711
- arg1 = reinterpret_cast< svm_model * >(argp1);
3712
- res2 = SWIG_ConvertPtr(argv[1], &argp2,SWIGTYPE_p_int, 0 | 0 );
3713
- if (!SWIG_IsOK(res2)) {
3714
- SWIG_exception_fail(SWIG_ArgError(res2), Ruby_Format_TypeError( "", "int *","svm_get_model_perm", 2, argv[1] ));
3715
- }
3716
- arg2 = reinterpret_cast< int * >(argp2);
3717
- svm_get_model_perm(arg1,arg2);
3718
- return Qnil;
3719
- fail:
3720
- return Qnil;
3721
- }
3722
-
3723
-
3724
3586
  SWIGINTERN VALUE
3725
3587
  _wrap_new_int(int argc, VALUE *argv, VALUE self) {
3726
3588
  size_t arg1 ;
@@ -4513,7 +4375,6 @@ SWIGEXPORT void Init_libsvm(void) {
4513
4375
  rb_define_module_function(mLibsvm, "svm_cross_validation", VALUEFUNC(_wrap_svm_cross_validation), -1);
4514
4376
  rb_define_module_function(mLibsvm, "svm_save_model", VALUEFUNC(_wrap_svm_save_model), -1);
4515
4377
  rb_define_module_function(mLibsvm, "svm_load_model", VALUEFUNC(_wrap_svm_load_model), -1);
4516
- rb_define_module_function(mLibsvm, "svm_get_obj", VALUEFUNC(_wrap_svm_get_obj), -1);
4517
4378
  rb_define_module_function(mLibsvm, "svm_get_svm_type", VALUEFUNC(_wrap_svm_get_svm_type), -1);
4518
4379
  rb_define_module_function(mLibsvm, "svm_get_nr_class", VALUEFUNC(_wrap_svm_get_nr_class), -1);
4519
4380
  rb_define_module_function(mLibsvm, "svm_get_labels", VALUEFUNC(_wrap_svm_get_labels), -1);
@@ -4524,10 +4385,6 @@ SWIGEXPORT void Init_libsvm(void) {
4524
4385
  rb_define_module_function(mLibsvm, "svm_destroy_model", VALUEFUNC(_wrap_svm_destroy_model), -1);
4525
4386
  rb_define_module_function(mLibsvm, "svm_check_parameter", VALUEFUNC(_wrap_svm_check_parameter), -1);
4526
4387
  rb_define_module_function(mLibsvm, "svm_check_probability_model", VALUEFUNC(_wrap_svm_check_probability_model), -1);
4527
- rb_define_module_function(mLibsvm, "svm_get_model_rho", VALUEFUNC(_wrap_svm_get_model_rho), -1);
4528
- rb_define_module_function(mLibsvm, "svm_get_model_num_coefs", VALUEFUNC(_wrap_svm_get_model_num_coefs), -1);
4529
- rb_define_module_function(mLibsvm, "svm_get_model_coefs", VALUEFUNC(_wrap_svm_get_model_coefs), -1);
4530
- rb_define_module_function(mLibsvm, "svm_get_model_perm", VALUEFUNC(_wrap_svm_get_model_perm), -1);
4531
4388
  rb_define_module_function(mLibsvm, "new_int", VALUEFUNC(_wrap_new_int), -1);
4532
4389
  rb_define_module_function(mLibsvm, "delete_int", VALUEFUNC(_wrap_delete_int), -1);
4533
4390
  rb_define_module_function(mLibsvm, "int_getitem", VALUEFUNC(_wrap_int_getitem), -1);
data/ext/svm.cpp CHANGED
@@ -6,6 +6,7 @@
6
6
  #include <string.h>
7
7
  #include <stdarg.h>
8
8
  #include "svm.h"
9
+ int libsvm_version = LIBSVM_VERSION;
9
10
  typedef float Qfloat;
10
11
  typedef signed char schar;
11
12
  #ifndef min
@@ -22,36 +23,40 @@ template <class S, class T> inline void clone(T*& dst, S* src, int n)
22
23
  }
23
24
  inline double powi(double base, int times)
24
25
  {
25
- double tmp = base, ret = 1.0;
26
+ double tmp = base, ret = 1.0;
26
27
 
27
- for(int t=times; t>0; t/=2)
28
+ for(int t=times; t>0; t/=2)
28
29
  {
29
- if(t%2==1) ret*=tmp;
30
- tmp = tmp * tmp;
31
- }
32
- return ret;
30
+ if(t%2==1) ret*=tmp;
31
+ tmp = tmp * tmp;
32
+ }
33
+ return ret;
33
34
  }
34
35
  #define INF HUGE_VAL
35
36
  #define TAU 1e-12
36
37
  #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
38
+
39
+ static void print_string_stdout(const char *s)
40
+ {
41
+ fputs(s,stdout);
42
+ fflush(stdout);
43
+ }
44
+ void (*svm_print_string) (const char *) = &print_string_stdout;
37
45
  #if 1
38
46
  int info_on = 0;
39
47
  static void info(const char *fmt,...)
40
48
  {
49
+ char buf[BUFSIZ];
41
50
  va_list ap;
42
51
  if (info_on==1) {
43
- va_start(ap,fmt);
44
- vprintf(fmt,ap);
45
- va_end(ap);
52
+ va_start(ap,fmt);
53
+ vsprintf(buf,fmt,ap);
54
+ va_end(ap);
55
+ (*svm_print_string)(buf);
46
56
  }
47
57
  }
48
- static void info_flush()
49
- {
50
- if (info_on==1) fflush(stdout);
51
- }
52
58
  #else
53
- static void info(char *fmt,...) {}
54
- static void info_flush() {}
59
+ static void info(const char *fmt,...) {}
55
60
  #endif
56
61
 
57
62
  //
@@ -564,7 +569,7 @@ void Solver::Solve(int l, const QMatrix& Q, const double *p_, const schar *y_,
564
569
  {
565
570
  counter = min(l,1000);
566
571
  if(shrinking) do_shrinking();
567
- info("."); info_flush();
572
+ info(".");
568
573
  }
569
574
 
570
575
  int i,j;
@@ -574,7 +579,7 @@ void Solver::Solve(int l, const QMatrix& Q, const double *p_, const schar *y_,
574
579
  reconstruct_gradient();
575
580
  // reset active set size and check
576
581
  active_size = l;
577
- info("*"); info_flush();
582
+ info("*");
578
583
  if(select_working_set(i,j)!=0)
579
584
  break;
580
585
  else
@@ -927,7 +932,7 @@ void Solver::do_shrinking()
927
932
  unshrink = true;
928
933
  reconstruct_gradient();
929
934
  active_size = l;
930
- info("*"); info_flush();
935
+ info("*");
931
936
  }
932
937
 
933
938
  for(i=0;i<active_size;i++)
@@ -1110,7 +1115,7 @@ int Solver_NU::select_working_set(int &out_i, int &out_j)
1110
1115
  }
1111
1116
 
1112
1117
  if(max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps)
1113
- return 1;
1118
+ return 1;
1114
1119
 
1115
1120
  if (y[Gmin_idx] == +1)
1116
1121
  out_i = Gmaxp_idx;
@@ -1388,7 +1393,7 @@ public:
1388
1393
  Qfloat *buf = buffer[next_buffer];
1389
1394
  next_buffer = 1 - next_buffer;
1390
1395
  schar si = sign[i];
1391
- for(int j=0;j<len;j++)
1396
+ for(j=0;j<len;j++)
1392
1397
  buf[j] = (Qfloat) si * (Qfloat) sign[j] * data[index[j]];
1393
1398
  return buf;
1394
1399
  }
@@ -1625,7 +1630,6 @@ struct decision_function
1625
1630
  {
1626
1631
  double *alpha;
1627
1632
  double rho;
1628
- double obj;
1629
1633
  };
1630
1634
 
1631
1635
  decision_function svm_train_one(
@@ -1682,7 +1686,6 @@ decision_function svm_train_one(
1682
1686
  decision_function f;
1683
1687
  f.alpha = alpha;
1684
1688
  f.rho = si.rho;
1685
- f.obj = si.obj;
1686
1689
  return f;
1687
1690
  }
1688
1691
 
@@ -1697,11 +1700,9 @@ struct svm_model
1697
1700
  svm_node **SV; // SVs (SV[l])
1698
1701
  double **sv_coef; // coefficients for SVs in decision functions (sv_coef[k-1][l])
1699
1702
  double *rho; // constants in decision functions (rho[k*(k-1)/2])
1700
- double *probA; // pariwise probability information
1703
+ double *probA; // pariwise probability information
1701
1704
  double *probB;
1702
1705
 
1703
- double *obj;
1704
-
1705
1706
  // for classification only
1706
1707
 
1707
1708
  int *label; // label of each class (label[k])
@@ -1712,48 +1713,6 @@ struct svm_model
1712
1713
  // 0 if svm_model is created by svm_train
1713
1714
  };
1714
1715
 
1715
- // Get the rho element of the model. Only works when there are two
1716
- // labels (1 classifier).
1717
- double svm_get_model_rho(struct svm_model *model)
1718
- {
1719
- if (model->nr_class > 2)
1720
- info("warning: rho requested for model with more than 2 labels");
1721
- return model->rho[0];
1722
- }
1723
-
1724
- int svm_get_model_num_coefs(struct svm_model *model)
1725
- {
1726
- return model->l;
1727
- }
1728
-
1729
- // Get the coefficients of the model. Only works when there are two
1730
- // labels (1 classifier).
1731
- void svm_get_model_coefs(struct svm_model *model, double* out_array)
1732
- {
1733
- if (model->nr_class > 2)
1734
- info("warning: coefficients requested for model with more than 2 labels");
1735
- memcpy(out_array, model->sv_coef[0], sizeof(double) * model->l);
1736
- }
1737
-
1738
- // Get the permutation of the indices of the coefficients w.r.t. to the input problem.
1739
- void svm_get_model_perm(struct svm_model *model, int* out_array)
1740
- {
1741
- if (model->nr_class > 2)
1742
- info("warning: permutation requested for model with more than 2 labels");
1743
- int i;
1744
- for ( i = 0; i < model->l; ++i)
1745
- {
1746
- struct svm_node* n = model->SV[i];
1747
- if (0 != n->index)
1748
- {
1749
- info("warning: missing 0 index");
1750
- out_array[i] = -1;
1751
- }
1752
- else
1753
- out_array[i] = (int)n->value;
1754
- }
1755
- }
1756
-
1757
1716
  // Platt's binary SVM Probablistic Output: an improvement from Lin et al.
1758
1717
  void sigmoid_train(
1759
1718
  int l, const double *dec_values, const double *labels,
@@ -1766,7 +1725,7 @@ void sigmoid_train(
1766
1725
  if (labels[i] > 0) prior1+=1;
1767
1726
  else prior0+=1;
1768
1727
 
1769
- int max_iter=100; // Maximal number of iterations
1728
+ int max_iter=100; // Maximal number of iterations
1770
1729
  double min_step=1e-10; // Minimal step taken in line search
1771
1730
  double sigma=1e-12; // For numerically strict PD of Hessian
1772
1731
  double eps=1e-5;
@@ -1830,7 +1789,7 @@ void sigmoid_train(
1830
1789
  gd=g1*dA+g2*dB;
1831
1790
 
1832
1791
 
1833
- stepsize = 1; // Line Search
1792
+ stepsize = 1; // Line Search
1834
1793
  while (stepsize >= min_step)
1835
1794
  {
1836
1795
  newA = A + stepsize * dA;
@@ -2050,10 +2009,10 @@ double svm_svr_probability(
2050
2009
  int count=0;
2051
2010
  mae=0;
2052
2011
  for(i=0;i<prob->l;i++)
2053
- if (fabs(ymv[i]) > 5*std)
2054
- count=count+1;
2012
+ if (fabs(ymv[i]) > 5*std)
2013
+ count=count+1;
2055
2014
  else
2056
- mae+=fabs(ymv[i]);
2015
+ mae+=fabs(ymv[i]);
2057
2016
  mae /= (prob->l-count);
2058
2017
  info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n",mae);
2059
2018
  free(ymv);
@@ -2151,8 +2110,6 @@ svm_model *svm_train(const svm_problem *prob, const svm_parameter *param)
2151
2110
  decision_function f = svm_train_one(prob,param,0,0);
2152
2111
  model->rho = Malloc(double,1);
2153
2112
  model->rho[0] = f.rho;
2154
- model->obj = Malloc(double,1);
2155
- model->obj[0] = f.obj;
2156
2113
 
2157
2114
  int nSV = 0;
2158
2115
  int i;
@@ -2266,13 +2223,8 @@ svm_model *svm_train(const svm_problem *prob, const svm_parameter *param)
2266
2223
  model->label[i] = label[i];
2267
2224
 
2268
2225
  model->rho = Malloc(double,nr_class*(nr_class-1)/2);
2269
- model->obj = Malloc(double,nr_class*(nr_class-1)/2);
2270
2226
  for(i=0;i<nr_class*(nr_class-1)/2;i++)
2271
- {
2272
2227
  model->rho[i] = f[i].rho;
2273
- model->obj[i] = f[i].obj;
2274
- }
2275
-
2276
2228
 
2277
2229
  if(param->probability)
2278
2230
  {
@@ -2482,10 +2434,6 @@ void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, i
2482
2434
  free(perm);
2483
2435
  }
2484
2436
 
2485
- double svm_get_obj(const svm_model *model, const int i)
2486
- {
2487
- return model->obj[i];
2488
- }
2489
2437
 
2490
2438
  int svm_get_svm_type(const svm_model *model)
2491
2439
  {
@@ -2511,7 +2459,7 @@ double svm_get_svr_probability(const svm_model *model)
2511
2459
  return model->probA[0];
2512
2460
  else
2513
2461
  {
2514
- info("Model doesn't contain information for SVR probability inference\n");
2462
+ fprintf(stderr,"Model doesn't contain information for SVR probability inference\n");
2515
2463
  return 0;
2516
2464
  }
2517
2465
  }
@@ -2647,7 +2595,7 @@ double svm_predict_probability(
2647
2595
  for(i=0;i<nr_class;i++)
2648
2596
  free(pairwise_prob[i]);
2649
2597
  free(dec_values);
2650
- free(pairwise_prob);
2598
+ free(pairwise_prob);
2651
2599
  return model->label[prob_max_idx];
2652
2600
  }
2653
2601
  else
@@ -2751,6 +2699,27 @@ int svm_save_model(const char *model_file_name, const svm_model *model)
2751
2699
  else return 0;
2752
2700
  }
2753
2701
 
2702
+ static char *line = NULL;
2703
+ static int max_line_len;
2704
+
2705
+ static char* readline(FILE *input)
2706
+ {
2707
+ int len;
2708
+
2709
+ if(fgets(line,max_line_len,input) == NULL)
2710
+ return NULL;
2711
+
2712
+ while(strrchr(line,'\n') == NULL)
2713
+ {
2714
+ max_line_len *= 2;
2715
+ line = (char *) realloc(line,max_line_len);
2716
+ len = (int) strlen(line);
2717
+ if(fgets(line+len,max_line_len-len,input) == NULL)
2718
+ break;
2719
+ }
2720
+ return line;
2721
+ }
2722
+
2754
2723
  svm_model *svm_load_model(const char *model_file_name)
2755
2724
  {
2756
2725
  FILE *fp = fopen(model_file_name,"rb");
@@ -2766,8 +2735,6 @@ svm_model *svm_load_model(const char *model_file_name)
2766
2735
  model->label = NULL;
2767
2736
  model->nSV = NULL;
2768
2737
 
2769
- model->obj = NULL;
2770
-
2771
2738
  char cmd[81];
2772
2739
  while(1)
2773
2740
  {
@@ -2789,7 +2756,6 @@ svm_model *svm_load_model(const char *model_file_name)
2789
2756
  {
2790
2757
  fprintf(stderr,"unknown svm type.\n");
2791
2758
  free(model->rho);
2792
- free(model->obj);
2793
2759
  free(model->label);
2794
2760
  free(model->nSV);
2795
2761
  free(model);
@@ -2812,7 +2778,6 @@ svm_model *svm_load_model(const char *model_file_name)
2812
2778
  {
2813
2779
  fprintf(stderr,"unknown kernel function.\n");
2814
2780
  free(model->rho);
2815
- free(model->obj);
2816
2781
  free(model->label);
2817
2782
  free(model->nSV);
2818
2783
  free(model);
@@ -2877,7 +2842,6 @@ svm_model *svm_load_model(const char *model_file_name)
2877
2842
  {
2878
2843
  fprintf(stderr,"unknown text in model file: [%s]\n",cmd);
2879
2844
  free(model->rho);
2880
- free(model->obj);
2881
2845
  free(model->label);
2882
2846
  free(model->nSV);
2883
2847
  free(model);
@@ -2890,23 +2854,23 @@ svm_model *svm_load_model(const char *model_file_name)
2890
2854
  int elements = 0;
2891
2855
  long pos = ftell(fp);
2892
2856
 
2893
- while(1)
2857
+ max_line_len = 1024;
2858
+ line = Malloc(char,max_line_len);
2859
+ char *p,*endptr,*idx,*val;
2860
+
2861
+ while(readline(fp)!=NULL)
2894
2862
  {
2895
- int c = fgetc(fp);
2896
- switch(c)
2863
+ p = strtok(line,":");
2864
+ while(1)
2897
2865
  {
2898
- case '\n':
2899
- // count the '-1' element
2900
- case ':':
2901
- ++elements;
2866
+ p = strtok(NULL,":");
2867
+ if(p == NULL)
2902
2868
  break;
2903
- case EOF:
2904
- goto out;
2905
- default:
2906
- ;
2869
+ ++elements;
2907
2870
  }
2908
2871
  }
2909
- out:
2872
+ elements += model->l;
2873
+
2910
2874
  fseek(fp,pos,SEEK_SET);
2911
2875
 
2912
2876
  int m = model->nr_class - 1;
@@ -2916,30 +2880,41 @@ out:
2916
2880
  for(i=0;i<m;i++)
2917
2881
  model->sv_coef[i] = Malloc(double,l);
2918
2882
  model->SV = Malloc(svm_node*,l);
2919
- svm_node *x_space=NULL;
2883
+ svm_node *x_space = NULL;
2920
2884
  if(l>0) x_space = Malloc(svm_node,elements);
2921
2885
 
2922
2886
  int j=0;
2923
2887
  for(i=0;i<l;i++)
2924
2888
  {
2889
+ readline(fp);
2925
2890
  model->SV[i] = &x_space[j];
2926
- for(int k=0;k<m;k++)
2927
- fscanf(fp,"%lf",&model->sv_coef[k][i]);
2891
+
2892
+ p = strtok(line, " \t");
2893
+ model->sv_coef[0][i] = strtod(p,&endptr);
2894
+ for(int k=1;k<m;k++)
2895
+ {
2896
+ p = strtok(NULL, " \t");
2897
+ model->sv_coef[k][i] = strtod(p,&endptr);
2898
+ }
2899
+
2928
2900
  while(1)
2929
2901
  {
2930
- int c;
2931
- do {
2932
- c = getc(fp);
2933
- if(c=='\n') goto out2;
2934
- } while(isspace(c));
2935
- ungetc(c,fp);
2936
- fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value));
2902
+ idx = strtok(NULL, ":");
2903
+ val = strtok(NULL, " \t");
2904
+
2905
+ if(val == NULL)
2906
+ break;
2907
+ x_space[j].index = (int) strtol(idx,&endptr,10);
2908
+ x_space[j].value = strtod(val,&endptr);
2909
+
2937
2910
  ++j;
2938
- }
2939
- out2:
2911
+ }
2940
2912
  x_space[j++].index = -1;
2941
2913
  }
2942
- if (ferror(fp) != 0 || fclose(fp) != 0) return NULL;
2914
+ free(line);
2915
+
2916
+ if (ferror(fp) != 0 || fclose(fp) != 0)
2917
+ return NULL;
2943
2918
 
2944
2919
  model->free_sv = 1; // XXX
2945
2920
  return model;
@@ -2954,7 +2929,6 @@ void svm_destroy_model(svm_model* model)
2954
2929
  free(model->SV);
2955
2930
  free(model->sv_coef);
2956
2931
  free(model->rho);
2957
- free(model->obj);
2958
2932
  free(model->label);
2959
2933
  free(model->probA);
2960
2934
  free(model->probB);
data/ext/svm.h CHANGED
@@ -1,12 +1,14 @@
1
1
  #ifndef _LIBSVM_H
2
2
  #define _LIBSVM_H
3
3
 
4
- #define LIBSVM_VERSION 288
4
+ #define LIBSVM_VERSION 289
5
5
 
6
6
  #ifdef __cplusplus
7
7
  extern "C" {
8
8
  #endif
9
9
 
10
+ extern int libsvm_version;
11
+
10
12
  struct svm_node
11
13
  {
12
14
  int index;
@@ -50,8 +52,6 @@ void svm_cross_validation(const struct svm_problem *prob, const struct svm_param
50
52
  int svm_save_model(const char *model_file_name, const struct svm_model *model);
51
53
  struct svm_model *svm_load_model(const char *model_file_name);
52
54
 
53
- double svm_get_obj(const struct svm_model *model, const int i);
54
-
55
55
  int svm_get_svm_type(const struct svm_model *model);
56
56
  int svm_get_nr_class(const struct svm_model *model);
57
57
  void svm_get_labels(const struct svm_model *model, int *label);
@@ -67,12 +67,7 @@ void svm_destroy_param(struct svm_parameter *param);
67
67
  const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
68
68
  int svm_check_probability_model(const struct svm_model *model);
69
69
 
70
- double svm_get_model_rho(struct svm_model *model);
71
- int svm_get_model_num_coefs(struct svm_model *model);
72
- // out_array must have svm_get_model_num_coefs(model) elements
73
- void svm_get_model_coefs(struct svm_model *model, double* out_array);
74
- // out_array must have svm_get_model_num_coef(model)s elements
75
- void svm_get_model_perm(struct svm_model *model, int* out_array);
70
+ extern void (*svm_print_string) (const char *);
76
71
 
77
72
  #ifdef __cplusplus
78
73
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomz-libsvm-ruby-swig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Zeng