rb-libsvm 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28c8bcde167de58a135e78ebfb547d930adf2ddf
4
- data.tar.gz: 5894971f3815bdc838ff68cd940e88008930bca0
3
+ metadata.gz: 50ef30c4d3cfe8e3785f6c408a48a34b5ff585b2
4
+ data.tar.gz: 80bbaef88fe9e9738b61fde4804c592e4e053367
5
5
  SHA512:
6
- metadata.gz: 7d14745db75f09d07c1c61b35221805abf3f51bbf2aef31e295423474913011961fd5849e69cbf01e0127a2bb5cc628387229675ec92bfc07665bd2355a56705
7
- data.tar.gz: 73e039abd412ff291514c559499ee5508684390f6ee335889c2a93bd4dd7b3a1a07ef9729f6d9348f039945d25f92de94dc20e2afb82210fddbdccfc6cbac77d
6
+ metadata.gz: 1b8a9ff101bdc72026634f744583f858f1963b7241a18068a22fdd3ac0d84413937ed23093a34507a10f42052e9a73b79c396e85bf6e4b542c8c6c96249ea79a
7
+ data.tar.gz: 13de67e9c95836e814cc88077f8e9c59977f27dd9fa4e4171591c60f93059216046676a4ecc933ab9e37a47fb2e6b795e1aed45ccf82e0b0a8ef29bb8abd3ebb
data/.travis.yml CHANGED
@@ -1,7 +1,8 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.8.7
3
- - 1.9.2
4
4
  - 1.9.3
5
5
  - 2.0.0
6
- - rbx-18mode
7
- - rbx-19mode
6
+ - 2.1.1
7
+ - rbx
8
+ - ruby-head
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.17.
24
+ Currently this package includes libsvm version 3.18.
25
25
 
26
26
  ## Dependencies
27
27
 
@@ -110,4 +110,4 @@ http://www.igvita.com/2008/01/07/support-vector-machines-svm-in-ruby/
110
110
 
111
111
  [ruby-svm]: http://sourceforge.net/projects/rubysvm/
112
112
 
113
- [README]: https://github.com/febeling/libsvm/blob/master/README
113
+ [README]: https://github.com/cjlin1/libsvm/blob/master/README
data/ext/libsvm/svm.cpp CHANGED
@@ -74,7 +74,7 @@ public:
74
74
  // return some position p where [p,len) need to be filled
75
75
  // (p >= len if nothing needs to be filled)
76
76
  int get_data(const int index, Qfloat **data, int len);
77
- void swap_index(int i, int j);
77
+ void swap_index(int i, int j);
78
78
  private:
79
79
  int l;
80
80
  long int size;
@@ -444,7 +444,7 @@ protected:
444
444
  virtual double calculate_rho();
445
445
  virtual void do_shrinking();
446
446
  private:
447
- bool be_shrunk(int i, double Gmax1, double Gmax2);
447
+ bool be_shrunk(int i, double Gmax1, double Gmax2);
448
448
  };
449
449
 
450
450
  void Solver::swap_index(int i, int j)
@@ -833,7 +833,7 @@ int Solver::select_working_set(int &out_i, int &out_j)
833
833
  Gmax2 = G[j];
834
834
  if (grad_diff > 0)
835
835
  {
836
- double obj_diff;
836
+ double obj_diff;
837
837
  double quad_coef = QD[i]+QD[j]-2.0*y[i]*Q_i[j];
838
838
  if (quad_coef > 0)
839
839
  obj_diff = -(grad_diff*grad_diff)/quad_coef;
@@ -857,7 +857,7 @@ int Solver::select_working_set(int &out_i, int &out_j)
857
857
  Gmax2 = -G[j];
858
858
  if (grad_diff > 0)
859
859
  {
860
- double obj_diff;
860
+ double obj_diff;
861
861
  double quad_coef = QD[i]+QD[j]+2.0*y[i]*Q_i[j];
862
862
  if (quad_coef > 0)
863
863
  obj_diff = -(grad_diff*grad_diff)/quad_coef;
@@ -1085,7 +1085,7 @@ int Solver_NU::select_working_set(int &out_i, int &out_j)
1085
1085
  Gmaxp2 = G[j];
1086
1086
  if (grad_diff > 0)
1087
1087
  {
1088
- double obj_diff;
1088
+ double obj_diff;
1089
1089
  double quad_coef = QD[ip]+QD[j]-2*Q_ip[j];
1090
1090
  if (quad_coef > 0)
1091
1091
  obj_diff = -(grad_diff*grad_diff)/quad_coef;
@@ -1109,7 +1109,7 @@ int Solver_NU::select_working_set(int &out_i, int &out_j)
1109
1109
  Gmaxn2 = -G[j];
1110
1110
  if (grad_diff > 0)
1111
1111
  {
1112
- double obj_diff;
1112
+ double obj_diff;
1113
1113
  double quad_coef = QD[in]+QD[j]-2*Q_in[j];
1114
1114
  if (quad_coef > 0)
1115
1115
  obj_diff = -(grad_diff*grad_diff)/quad_coef;
@@ -1641,7 +1641,7 @@ static void solve_nu_svr(
1641
1641
  struct decision_function
1642
1642
  {
1643
1643
  double *alpha;
1644
- double rho;
1644
+ double rho;
1645
1645
  };
1646
1646
 
1647
1647
  static decision_function svm_train_one(
@@ -1722,7 +1722,7 @@ static void sigmoid_train(
1722
1722
  double *t=Malloc(double,l);
1723
1723
  double fApB,p,q,h11,h22,h21,g1,g2,det,dA,dB,gd,stepsize;
1724
1724
  double newA,newB,newf,d1,d2;
1725
- int iter;
1725
+ int iter;
1726
1726
 
1727
1727
  // Initial Point and Initial Fun Value
1728
1728
  A=0.0; B=log((prior0+1.0)/(prior1+1.0));
@@ -1961,7 +1961,7 @@ static void svm_binary_svc_probability(
1961
1961
  struct svm_model *submodel = svm_train(&subprob,&subparam);
1962
1962
  for(j=begin;j<end;j++)
1963
1963
  {
1964
- svm_predict_values(submodel,prob->x[perm[j]],&(dec_values[perm[j]]));
1964
+ svm_predict_values(submodel,prob->x[perm[j]],&(dec_values[perm[j]]));
1965
1965
  // ensure +1 -1 order; reason not using CV subroutine
1966
1966
  dec_values[perm[j]] *= submodel->label[0];
1967
1967
  }
@@ -2018,7 +2018,7 @@ static void svm_group_classes(const svm_problem *prob, int *nr_class_ret, int **
2018
2018
  int nr_class = 0;
2019
2019
  int *label = Malloc(int,max_nr_class);
2020
2020
  int *count = Malloc(int,max_nr_class);
2021
- int *data_label = Malloc(int,l);
2021
+ int *data_label = Malloc(int,l);
2022
2022
  int i;
2023
2023
 
2024
2024
  for(i=0;i<l;i++)
@@ -2394,9 +2394,9 @@ void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, i
2394
2394
  fold_start[0]=0;
2395
2395
  for (i=1;i<=nr_fold;i++)
2396
2396
  fold_start[i] = fold_start[i-1]+fold_count[i-1];
2397
- free(start);
2397
+ free(start);
2398
2398
  free(label);
2399
- free(count);
2399
+ free(count);
2400
2400
  free(index);
2401
2401
  free(fold_count);
2402
2402
  }
@@ -2443,7 +2443,7 @@ void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, i
2443
2443
  double *prob_estimates=Malloc(double,svm_get_nr_class(submodel));
2444
2444
  for(j=begin;j<end;j++)
2445
2445
  target[perm[j]] = svm_predict_probability(submodel,prob->x[perm[j]],prob_estimates);
2446
- free(prob_estimates);
2446
+ free(prob_estimates);
2447
2447
  }
2448
2448
  else
2449
2449
  for(j=begin;j<end;j++)
@@ -2453,7 +2453,7 @@ void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, i
2453
2453
  free(subprob.y);
2454
2454
  }
2455
2455
  free(fold_start);
2456
- free(perm);
2456
+ free(perm);
2457
2457
  }
2458
2458
 
2459
2459
 
@@ -2621,7 +2621,7 @@ double svm_predict_probability(
2621
2621
  for(i=0;i<nr_class;i++)
2622
2622
  free(pairwise_prob[i]);
2623
2623
  free(dec_values);
2624
- free(pairwise_prob);
2624
+ free(pairwise_prob);
2625
2625
  return model->label[prob_max_idx];
2626
2626
  }
2627
2627
  else
@@ -2753,33 +2753,25 @@ static char* readline(FILE *input)
2753
2753
  return line;
2754
2754
  }
2755
2755
 
2756
- svm_model *svm_load_model(const char *model_file_name)
2756
+ //
2757
+ // FSCANF helps to handle fscanf failures.
2758
+ // Its do-while block avoids the ambiguity when
2759
+ // if (...)
2760
+ // FSCANF();
2761
+ // is used
2762
+ //
2763
+ #define FSCANF(_stream, _format, _var) do{ if (fscanf(_stream, _format, _var) != 1) return false; }while(0)
2764
+ bool read_model_header(FILE *fp, svm_model* model)
2757
2765
  {
2758
- FILE *fp = fopen(model_file_name,"rb");
2759
- if(fp==NULL) return NULL;
2760
-
2761
- char *old_locale = strdup(setlocale(LC_ALL, NULL));
2762
- setlocale(LC_ALL, "C");
2763
-
2764
- // read parameters
2765
-
2766
- svm_model *model = Malloc(svm_model,1);
2767
2766
  svm_parameter& param = model->param;
2768
- model->rho = NULL;
2769
- model->probA = NULL;
2770
- model->probB = NULL;
2771
- model->sv_indices = NULL;
2772
- model->label = NULL;
2773
- model->nSV = NULL;
2774
-
2775
2767
  char cmd[81];
2776
2768
  while(1)
2777
2769
  {
2778
- fscanf(fp,"%80s",cmd);
2770
+ FSCANF(fp,"%80s",cmd);
2779
2771
 
2780
2772
  if(strcmp(cmd,"svm_type")==0)
2781
2773
  {
2782
- fscanf(fp,"%80s",cmd);
2774
+ FSCANF(fp,"%80s",cmd);
2783
2775
  int i;
2784
2776
  for(i=0;svm_type_table[i];i++)
2785
2777
  {
@@ -2792,19 +2784,12 @@ svm_model *svm_load_model(const char *model_file_name)
2792
2784
  if(svm_type_table[i] == NULL)
2793
2785
  {
2794
2786
  fprintf(stderr,"unknown svm type.\n");
2795
-
2796
- setlocale(LC_ALL, old_locale);
2797
- free(old_locale);
2798
- free(model->rho);
2799
- free(model->label);
2800
- free(model->nSV);
2801
- free(model);
2802
- return NULL;
2787
+ return false;
2803
2788
  }
2804
2789
  }
2805
2790
  else if(strcmp(cmd,"kernel_type")==0)
2806
2791
  {
2807
- fscanf(fp,"%80s",cmd);
2792
+ FSCANF(fp,"%80s",cmd);
2808
2793
  int i;
2809
2794
  for(i=0;kernel_type_table[i];i++)
2810
2795
  {
@@ -2816,85 +2801,106 @@ svm_model *svm_load_model(const char *model_file_name)
2816
2801
  }
2817
2802
  if(kernel_type_table[i] == NULL)
2818
2803
  {
2819
- fprintf(stderr,"unknown kernel function.\n");
2820
-
2821
- setlocale(LC_ALL, old_locale);
2822
- free(old_locale);
2823
- free(model->rho);
2824
- free(model->label);
2825
- free(model->nSV);
2826
- free(model);
2827
- return NULL;
2804
+ fprintf(stderr,"unknown kernel function.\n");
2805
+ return false;
2828
2806
  }
2829
2807
  }
2830
2808
  else if(strcmp(cmd,"degree")==0)
2831
- fscanf(fp,"%d",&param.degree);
2809
+ FSCANF(fp,"%d",&param.degree);
2832
2810
  else if(strcmp(cmd,"gamma")==0)
2833
- fscanf(fp,"%lf",&param.gamma);
2811
+ FSCANF(fp,"%lf",&param.gamma);
2834
2812
  else if(strcmp(cmd,"coef0")==0)
2835
- fscanf(fp,"%lf",&param.coef0);
2813
+ FSCANF(fp,"%lf",&param.coef0);
2836
2814
  else if(strcmp(cmd,"nr_class")==0)
2837
- fscanf(fp,"%d",&model->nr_class);
2815
+ FSCANF(fp,"%d",&model->nr_class);
2838
2816
  else if(strcmp(cmd,"total_sv")==0)
2839
- fscanf(fp,"%d",&model->l);
2817
+ FSCANF(fp,"%d",&model->l);
2840
2818
  else if(strcmp(cmd,"rho")==0)
2841
2819
  {
2842
2820
  int n = model->nr_class * (model->nr_class-1)/2;
2843
2821
  model->rho = Malloc(double,n);
2844
2822
  for(int i=0;i<n;i++)
2845
- fscanf(fp,"%lf",&model->rho[i]);
2823
+ FSCANF(fp,"%lf",&model->rho[i]);
2846
2824
  }
2847
2825
  else if(strcmp(cmd,"label")==0)
2848
2826
  {
2849
2827
  int n = model->nr_class;
2850
2828
  model->label = Malloc(int,n);
2851
2829
  for(int i=0;i<n;i++)
2852
- fscanf(fp,"%d",&model->label[i]);
2830
+ FSCANF(fp,"%d",&model->label[i]);
2853
2831
  }
2854
2832
  else if(strcmp(cmd,"probA")==0)
2855
2833
  {
2856
2834
  int n = model->nr_class * (model->nr_class-1)/2;
2857
2835
  model->probA = Malloc(double,n);
2858
2836
  for(int i=0;i<n;i++)
2859
- fscanf(fp,"%lf",&model->probA[i]);
2837
+ FSCANF(fp,"%lf",&model->probA[i]);
2860
2838
  }
2861
2839
  else if(strcmp(cmd,"probB")==0)
2862
2840
  {
2863
2841
  int n = model->nr_class * (model->nr_class-1)/2;
2864
2842
  model->probB = Malloc(double,n);
2865
2843
  for(int i=0;i<n;i++)
2866
- fscanf(fp,"%lf",&model->probB[i]);
2844
+ FSCANF(fp,"%lf",&model->probB[i]);
2867
2845
  }
2868
2846
  else if(strcmp(cmd,"nr_sv")==0)
2869
2847
  {
2870
2848
  int n = model->nr_class;
2871
2849
  model->nSV = Malloc(int,n);
2872
2850
  for(int i=0;i<n;i++)
2873
- fscanf(fp,"%d",&model->nSV[i]);
2851
+ FSCANF(fp,"%d",&model->nSV[i]);
2874
2852
  }
2875
2853
  else if(strcmp(cmd,"SV")==0)
2876
2854
  {
2877
2855
  while(1)
2878
2856
  {
2879
2857
  int c = getc(fp);
2880
- if(c==EOF || c=='\n') break;
2858
+ if(c==EOF || c=='\n') break;
2881
2859
  }
2882
2860
  break;
2883
2861
  }
2884
2862
  else
2885
2863
  {
2886
2864
  fprintf(stderr,"unknown text in model file: [%s]\n",cmd);
2887
-
2888
- setlocale(LC_ALL, old_locale);
2889
- free(old_locale);
2890
- free(model->rho);
2891
- free(model->label);
2892
- free(model->nSV);
2893
- free(model);
2894
- return NULL;
2865
+ return false;
2895
2866
  }
2896
2867
  }
2897
2868
 
2869
+ return true;
2870
+
2871
+ }
2872
+
2873
+ svm_model *svm_load_model(const char *model_file_name)
2874
+ {
2875
+ FILE *fp = fopen(model_file_name,"rb");
2876
+ if(fp==NULL) return NULL;
2877
+
2878
+ char *old_locale = strdup(setlocale(LC_ALL, NULL));
2879
+ setlocale(LC_ALL, "C");
2880
+
2881
+ // read parameters
2882
+
2883
+ svm_model *model = Malloc(svm_model,1);
2884
+ model->rho = NULL;
2885
+ model->probA = NULL;
2886
+ model->probB = NULL;
2887
+ model->sv_indices = NULL;
2888
+ model->label = NULL;
2889
+ model->nSV = NULL;
2890
+
2891
+ // read header
2892
+ if (!read_model_header(fp, model))
2893
+ {
2894
+ fprintf(stderr, "ERROR: fscanf failed to read model\n");
2895
+ setlocale(LC_ALL, old_locale);
2896
+ free(old_locale);
2897
+ free(model->rho);
2898
+ free(model->label);
2899
+ free(model->nSV);
2900
+ free(model);
2901
+ return NULL;
2902
+ }
2903
+
2898
2904
  // read sv_coef and SV
2899
2905
 
2900
2906
  int elements = 0;
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 317
4
+ #define LIBSVM_VERSION 318
5
5
 
6
6
  #ifdef __cplusplus
7
7
  extern "C" {
@@ -1,3 +1,3 @@
1
1
  module Libsvm
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
data/rb-libsvm.gemspec CHANGED
@@ -10,6 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.homepage = "https://github.com/febeling/rb-libsvm"
11
11
  s.summary = %q{Ruby language bindings for LIBSVM}
12
12
  s.description = %q{libsvm and ruby without using swig}
13
+ s.required_ruby_version = '>= 1.8.7'
13
14
 
14
15
  s.rubyforge_project = "rb-libsvm"
15
16
 
@@ -18,7 +19,6 @@ Gem::Specification.new do |s|
18
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
20
  s.require_paths = ["lib"]
20
21
 
21
- # specify any dependencies here; for example:
22
22
  s.add_development_dependency('rake-compiler')
23
23
  s.add_development_dependency('rspec', '>= 2.7.0')
24
24
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-libsvm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - C. Florian Ebeling
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-20 00:00:00.000000000 Z
12
+ date: 2014-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
88
+ version: 1.8.7
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
@@ -104,3 +104,4 @@ test_files:
104
104
  - spec/problem_spec.rb
105
105
  - spec/spec_helper.rb
106
106
  - spec/usage_spec.rb
107
+ has_rdoc: