liblinear-ruby 0.0.5 → 0.0.6

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: ac700b40f9f8bc42fbb319f6d510b150740556e7
4
- data.tar.gz: 1027bf69f4ab2a882c12b0d569ce952a2729c350
3
+ metadata.gz: 7da30519f73e2c8623e55d79bbceba8338cf3f14
4
+ data.tar.gz: 765eda167d95dc14cbeb598d1224fcca8be9b2bb
5
5
  SHA512:
6
- metadata.gz: 4ea3e62ea0b3977d0a49f5ed700f7e40079c99b144eda439a384cb04de644d21355db4e032f150430c3abc9df4cfea1fdd3828409f119a636dd01fbcc63b254e
7
- data.tar.gz: 610cf9eab7d391d5c4ee1767e28accc957c158402a15d5bd637c55eaa1bf960218f5d52581752499ffbb77dab3bd5e65a0e8d14a12ee9772047d14ac76077269
6
+ metadata.gz: ca676587ed932b52aceac12795055459c2260596c63f1b417e89adb3f0deba84c0222a0f4b2d418f6145d613b5df8fc196820e6e3a2016ac549d3f6a8b20d595
7
+ data.tar.gz: 1e05e2eeccc9405fba3b6b040421156b139e77d39914998d3ca3fa441c0806437304eaf753cc9244510de12f36897f6c5b2b48ffa08a54bd3bd75b0887e2b83f
data/README.md CHANGED
@@ -43,6 +43,15 @@ puts model.predict({1=>1, 2=>1, 3=>1, 4=>1, 5=>1}) # => -1.0
43
43
  # Analyzing phase
44
44
  puts model.coefficient
45
45
  puts model.bias
46
+
47
+ # Cross Validation
48
+ fold = 2
49
+ cv = Liblinear::CrossValidator.new(prob, param, fold)
50
+ cv.execute
51
+
52
+ puts cv.accuracy # for classification
53
+ puts cv.mean_squared_error # for regression
54
+ puts cv.squared_correlation_coefficient # for regression
46
55
  ```
47
56
  ## Usage
48
57
 
data/lib/liblinear.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $: << File.expand_path(File.join(__FILE__, '..', '..', 'ext'))
2
2
 
3
3
  require 'liblinearswig'
4
+ require 'liblinear/cross_validator'
4
5
  require 'liblinear/error'
5
6
  require 'liblinear/model'
6
7
  require 'liblinear/parameter'
@@ -0,0 +1,58 @@
1
+ module Liblinear
2
+ class CrossValidator
3
+ include Liblinear
4
+ include Liblinearswig
5
+
6
+ # @param prob [LibLinear::Problem]
7
+ # @param param [Liblinear::Parameter]
8
+ # @param fold [Integer]
9
+ def initialize(prob, param, fold)
10
+ @prob = prob
11
+ @param = param
12
+ @fold = fold
13
+ end
14
+
15
+ # @return [Array <Integer, Double>]
16
+ def execute
17
+ target = new_double_array(@prob.labels.size.times.map { 0.0 })
18
+ cross_validation(@prob.prob, @param.param, @fold, target)
19
+ @predictions = double_array_c_to_ruby(target, @prob.labels.size)
20
+ end
21
+
22
+ # @return [Double]
23
+ def accuracy
24
+ total_correct = 0
25
+ @prob.labels.size.times do |i|
26
+ total_correct += 1 if @predictions[i] == @prob.labels[i].to_f
27
+ end
28
+ total_correct.to_f / @prob.labels.size.to_f
29
+ end
30
+
31
+ # @return [Double]
32
+ def mean_squared_error
33
+ total_error = 0.0
34
+ @prob.labels.size.times do |i|
35
+ total_error += (@prob.labels[i].to_f - @predictions[i].to_f) ** 2
36
+ end
37
+ total_error / @prob.labels.size.to_f
38
+ end
39
+
40
+ # @return [Double]
41
+ def squared_correlation_coefficient
42
+ sum_x = 0.0
43
+ sum_y = 0.0
44
+ sum_xx = 0.0
45
+ sum_yy = 0.0
46
+ sum_xy = 0.0
47
+ @prob.labels.size.times do |i|
48
+ sum_x += @predictions[i].to_f
49
+ sum_y += @prob.labels[i].to_f
50
+ sum_xx += @predictions[i].to_f ** 2
51
+ sum_yy += @prob.labels[i].to_f ** 2
52
+ sum_xy += @predictions[i].to_f * @prob.labels[i].to_f
53
+ end
54
+ ((@prob.labels.size * sum_xy - sum_x * sum_y) ** 2) /
55
+ ((@prob.labels.size * sum_xx - sum_x ** 2) * (@prob.labels.size * sum_yy - sum_y ** 2))
56
+ end
57
+ end
58
+ end
@@ -13,9 +13,9 @@ module Liblinear
13
13
  unless arg_1.is_a?(Liblinear::Problem) && arg_2.is_a?(Liblinear::Parameter)
14
14
  raise ArgumentError, 'arguments must be [Liblinear::Problem] and [Liblinear::Parameter]'
15
15
  end
16
- error_msg = check_parameter(arg_1.prob, arg_2.params)
16
+ error_msg = check_parameter(arg_1.prob, arg_2.param)
17
17
  raise InvalidParameter, error_msg if error_msg
18
- @model = train(arg_1.prob, arg_2.params)
18
+ @model = train(arg_1.prob, arg_2.param)
19
19
  else
20
20
  raise ArgumentError, 'argument must be [String]' unless arg_1.is_a?(String)
21
21
  @model = load_model(arg_1)
@@ -2,11 +2,11 @@ module Liblinear
2
2
  class Parameter
3
3
  include Liblinear
4
4
  include Liblinearswig
5
- attr_accessor :params
5
+ attr_accessor :param
6
6
 
7
- # @param params [Hash]
8
- def initialize(params = {})
9
- @params = Liblinearswig::Parameter.new
7
+ # @param param [Hash]
8
+ def initialize(param = {})
9
+ @param = Liblinearswig::Parameter.new
10
10
  self.solver_type = 1
11
11
  self.C = 1
12
12
  self.eps = 0.1
@@ -14,28 +14,28 @@ module Liblinear
14
14
  self.nr_weight = 0
15
15
  self.weight_label = []
16
16
  self.weight = []
17
- params.each do |k, v|
17
+ param.each do |k, v|
18
18
  self.send("#{k}=", v)
19
19
  end
20
20
  end
21
21
 
22
- # @params weigt_label [Array <Integer>]
22
+ # @param weigt_label [Array <Integer>]
23
23
  def weight_label=(weight_label)
24
- free_int_array(@params.weight_label)
25
- @params.weight_label = new_int_array(weight_label)
24
+ free_int_array(@param.weight_label)
25
+ @param.weight_label = new_int_array(weight_label)
26
26
  end
27
27
 
28
- # @params weight [Array <Double>]
28
+ # @param weight [Array <Double>]
29
29
  def weight=(weight)
30
- free_double_array(@params.weight)
31
- @params.weight = new_double_array(weight)
30
+ free_double_array(@param.weight)
31
+ @param.weight = new_double_array(weight)
32
32
  end
33
33
 
34
34
  def method_missing(m, *args)
35
35
  if m.to_s.index('=')
36
- @params.send(m.to_sym, args.first)
36
+ @param.send(m.to_sym, args.first)
37
37
  else
38
- @params.send(m.to_sym)
38
+ @param.send(m.to_sym)
39
39
  end
40
40
  end
41
41
  end
@@ -3,6 +3,7 @@ module Liblinear
3
3
  include Liblinear
4
4
  include Liblinearswig
5
5
  attr_accessor :prob
6
+ attr_reader :labels, :examples
6
7
 
7
8
  # @param labels [Array <Double>]
8
9
  # @param examples [Array <Double, Hash>]
@@ -13,7 +14,8 @@ module Liblinear
13
14
  raise ArgumentError, 'labels and examples must be same size'
14
15
  end
15
16
  @prob = Liblinearswig::Problem.new
16
- @c_label = new_double_array(labels)
17
+ @labels = labels
18
+ c_labels = new_double_array(@labels)
17
19
  @examples = examples
18
20
  @bias = bias
19
21
  @max_example_index = max_index(@examples)
@@ -23,7 +25,7 @@ module Liblinear
23
25
  set_example_matrix
24
26
 
25
27
  @prob.tap do |p|
26
- p.y = @c_label
28
+ p.y = c_labels
27
29
  p.x = @example_matrix
28
30
  p.bias = bias
29
31
  p.l = examples.size
@@ -1,3 +1,3 @@
1
1
  module Liblinear
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -0,0 +1,33 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'liblinear'
3
+
4
+ describe Liblinear::CrossValidator do
5
+ before do
6
+ @prob = Liblinear::Problem.new([1, 2], [[1],[2]])
7
+ @param_1 = Liblinear::Parameter.new
8
+ @param_2 = Liblinear::Parameter.new({ solver_type: Liblinear::L2R_L2LOSS_SVR })
9
+
10
+ @cv_classification = Liblinear::CrossValidator.new(@prob, @param_1, 2)
11
+ @cv_classification.execute
12
+ @cv_regression = Liblinear::CrossValidator.new(@prob, @param_2, 2)
13
+ @cv_regression.execute
14
+ end
15
+
16
+ describe '#accuracy' do
17
+ it 'returns accuracy' do
18
+ expect(@cv_classification.accuracy.class).to eq(Float)
19
+ end
20
+ end
21
+
22
+ describe '#mean_squared_error' do
23
+ it 'returns mean_squared_error' do
24
+ expect(@cv_regression.mean_squared_error.class).to eq(Float)
25
+ end
26
+ end
27
+
28
+ describe 'squared_correlation_coefficient' do
29
+ it 'returns squared_correlation_coefficient' do
30
+ expect(@cv_regression.squared_correlation_coefficient.class).to eq(Float)
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liblinear-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kei Tsuchiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2014-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - ext/tron.cpp
66
66
  - ext/tron.h
67
67
  - lib/liblinear.rb
68
+ - lib/liblinear/cross_validator.rb
68
69
  - lib/liblinear/error.rb
69
70
  - lib/liblinear/model.rb
70
71
  - lib/liblinear/parameter.rb
@@ -110,6 +111,7 @@ files:
110
111
  - liblinear-1.95/windows/train.exe
111
112
  - liblinear-1.95/windows/train.mexw64
112
113
  - liblinear-ruby.gemspec
114
+ - spec/liblinear/cross_validator_spec.rb
113
115
  - spec/liblinear/model_spec.rb
114
116
  - spec/liblinear/parameter_spec.rb
115
117
  - spec/liblinear/problem_spec.rb
@@ -139,6 +141,7 @@ signing_key:
139
141
  specification_version: 4
140
142
  summary: Ruby wrapper of LIBLINEAR using SWIG
141
143
  test_files:
144
+ - spec/liblinear/cross_validator_spec.rb
142
145
  - spec/liblinear/model_spec.rb
143
146
  - spec/liblinear/parameter_spec.rb
144
147
  - spec/liblinear/problem_spec.rb