rumale-svm 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/CHANGELOG.md +3 -0
- data/README.md +0 -6
- data/lib/rumale/svm.rb +1 -0
- data/lib/rumale/svm/linear_one_class_svm.rb +121 -0
- data/lib/rumale/svm/version.rb +1 -1
- data/rumale-svm.gemspec +1 -1
- metadata +5 -7
- data/.github/workflows/build.yml +0 -24
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb3d059168c39ad2dd1dc2052528b060f0b469165f3d5e60db3a6959a1994637
|
4
|
+
data.tar.gz: 3a7fa1c8d7445573308413e195426b294ad01c97fec59c345b46bf74b5ff8096
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88191bf03ef5ade4eb9d6d927ccbf53a2f0e74789aebbe9616dea472548a28e8e46a0fb6e0a0903c67e84f919e22903830c39cbe8f2a761a183b2a429836ddda
|
7
|
+
data.tar.gz: a6897776e03bb756f40138984537353ac42643006892fa4ef61d89950478f074f530d0dc57192185f4f72d30768b83619d9cd813a3dab3bdecab5e6034eddb9f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -77,12 +77,6 @@ $ ruby rumale_svm_test.rb
|
|
77
77
|
Accuracy: 0.835
|
78
78
|
```
|
79
79
|
|
80
|
-
## Development
|
81
|
-
|
82
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
-
|
84
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
-
|
86
80
|
## Contributing
|
87
81
|
|
88
82
|
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/rumale-svm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/rumale/svm.rb
CHANGED
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'numo/liblinear'
|
4
|
+
require 'rumale/base/base_estimator'
|
5
|
+
require 'rumale/validation'
|
6
|
+
|
7
|
+
module Rumale
|
8
|
+
module SVM
|
9
|
+
# LinearOneClassSVM is a class that provides linear One-class Support Vector Machine in LIBLINEAR with Rumale interface.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# estimator = Rumale::SVM::LinearOneClassSVM.new(nu: 0.05, random_seed: 1)
|
13
|
+
# estimator.fit(training_samples, traininig_labels)
|
14
|
+
# results = estimator.predict(testing_samples)
|
15
|
+
class LinearOneClassSVM
|
16
|
+
include Base::BaseEstimator
|
17
|
+
include Validation
|
18
|
+
|
19
|
+
# Return the weight vector for LinearOneClassSVM.
|
20
|
+
# @return [Numo::DFloat] (shape: [n_features])
|
21
|
+
attr_reader :weight_vec
|
22
|
+
|
23
|
+
# Return the bias term (a.k.a. intercept) for LinearOneClassSVM.
|
24
|
+
# @return [Float]
|
25
|
+
attr_reader :bias_term
|
26
|
+
|
27
|
+
# Create a new estimator with linear One-class Support Vector Machine.
|
28
|
+
#
|
29
|
+
# @param nu [Float] The fraction of data as outliers. The interval of nu is (0, 1].
|
30
|
+
# @param reg_param [Float] The regularization parameter.
|
31
|
+
# @param tol [Float] The tolerance of termination criterion.
|
32
|
+
# @param verbose [Boolean] The flag indicating whether to output learning process message
|
33
|
+
# @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
|
34
|
+
def initialize(nu: 0.05, reg_param: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
|
35
|
+
check_params_numeric(nu: nu, reg_param: reg_param, tol: tol)
|
36
|
+
check_params_boolean(verbose: verbose)
|
37
|
+
check_params_numeric_or_nil(random_seed: random_seed)
|
38
|
+
@params = {}
|
39
|
+
@params[:nu] = nu.to_f
|
40
|
+
@params[:reg_param] = reg_param.to_f
|
41
|
+
@params[:tol] = tol.to_f
|
42
|
+
@params[:verbose] = verbose
|
43
|
+
@params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i
|
44
|
+
@weight_vec = nil
|
45
|
+
@bias_term = nil
|
46
|
+
@model = nil
|
47
|
+
end
|
48
|
+
|
49
|
+
# Fit the model with given training data.
|
50
|
+
#
|
51
|
+
# @overload fit(x) -> LinearOneClassSVM
|
52
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
53
|
+
#
|
54
|
+
# @return [LinearOneClassSVM] The learned estimator itself.
|
55
|
+
def fit(x, _y = nil)
|
56
|
+
x = check_convert_sample_array(x)
|
57
|
+
dummy = Numo::DFloat.ones(x.shape[0])
|
58
|
+
@model = Numo::Liblinear.train(x, dummy, liblinear_params)
|
59
|
+
@weight_vec = @model[:w].dup
|
60
|
+
@bias_term = @model[:rho]
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
# Calculate confidence scores for samples.
|
65
|
+
#
|
66
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
|
67
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
68
|
+
def decision_function(x)
|
69
|
+
raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
|
70
|
+
x = check_convert_sample_array(x)
|
71
|
+
Numo::Liblinear.decision_function(x, liblinear_params, @model)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Predict class labels for samples.
|
75
|
+
#
|
76
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
|
77
|
+
# @return [Numo::Int32] (shape: [n_samples]) Predicted label per sample.
|
78
|
+
def predict(x)
|
79
|
+
raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
|
80
|
+
x = check_convert_sample_array(x)
|
81
|
+
Numo::Int32.cast(Numo::Liblinear.predict(x, liblinear_params, @model))
|
82
|
+
end
|
83
|
+
|
84
|
+
# Dump marshal data.
|
85
|
+
# @return [Hash] The marshal data about LinearOneClassSVM.
|
86
|
+
def marshal_dump
|
87
|
+
{ params: @params,
|
88
|
+
model: @model,
|
89
|
+
weight_vec: @weight_vec,
|
90
|
+
bias_term: @bias_term }
|
91
|
+
end
|
92
|
+
|
93
|
+
# Load marshal data.
|
94
|
+
# @return [nil]
|
95
|
+
def marshal_load(obj)
|
96
|
+
@params = obj[:params]
|
97
|
+
@model = obj[:model]
|
98
|
+
@weight_vec = obj[:weight_vec]
|
99
|
+
@bias_term = obj[:bias_term]
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def liblinear_params
|
106
|
+
res = {}
|
107
|
+
res[:solver_type] = Numo::Liblinear::SolverType::ONECLASS_SVM
|
108
|
+
res[:eps] = @params[:tol]
|
109
|
+
res[:C] = @params[:reg_param]
|
110
|
+
res[:nu] = @params[:nu]
|
111
|
+
res[:verbose] = @params[:verbose]
|
112
|
+
res[:random_seed] = @params[:random_seed]
|
113
|
+
res
|
114
|
+
end
|
115
|
+
|
116
|
+
def trained?
|
117
|
+
!@model.nil?
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/rumale/svm/version.rb
CHANGED
data/rumale-svm.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
32
|
spec.require_paths = ['lib']
|
33
33
|
|
34
|
-
spec.add_runtime_dependency 'numo-liblinear', '~> 1.
|
34
|
+
spec.add_runtime_dependency 'numo-liblinear', '~> 1.1'
|
35
35
|
spec.add_runtime_dependency 'numo-libsvm', '~> 1.0'
|
36
36
|
spec.add_runtime_dependency 'rumale', '~> 0.14'
|
37
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rumale-svm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-liblinear
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: numo-libsvm
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -63,7 +63,6 @@ extensions: []
|
|
63
63
|
extra_rdoc_files: []
|
64
64
|
files:
|
65
65
|
- ".coveralls.yml"
|
66
|
-
- ".github/workflows/build.yml"
|
67
66
|
- ".gitignore"
|
68
67
|
- ".rspec"
|
69
68
|
- ".rubocop.yml"
|
@@ -74,9 +73,8 @@ files:
|
|
74
73
|
- LICENSE.txt
|
75
74
|
- README.md
|
76
75
|
- Rakefile
|
77
|
-
- bin/console
|
78
|
-
- bin/setup
|
79
76
|
- lib/rumale/svm.rb
|
77
|
+
- lib/rumale/svm/linear_one_class_svm.rb
|
80
78
|
- lib/rumale/svm/linear_svc.rb
|
81
79
|
- lib/rumale/svm/linear_svr.rb
|
82
80
|
- lib/rumale/svm/logistic_regression.rb
|
data/.github/workflows/build.yml
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
name: build
|
2
|
-
|
3
|
-
on: [push]
|
4
|
-
|
5
|
-
jobs:
|
6
|
-
build:
|
7
|
-
|
8
|
-
runs-on: ubuntu-latest
|
9
|
-
|
10
|
-
strategy:
|
11
|
-
matrix:
|
12
|
-
ruby: ['2.4.x', '2.5.x', '2.6.x']
|
13
|
-
|
14
|
-
steps:
|
15
|
-
- uses: actions/checkout@master
|
16
|
-
- name: Set up Ruby
|
17
|
-
uses: actions/setup-ruby@master
|
18
|
-
with:
|
19
|
-
ruby-version: ${{ matrix.ruby }}
|
20
|
-
- name: Build and test with Rake
|
21
|
-
run: |
|
22
|
-
gem install bundler
|
23
|
-
bundle install --jobs 4 --retry 3
|
24
|
-
bundle exec rake
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "rumale/svm"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|