hexgnu-libsvm-ruby-swig 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hex-svm.rb +19 -10
- data/lib/libsvm/model.rb +5 -6
- data/lib/libsvm/problem.rb +5 -5
- metadata +3 -3
data/lib/hex-svm.rb
CHANGED
@@ -71,16 +71,25 @@ end
|
|
71
71
|
module SVM
|
72
72
|
extend self
|
73
73
|
|
74
|
-
def convert_to_svm_node_array(
|
75
|
-
#
|
76
|
-
#
|
77
|
-
# iter_range = x.
|
78
|
-
|
79
|
-
data = svm_node_array(
|
80
|
-
svm_node_array_set(data,
|
81
|
-
|
82
|
-
|
83
|
-
|
74
|
+
def convert_to_svm_node_array(indicies, max)
|
75
|
+
# Make index array
|
76
|
+
# x = indexes_to_array(indicies, max)
|
77
|
+
# iter_range = x.each_index.to_a
|
78
|
+
|
79
|
+
data = svm_node_array(indicies.length + 1)
|
80
|
+
svm_node_array_set(data, indicies.length, -1, 0)
|
81
|
+
|
82
|
+
# max.times do |i|
|
83
|
+
# # Set to zero if not in indicies
|
84
|
+
# if indicies.include?(i)
|
85
|
+
# svm_node_array_set(data, i, i, 1)
|
86
|
+
# else
|
87
|
+
# svm_node_array_set(data, i, i, 0)
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
|
91
|
+
indicies.sort.each_with_index do |idx, i|
|
92
|
+
svm_node_array_set(data, i, idx, 1)
|
84
93
|
end
|
85
94
|
|
86
95
|
data
|
data/lib/libsvm/model.rb
CHANGED
@@ -16,7 +16,7 @@ module SVM
|
|
16
16
|
end
|
17
17
|
msg = svm_check_parameter(prob.prob,param.param)
|
18
18
|
raise ::ArgumentError, msg if msg
|
19
|
-
@model = svm_train(prob.prob,param.param)
|
19
|
+
@model = svm_train(prob.prob, param.param)
|
20
20
|
end
|
21
21
|
|
22
22
|
#setup some classwide variables
|
@@ -29,11 +29,10 @@ module SVM
|
|
29
29
|
delete_int(intarr)
|
30
30
|
#check if valid probability model
|
31
31
|
@probability = svm_check_probability_model(@model)
|
32
|
-
|
33
32
|
end
|
34
33
|
|
35
|
-
def predict(x)
|
36
|
-
data = SVM.convert_to_svm_node_array(x)
|
34
|
+
def predict(x, max = x.max)
|
35
|
+
data = SVM.convert_to_svm_node_array(x, max)
|
37
36
|
ret = svm_predict(@model,data)
|
38
37
|
svm_node_array_destroy(data)
|
39
38
|
return ret
|
@@ -54,7 +53,7 @@ module SVM
|
|
54
53
|
def predict_values_raw(x)
|
55
54
|
#convert x into svm_node, allocate a double array for return
|
56
55
|
n = (@nr_class*(@nr_class-1)/2).floor
|
57
|
-
data =
|
56
|
+
data = SVM.convert_to_svm_node_array(x)
|
58
57
|
dblarr = new_double(n)
|
59
58
|
svm_predict_values(@model, data, dblarr)
|
60
59
|
ret = _double_array_to_list(dblarr, n)
|
@@ -101,7 +100,7 @@ module SVM
|
|
101
100
|
end
|
102
101
|
|
103
102
|
#convert x into svm_node, alloc a double array to receive probabilities
|
104
|
-
data =
|
103
|
+
data = SVM.convert_to_svm_node_array(x)
|
105
104
|
dblarr = new_double(@nr_class)
|
106
105
|
pred = svm_predict_probability(@model, data, dblarr)
|
107
106
|
pv = _double_array_to_list(dblarr, @nr_class)
|
data/lib/libsvm/problem.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
module SVM
|
2
2
|
class Problem
|
3
3
|
attr_accessor :prob, :maxlen, :size
|
4
|
+
attr_reader :max
|
4
5
|
|
5
6
|
def initialize(y, *x)
|
6
7
|
#assert y.size == x.size
|
8
|
+
@max = x.flatten.max #aggh hate doing this...
|
7
9
|
@prob = prob = Svm_problem.new
|
8
10
|
@size = size = y.size
|
9
11
|
|
@@ -14,12 +16,10 @@ module SVM
|
|
14
16
|
|
15
17
|
@x_matrix = x_matrix = svm_node_matrix(size)
|
16
18
|
@data = []
|
17
|
-
@maxlen =
|
19
|
+
@maxlen = max
|
18
20
|
x.each_with_index do |row, i|
|
19
|
-
|
20
|
-
|
21
|
-
@data << data
|
22
|
-
svm_node_matrix_set(x_matrix, i, data)
|
21
|
+
@data << SVM.convert_to_svm_node_array(row, max)
|
22
|
+
svm_node_matrix_set(x_matrix, i, @data.last)
|
23
23
|
@maxlen = [@maxlen, row.size].max
|
24
24
|
end
|
25
25
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hexgnu-libsvm-ruby-swig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tom Zeng
|