hexgnu-libsvm-ruby-swig 0.1.0

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.
data/ext/svm.h ADDED
@@ -0,0 +1,76 @@
1
+ #ifndef _LIBSVM_H
2
+ #define _LIBSVM_H
3
+
4
+ #define LIBSVM_VERSION 290
5
+
6
+ #ifdef __cplusplus
7
+ extern "C" {
8
+ #endif
9
+
10
+ extern int libsvm_version;
11
+
12
+ struct svm_node
13
+ {
14
+ int index;
15
+ double value;
16
+ };
17
+
18
+ struct svm_problem
19
+ {
20
+ int l;
21
+ double *y;
22
+ struct svm_node **x;
23
+ };
24
+
25
+ enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
26
+ enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */
27
+
28
+ struct svm_parameter
29
+ {
30
+ int svm_type;
31
+ int kernel_type;
32
+ int degree; /* for poly */
33
+ double gamma; /* for poly/rbf/sigmoid */
34
+ double coef0; /* for poly/sigmoid */
35
+
36
+ /* these are for training only */
37
+ double cache_size; /* in MB */
38
+ double eps; /* stopping criteria */
39
+ double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
40
+ int nr_weight; /* for C_SVC */
41
+ int *weight_label; /* for C_SVC */
42
+ double* weight; /* for C_SVC */
43
+ double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
44
+ double p; /* for EPSILON_SVR */
45
+ int shrinking; /* use the shrinking heuristics */
46
+ int probability; /* do probability estimates */
47
+ };
48
+
49
+ struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param);
50
+ void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target);
51
+
52
+ int svm_save_model(const char *model_file_name, const struct svm_model *model);
53
+ struct svm_model *svm_load_model(const char *model_file_name);
54
+
55
+ int svm_get_svm_type(const struct svm_model *model);
56
+ int svm_get_nr_class(const struct svm_model *model);
57
+ void svm_get_labels(const struct svm_model *model, int *label);
58
+ double svm_get_svr_probability(const struct svm_model *model);
59
+
60
+ void svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values);
61
+ double svm_predict(const struct svm_model *model, const struct svm_node *x);
62
+ double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates);
63
+
64
+ void svm_destroy_model(struct svm_model *model);
65
+ void svm_destroy_param(struct svm_parameter *param);
66
+
67
+ const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
68
+ int svm_check_probability_model(const struct svm_model *model);
69
+
70
+ extern void (*svm_print_string) (const char *);
71
+
72
+ #ifdef __cplusplus
73
+ }
74
+ #endif
75
+
76
+ #endif /* _LIBSVM_H */
data/lib/svm.rb ADDED
@@ -0,0 +1,124 @@
1
+ require File.join(File.dirname(__FILE__), 'libsvm')
2
+ include Libsvm
3
+
4
+ %w[./libsvm/problem ./libsvm/model ./libsvm/parameter].each do |r|
5
+ require File.join(File.dirname(__FILE__), r)
6
+ end
7
+
8
+ # TODO: these are super weird should be inside of a module; extend self; end...
9
+
10
+
11
+ def _int_array(seq)
12
+ size = seq.size
13
+ array = new_int(size)
14
+ i = 0
15
+ for item in seq
16
+ int_setitem(array,i,item)
17
+ i = i + 1
18
+ end
19
+ return array
20
+ end
21
+
22
+ def _double_array(seq)
23
+ size = seq.size
24
+ array = new_double(size)
25
+ i = 0
26
+ for item in seq
27
+ double_setitem(array,i,item)
28
+ i = i + 1
29
+ end
30
+ return array
31
+ end
32
+
33
+ def _free_int_array(x)
34
+ if !x.nil? and !x.empty?
35
+ delete_int(x)
36
+ end
37
+ end
38
+
39
+ def _free_double_array(x)
40
+ if !x.nil? and !x.empty?
41
+ delete_double(x)
42
+ end
43
+ end
44
+
45
+ def _int_array_to_list(x,n)
46
+ list = []
47
+ (0..n-1).each {|i| list << int_getitem(x,i) }
48
+ return list
49
+ end
50
+
51
+ def _double_array_to_list(x,n)
52
+ list = []
53
+ (0..n-1).each {|i| list << double_getitem(x,i) }
54
+ return list
55
+ end
56
+
57
+ def _convert_to_svm_node_array(x)
58
+ # Make index array
59
+ iter_range = x.each_index.to_a
60
+
61
+ data = svm_node_array(iter_range.length + 1)
62
+ svm_node_array_set(data, iter_range.length, -1, 0)
63
+
64
+ iter_range.each do |k|
65
+ svm_node_array_set(data, k, k, x[k])
66
+ end
67
+
68
+ data
69
+ end
70
+
71
+ module SVM
72
+ extend self
73
+
74
+ def convert_to_svm_node_array(x, max = 30)
75
+ # X is an array of indicies
76
+ # Make index array
77
+ # iter_range = x.sort
78
+
79
+ data = svm_node_array(max + 1)
80
+ svm_node_array_set(data, max, -1, 0)
81
+
82
+ x.sort.each do |k|
83
+ svm_node_array_set(data, k, k, 1)
84
+ end
85
+
86
+ data
87
+ end
88
+ end
89
+
90
+
91
+ def cross_validation(prob, param, fold)
92
+ if param.gamma == 0
93
+ param.gamma = 1.0/prob.maxlen
94
+ end
95
+ dblarr = new_double(prob.size)
96
+ svm_cross_validation(prob.prob, param.param, fold, dblarr)
97
+ ret = _double_array_to_list(dblarr, prob.size)
98
+ delete_double(dblarr)
99
+ return ret
100
+ end
101
+
102
+ def read_file filename
103
+ labels = []
104
+ samples = []
105
+ max_index = 0
106
+
107
+ f = File.open(filename)
108
+ f.each do |line|
109
+ elems = line.split
110
+ sample = {}
111
+ for e in elems[1..-1]
112
+ points = e.split(":")
113
+ sample[points[0].to_i] = points[1].to_f
114
+ if points[0].to_i < max_index
115
+ max_index = points[0].to_i
116
+ end
117
+ end
118
+ labels << elems[0].to_i
119
+ samples << sample
120
+ end
121
+ puts "#{filename}: #{samples.size} samples loaded."
122
+ return labels,samples
123
+ end
124
+
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hexgnu-libsvm-ruby-swig
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Zeng
14
+ - Matthew Kirk
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-04-17 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: hoe
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 49
31
+ segments:
32
+ - 1
33
+ - 8
34
+ - 3
35
+ version: 1.8.3
36
+ type: :development
37
+ version_requirements: *id001
38
+ description: Ruby wrapper of LIBSVM using SWIG
39
+ email: matt@matthewkirk.com
40
+ executables: []
41
+
42
+ extensions:
43
+ - ext/extconf.rb
44
+ extra_rdoc_files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - README.rdoc
48
+ files:
49
+ - History.txt
50
+ - COPYING
51
+ - AUTHORS
52
+ - Manifest.txt
53
+ - README.rdoc
54
+ - Rakefile
55
+ - lib/svm.rb
56
+ - ext/libsvm_wrap.cxx
57
+ - ext/svm.cpp
58
+ - ext/svm.h
59
+ - ext/extconf.rb
60
+ has_rdoc: false
61
+ homepage: http://www.matthewkirk.com
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ require_paths:
69
+ - lib
70
+ - ext
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.5.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: Ruby wrapper fork of Tom Zeng's libsvm wrapper
96
+ test_files: []
97
+