confidence_weighted 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +27 -0
- data/README.md +11 -0
- data/Rakefile +6 -0
- data/bin/confidence_weighted +36 -0
- data/confidence_weighted.gemspec +24 -0
- data/lib/confidence_weighted/soft_confidence_weighted.rb +121 -0
- data/lib/confidence_weighted/version.rb +3 -0
- data/lib/confidence_weighted.rb +6 -0
- data/sample/colors.json +13 -0
- data/spec/confidence_weighted/soft_confidence_weighted_spec.rb +26 -0
- data/spec/confidence_weighted_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9cac63c645f3a3b60ac078ef0e583a723d87e6d3
|
4
|
+
data.tar.gz: d0ba9e844ea7618dc59c6d37dd8101c75bfad70f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 972f47bbd93bc4ae4cf9904114eac4c125966bbff462503a10e9c93ab8518edf546f54fdd5f04767a00bdeea4f3f7b3a2a2c15b3399123c7a6eeb71057f9023c
|
7
|
+
data.tar.gz: 7d949ca0f86c6ded764f721eff7925c4b4f7e95922f08fd3cace9708f4ba84fc56e0204aa258cd4d3cbbd5617c6b2ff73aff6e82f15f86cc290e958a6fe9aed0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2013, Maezawa Toshiyuki, HARUYAMA Seigo
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
Redistributions in binary form must reproduce the above copyright notice, this
|
11
|
+
list of conditions and the following disclaimer in the documentation and/or
|
12
|
+
other materials provided with the distribution.
|
13
|
+
|
14
|
+
Neither the name of the {organization} nor the names of its
|
15
|
+
contributors may be used to endorse or promote products derived from
|
16
|
+
this software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
19
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
22
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
25
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# ConfidenceWeighted
|
2
|
+
|
3
|
+
Ruby porting of https://github.com/echizentm/ConfidenceWeighted/ (python version)
|
4
|
+
|
5
|
+
Only Soft Confidence Weighted implemented.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
$ bundle install --path .bundle
|
10
|
+
$ bundle exec confidence_weighted -h
|
11
|
+
$ bundle exec confidence_weighted < sample/colors.json
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'confidence_weighted'
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = 'Usage: confidence_weighted [options]'
|
11
|
+
|
12
|
+
opts.on('-c', '--confidence [CONFIDENCE]', 'confidence parameter') do |confidence|
|
13
|
+
options[:confidence] = confidence.to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('-a', '--aggressiveness [AGGRESSIVENESS]', 'aggressiveness parameter') do |aggressiveness|
|
17
|
+
options[:aggressiveness] = aggressiveness.to_f
|
18
|
+
end
|
19
|
+
end.parse!
|
20
|
+
|
21
|
+
scw = ConfidenceWeighted::SoftConfidenceWeighted.new(options)
|
22
|
+
ARGF.each do |line|
|
23
|
+
line.chomp!
|
24
|
+
break if line == ''
|
25
|
+
obj = JSON.parse(line)
|
26
|
+
|
27
|
+
print "[USAGE] [label, {feature:weight,...}]\n" unless obj.size == 2
|
28
|
+
|
29
|
+
if obj[0] == 0
|
30
|
+
obj[0] = scw.classify(obj[1])
|
31
|
+
print 'classify: ' + obj.to_json + "\n"
|
32
|
+
else
|
33
|
+
scw.update(obj[1], obj[0])
|
34
|
+
print 'update: ' + obj.to_json + "\n"
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'confidence_weighted/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'confidence_weighted'
|
8
|
+
spec.version = ConfidenceWeighted::VERSION
|
9
|
+
spec.authors = ['Maezawa Toshiyuki' 'HARUYAMA Seigo']
|
10
|
+
spec.email = ['echizentm@gmail.com', 'haruyama@unixuser.org']
|
11
|
+
spec.description = %q{Confidence Weighted Classifier}
|
12
|
+
spec.summary = %q{Confidence Weighted Classifier}
|
13
|
+
spec.homepage = 'https://github.com/echizentm/ConfidenceWeighted'
|
14
|
+
spec.license = 'BSD (3-clause)'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module ConfidenceWeighted
|
2
|
+
# Soft Confidence Weighted classifier
|
3
|
+
class SoftConfidenceWeighted
|
4
|
+
|
5
|
+
MIN_CONFIDENCE = 0.0
|
6
|
+
MAX_CONFIDENCE = 1.0
|
7
|
+
DEFAULT_CONFIDENCE = 0.7
|
8
|
+
MIN_AGGRESSIVENESS = 0.0
|
9
|
+
DEFAULT_AGGRESSIVENESS = 1.0
|
10
|
+
VALID_LABEL = [1, -1]
|
11
|
+
ERF_ORDER = 30
|
12
|
+
|
13
|
+
def initialize(confidence: DEFAULT_CONFIDENCE, aggressiveness: DEFAULT_AGGRESSIVENESS)
|
14
|
+
|
15
|
+
if confidence < MIN_CONFIDENCE
|
16
|
+
confidence = MIN_CONFIDENCE
|
17
|
+
elsif confidence > MAX_CONFIDENCE
|
18
|
+
confidence = MAX_CONFIDENCE
|
19
|
+
end
|
20
|
+
|
21
|
+
if aggressiveness < MIN_AGGRESSIVENESS
|
22
|
+
aggressiveness = MIN_AGGRESSIVENESS
|
23
|
+
end
|
24
|
+
|
25
|
+
@aggressiveness = aggressiveness
|
26
|
+
@phi = probit(confidence)
|
27
|
+
@psi = 1.0 + @phi * @phi / 2.0
|
28
|
+
@zeta = 1.0 + @phi * @phi
|
29
|
+
|
30
|
+
@mu = Hash.new(0.0)
|
31
|
+
@sigma = Hash.new(1.0)
|
32
|
+
end
|
33
|
+
|
34
|
+
def classify(data)
|
35
|
+
margin = 0.0
|
36
|
+
data.each do |feature, weight|
|
37
|
+
margin += @mu[feature] * weight if @mu.key?(feature)
|
38
|
+
end
|
39
|
+
return 1 if margin > 0
|
40
|
+
-1
|
41
|
+
end
|
42
|
+
|
43
|
+
def update(data, label)
|
44
|
+
return false unless VALID_LABEL.include?(label)
|
45
|
+
|
46
|
+
sigma_x = get_sigma_x(data)
|
47
|
+
margin_mean, variance = get_margin_mean_and_variance(label, data, sigma_x)
|
48
|
+
|
49
|
+
return true if @phi * Math.sqrt(variance) <= margin_mean
|
50
|
+
|
51
|
+
alpha, beta = get_alpha_and_beta(margin_mean, variance)
|
52
|
+
return true if alpha == 0.0 || beta == 0.0
|
53
|
+
|
54
|
+
sigma_x.each do |feature, weight|
|
55
|
+
@mu[feature] += alpha * label * weight
|
56
|
+
@sigma[feature] -= beta * weight * weight
|
57
|
+
end
|
58
|
+
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def get_sigma_x(data)
|
65
|
+
sigma_x = Hash.new(1.0)
|
66
|
+
data.each do |feature, weight|
|
67
|
+
sigma_x[feature] *= @sigma[feature] * weight
|
68
|
+
end
|
69
|
+
sigma_x
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_margin_mean_and_variance(label, data, sigma_x)
|
73
|
+
margin_mean = 0.0
|
74
|
+
variance = 0.0
|
75
|
+
data.each do |feature, weight|
|
76
|
+
margin_mean += @mu[feature] * weight
|
77
|
+
variance += sigma_x[feature] * weight
|
78
|
+
end
|
79
|
+
[margin_mean * label, variance]
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_alpha_and_beta(margin_mean, variance)
|
83
|
+
alpha_den = variance * @zeta
|
84
|
+
return [0.0, 0.0] if alpha_den == 0.0
|
85
|
+
|
86
|
+
term1 = margin_mean * @phi / 2.0
|
87
|
+
alpha = (-1.0 * margin_mean * @psi + @phi * Math.sqrt(term1 * term1 + alpha_den)) / alpha_den
|
88
|
+
return [0.0, 0.0] if alpha <= 0.0
|
89
|
+
|
90
|
+
alpha = @aggressiveness if alpha >= @aggressiveness
|
91
|
+
|
92
|
+
beta_num = alpha * @phi
|
93
|
+
term2 = variance * beta_num
|
94
|
+
beta_den = term2 + ( -1.0 * term2 + Math.sqrt(term2 * term2 + 4.0 * variance)) / 2.0
|
95
|
+
return [0.0, 0.0] if beta_den == 0.0
|
96
|
+
|
97
|
+
[alpha, beta_num / beta_den]
|
98
|
+
end
|
99
|
+
|
100
|
+
def probit(p)
|
101
|
+
Math.sqrt(2.0) * erf_inv(2.0 * p - 1.0)
|
102
|
+
end
|
103
|
+
|
104
|
+
def erf_inv(z)
|
105
|
+
value = 1.0
|
106
|
+
term = 1.0
|
107
|
+
c_memo = [1.0]
|
108
|
+
|
109
|
+
(1.. ERF_ORDER).each do |n|
|
110
|
+
term *= Math::PI * z * z / 4.0
|
111
|
+
c = 0.0
|
112
|
+
(0...n).each do |m|
|
113
|
+
c += c_memo[m] * c_memo[n - 1 - m] / (m + 1.0) / (2.0 * m + 1.0)
|
114
|
+
end
|
115
|
+
c_memo << c
|
116
|
+
value += c * term / (2.0 * n + 1.0)
|
117
|
+
end
|
118
|
+
Math.sqrt(Math::PI) * z * value / 2.0
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/sample/colors.json
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
[1,{"R":255,"G":0,"B":0}]
|
2
|
+
[-1,{"R":0,"G":255,"B":0}]
|
3
|
+
[-1,{"R":0,"G":0,"B":255}]
|
4
|
+
[-1,{"R":0,"G":255,"B":255}]
|
5
|
+
[1,{"R":255,"G":0,"B":255}]
|
6
|
+
[1,{"R":255,"G":255,"B":0}]
|
7
|
+
[0,{"R":255,"G":0,"B":0}]
|
8
|
+
[0,{"R":0,"G":255,"B":0}]
|
9
|
+
[0,{"R":0,"G":0,"B":255}]
|
10
|
+
[0,{"R":0,"G":255,"B":255}]
|
11
|
+
[0,{"R":255,"G":0,"B":255}]
|
12
|
+
[0,{"R":255,"G":255,"B":0}]
|
13
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfidenceWeighted::SoftConfidenceWeighted do
|
4
|
+
it 'should successfully initialize' do
|
5
|
+
ConfidenceWeighted::SoftConfidenceWeighted.new(confidence: 1.0, aggressiveness: 1.0)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should classify' do
|
9
|
+
scw = ConfidenceWeighted::SoftConfidenceWeighted.new
|
10
|
+
expect(scw.classify({})).to eq(-1)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should not update with invalid label' do
|
14
|
+
scw = ConfidenceWeighted::SoftConfidenceWeighted.new
|
15
|
+
expect(scw.update({}, -2)).to eq(false)
|
16
|
+
expect(scw.update({}, 0)).to eq(false)
|
17
|
+
expect(scw.update({}, 2)).to eq(false)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should successfully update' do
|
21
|
+
scw = ConfidenceWeighted::SoftConfidenceWeighted.new
|
22
|
+
expect(scw.update({}, -1)).to eq(true)
|
23
|
+
expect(scw.update({}, 1)).to eq(true)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: confidence_weighted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maezawa ToshiyukiHARUYAMA Seigo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Confidence Weighted Classifier
|
56
|
+
email:
|
57
|
+
- echizentm@gmail.com
|
58
|
+
- haruyama@unixuser.org
|
59
|
+
executables:
|
60
|
+
- confidence_weighted
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- .rspec
|
66
|
+
- .travis.yml
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/confidence_weighted
|
72
|
+
- confidence_weighted.gemspec
|
73
|
+
- lib/confidence_weighted.rb
|
74
|
+
- lib/confidence_weighted/soft_confidence_weighted.rb
|
75
|
+
- lib/confidence_weighted/version.rb
|
76
|
+
- sample/colors.json
|
77
|
+
- spec/confidence_weighted/soft_confidence_weighted_spec.rb
|
78
|
+
- spec/confidence_weighted_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: https://github.com/echizentm/ConfidenceWeighted
|
81
|
+
licenses:
|
82
|
+
- BSD (3-clause)
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.1.11
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Confidence Weighted Classifier
|
104
|
+
test_files:
|
105
|
+
- spec/confidence_weighted/soft_confidence_weighted_spec.rb
|
106
|
+
- spec/confidence_weighted_spec.rb
|
107
|
+
- spec/spec_helper.rb
|