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 CHANGED
@@ -71,16 +71,25 @@ end
71
71
  module SVM
72
72
  extend self
73
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)
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 = _convert_to_svm_node_array(x)
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 = _convert_to_svm_node_array(x)
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)
@@ -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 = 0
19
+ @maxlen = max
18
20
  x.each_with_index do |row, i|
19
- #data = _convert_to_svm_node_array(row)
20
- data = SVM.convert_to_svm_node_array(row)
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: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Zeng