svmkit 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +4 -0
- data/README.md +1 -1
- data/lib/svmkit.rb +2 -0
- data/lib/svmkit/ensemble/ada_boost_classifier.rb +212 -0
- data/lib/svmkit/ensemble/ada_boost_regressor.rb +200 -0
- data/lib/svmkit/version.rb +1 -1
- data/svmkit.gemspec +2 -2
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e3564c00ae91e7c4cc9db2d19b19df0158803571a18ba9b1446bea22c36b93e
|
4
|
+
data.tar.gz: ebd9a2d55e3935533144bc25498bc9db24d95cfa74fa9b2e8d6299a8b7d54409
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fc3e77783fa89bb73b68bd39c21251eae553f8f70259dab31189569b1950f760b72c41b5f839e4d133d156f952995cd2201f2538076435828d19393ea8df007
|
7
|
+
data.tar.gz: 953a07537d01e28b4c00714aebcea67789e654896af7ed5bb18900fcbf3d3fdd0be6d60bf8bfe43932e9e6b0e17fc1ade13aa7c9a9aeb564cbcfd79626979bcc
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,7 @@ SVMKit is a machine learninig library in Ruby.
|
|
9
9
|
SVMKit provides machine learning algorithms with interfaces similar to Scikit-Learn in Python.
|
10
10
|
SVMKit currently supports Linear / Kernel Support Vector Machine,
|
11
11
|
Logistic Regression, Linear Regression, Ridge, Lasso, Factorization Machine,
|
12
|
-
Naive Bayes, Decision Tree, Random Forest, K-nearest neighbor classifier,
|
12
|
+
Naive Bayes, Decision Tree, AdaBoost, Random Forest, K-nearest neighbor classifier,
|
13
13
|
K-Means, DBSCAN, Principal Component Analysis, Non-negative Matrix Factorization
|
14
14
|
and cross-validation.
|
15
15
|
|
data/lib/svmkit.rb
CHANGED
@@ -36,6 +36,8 @@ require 'svmkit/naive_bayes/naive_bayes'
|
|
36
36
|
require 'svmkit/tree/node'
|
37
37
|
require 'svmkit/tree/decision_tree_classifier'
|
38
38
|
require 'svmkit/tree/decision_tree_regressor'
|
39
|
+
require 'svmkit/ensemble/ada_boost_classifier'
|
40
|
+
require 'svmkit/ensemble/ada_boost_regressor'
|
39
41
|
require 'svmkit/ensemble/random_forest_classifier'
|
40
42
|
require 'svmkit/ensemble/random_forest_regressor'
|
41
43
|
require 'svmkit/clustering/k_means'
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'svmkit/validation'
|
4
|
+
require 'svmkit/base/base_estimator'
|
5
|
+
require 'svmkit/base/classifier'
|
6
|
+
require 'svmkit/tree/decision_tree_classifier'
|
7
|
+
|
8
|
+
module SVMKit
|
9
|
+
module Ensemble
|
10
|
+
# AdaBoostClassifier is a class that implements AdaBoost (SAMME.R) for classification.
|
11
|
+
# This class uses decision tree for a weak learner.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# estimator =
|
15
|
+
# SVMKit::Ensemble::AdaBoostClassifier.new(
|
16
|
+
# n_estimators: 10, criterion: 'gini', max_depth: 3, max_leaf_nodes: 10, min_samples_leaf: 5, random_seed: 1)
|
17
|
+
# estimator.fit(training_samples, traininig_labels)
|
18
|
+
# results = estimator.predict(testing_samples)
|
19
|
+
#
|
20
|
+
# *Reference*
|
21
|
+
# - J. Zhu, S. Rosset, H. Zou, and T.Hashie, "Multi-class AdaBoost," Technical Report No. 430, Department of Statistics, University of Michigan, 2005.
|
22
|
+
class AdaBoostClassifier
|
23
|
+
include Base::BaseEstimator
|
24
|
+
include Base::Classifier
|
25
|
+
|
26
|
+
# Return the set of estimators.
|
27
|
+
# @return [Array<DecisionTreeClassifier>]
|
28
|
+
attr_reader :estimators
|
29
|
+
|
30
|
+
# Return the class labels.
|
31
|
+
# @return [Numo::Int32] (size: n_classes)
|
32
|
+
attr_reader :classes
|
33
|
+
|
34
|
+
# Return the importance for each feature.
|
35
|
+
# @return [Numo::DFloat] (size: n_features)
|
36
|
+
attr_reader :feature_importances
|
37
|
+
|
38
|
+
# Return the random generator for random selection of feature index.
|
39
|
+
# @return [Random]
|
40
|
+
attr_reader :rng
|
41
|
+
|
42
|
+
# Create a new classifier with AdaBoost.
|
43
|
+
#
|
44
|
+
# @param n_estimators [Integer] The numeber of decision trees for contructing random forest.
|
45
|
+
# @param criterion [String] The function to evalue spliting point. Supported criteria are 'gini' and 'entropy'.
|
46
|
+
# @param max_depth [Integer] The maximum depth of the tree.
|
47
|
+
# If nil is given, decision tree grows without concern for depth.
|
48
|
+
# @param max_leaf_nodes [Integer] The maximum number of leaves on decision tree.
|
49
|
+
# If nil is given, number of leaves is not limited.
|
50
|
+
# @param min_samples_leaf [Integer] The minimum number of samples at a leaf node.
|
51
|
+
# @param max_features [Integer] The number of features to consider when searching optimal split point.
|
52
|
+
# If nil is given, split process considers all features.
|
53
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
54
|
+
# It is used to randomly determine the order of features when deciding spliting point.
|
55
|
+
def initialize(n_estimators: 50, criterion: 'gini', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1,
|
56
|
+
max_features: nil, random_seed: nil)
|
57
|
+
SVMKit::Validation.check_params_type_or_nil(Integer, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
|
58
|
+
max_features: max_features, random_seed: random_seed)
|
59
|
+
SVMKit::Validation.check_params_integer(n_estimators: n_estimators, min_samples_leaf: min_samples_leaf)
|
60
|
+
SVMKit::Validation.check_params_string(criterion: criterion)
|
61
|
+
SVMKit::Validation.check_params_positive(n_estimators: n_estimators, max_depth: max_depth,
|
62
|
+
max_leaf_nodes: max_leaf_nodes, min_samples_leaf: min_samples_leaf,
|
63
|
+
max_features: max_features)
|
64
|
+
@params = {}
|
65
|
+
@params[:n_estimators] = n_estimators
|
66
|
+
@params[:criterion] = criterion
|
67
|
+
@params[:max_depth] = max_depth
|
68
|
+
@params[:max_leaf_nodes] = max_leaf_nodes
|
69
|
+
@params[:min_samples_leaf] = min_samples_leaf
|
70
|
+
@params[:max_features] = max_features
|
71
|
+
@params[:random_seed] = random_seed
|
72
|
+
@params[:random_seed] ||= srand
|
73
|
+
@estimators = nil
|
74
|
+
@classes = nil
|
75
|
+
@feature_importances = nil
|
76
|
+
@rng = Random.new(@params[:random_seed])
|
77
|
+
end
|
78
|
+
|
79
|
+
# Fit the model with given training data.
|
80
|
+
#
|
81
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
82
|
+
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
|
83
|
+
# @return [AdaBoostClassifier] The learned classifier itself.
|
84
|
+
def fit(x, y) # rubocop:disable Metrics/AbcSize
|
85
|
+
SVMKit::Validation.check_sample_array(x)
|
86
|
+
SVMKit::Validation.check_label_array(y)
|
87
|
+
SVMKit::Validation.check_sample_label_size(x, y)
|
88
|
+
## Initialize some variables.
|
89
|
+
n_samples, n_features = x.shape
|
90
|
+
@estimators = []
|
91
|
+
@feature_importances = Numo::DFloat.zeros(n_features)
|
92
|
+
@params[:max_features] = n_features unless @params[:max_features].is_a?(Integer)
|
93
|
+
@params[:max_features] = [[1, @params[:max_features]].max, n_features].min
|
94
|
+
@classes = Numo::Int32.asarray(y.to_a.uniq.sort)
|
95
|
+
n_classes = @classes.shape[0]
|
96
|
+
## Boosting.
|
97
|
+
classes_arr = @classes.to_a
|
98
|
+
y_codes = Numo::DFloat.zeros(n_samples, n_classes) - 1.fdiv(n_classes - 1)
|
99
|
+
n_samples.times { |n| y_codes[n, classes_arr.index(y[n])] = 1.0 }
|
100
|
+
observation_weights = Numo::DFloat.zeros(n_samples) + 1.fdiv(n_samples)
|
101
|
+
@params[:n_estimators].times do |_t|
|
102
|
+
# Fit classfier.
|
103
|
+
ids = weighted_sampling(observation_weights)
|
104
|
+
break if y[ids].to_a.uniq.size != n_classes
|
105
|
+
tree = Tree::DecisionTreeClassifier.new(
|
106
|
+
criterion: @params[:criterion], max_depth: @params[:max_depth],
|
107
|
+
max_leaf_nodes: @params[:max_leaf_nodes], min_samples_leaf: @params[:min_samples_leaf],
|
108
|
+
max_features: @params[:max_features], random_seed: @rng.rand(int_max)
|
109
|
+
)
|
110
|
+
tree.fit(x[ids, true], y[ids])
|
111
|
+
# Calculate estimator error.
|
112
|
+
proba = tree.predict_proba(x).clip(1.0e-15, nil)
|
113
|
+
p = Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[proba[n, true].max_index] })
|
114
|
+
inds = p.ne(y)
|
115
|
+
error = (observation_weights * inds).sum / observation_weights.sum
|
116
|
+
# Store model.
|
117
|
+
@estimators.push(tree)
|
118
|
+
@feature_importances += tree.feature_importances
|
119
|
+
break if error.zero?
|
120
|
+
# Update observation weights.
|
121
|
+
log_proba = Numo::NMath.log(proba)
|
122
|
+
observation_weights *= Numo::NMath.exp(-1.0 * (n_classes - 1).fdiv(n_classes) * (y_codes * log_proba).sum(1))
|
123
|
+
observation_weights = observation_weights.clip(1.0e-15, nil)
|
124
|
+
sum_observation_weights = observation_weights.sum
|
125
|
+
break if sum_observation_weights.zero?
|
126
|
+
observation_weights /= sum_observation_weights
|
127
|
+
end
|
128
|
+
@feature_importances /= @feature_importances.sum
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
# Calculate confidence scores for samples.
|
133
|
+
#
|
134
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
|
135
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
136
|
+
def decision_function(x)
|
137
|
+
SVMKit::Validation.check_sample_array(x)
|
138
|
+
n_samples, = x.shape
|
139
|
+
n_classes = @classes.size
|
140
|
+
sum_probs = Numo::DFloat.zeros(n_samples, n_classes)
|
141
|
+
@estimators.each do |tree|
|
142
|
+
log_proba = Numo::NMath.log(tree.predict_proba(x).clip(1.0e-15, nil))
|
143
|
+
sum_probs += (n_classes - 1) * (log_proba - 1.fdiv(n_classes) * Numo::DFloat[log_proba.sum(1)].transpose)
|
144
|
+
end
|
145
|
+
sum_probs /= @estimators.size
|
146
|
+
end
|
147
|
+
|
148
|
+
# Predict class labels for samples.
|
149
|
+
#
|
150
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
|
151
|
+
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
|
152
|
+
def predict(x)
|
153
|
+
SVMKit::Validation.check_sample_array(x)
|
154
|
+
n_samples, = x.shape
|
155
|
+
probs = decision_function(x)
|
156
|
+
Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[probs[n, true].max_index] })
|
157
|
+
end
|
158
|
+
|
159
|
+
# Predict probability for samples.
|
160
|
+
#
|
161
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities.
|
162
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.
|
163
|
+
def predict_proba(x)
|
164
|
+
SVMKit::Validation.check_sample_array(x)
|
165
|
+
n_classes = @classes.size
|
166
|
+
probs = Numo::NMath.exp(1.fdiv(n_classes - 1) * decision_function(x))
|
167
|
+
sum_probs = probs.sum(1)
|
168
|
+
probs /= Numo::DFloat[sum_probs].transpose
|
169
|
+
probs
|
170
|
+
end
|
171
|
+
|
172
|
+
# Dump marshal data.
|
173
|
+
# @return [Hash] The marshal data about AdaBoostClassifier.
|
174
|
+
def marshal_dump
|
175
|
+
{ params: @params, estimators: @estimators, classes: @classes,
|
176
|
+
feature_importances: @feature_importances, rng: @rng }
|
177
|
+
end
|
178
|
+
|
179
|
+
# Load marshal data.
|
180
|
+
# @return [nil]
|
181
|
+
def marshal_load(obj)
|
182
|
+
@params = obj[:params]
|
183
|
+
@estimators = obj[:estimators]
|
184
|
+
@classes = obj[:classes]
|
185
|
+
@feature_importances = obj[:feature_importances]
|
186
|
+
@rng = obj[:rng]
|
187
|
+
nil
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
def weighted_sampling(weights)
|
193
|
+
Array.new(weights.size) do
|
194
|
+
target = @rng.rand
|
195
|
+
chosen = 0
|
196
|
+
weights.each_with_index do |w, idx|
|
197
|
+
if target <= w
|
198
|
+
chosen = idx
|
199
|
+
break
|
200
|
+
end
|
201
|
+
target -= w
|
202
|
+
end
|
203
|
+
chosen
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def int_max
|
208
|
+
@int_max ||= 2**([42].pack('i').size * 16 - 2) - 1
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'svmkit/validation'
|
4
|
+
require 'svmkit/base/base_estimator'
|
5
|
+
require 'svmkit/base/regressor'
|
6
|
+
require 'svmkit/tree/decision_tree_regressor'
|
7
|
+
|
8
|
+
module SVMKit
|
9
|
+
module Ensemble
|
10
|
+
# AdaBoostRegressor is a class that implements random forest for regression
|
11
|
+
# This class uses decision tree for a weak learner.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# estimator =
|
15
|
+
# SVMKit::Ensemble::AdaBoostRegressor.new(
|
16
|
+
# n_estimators: 10, criterion: 'mse', max_depth: 3, max_leaf_nodes: 10, min_samples_leaf: 5, random_seed: 1)
|
17
|
+
# estimator.fit(training_samples, traininig_values)
|
18
|
+
# results = estimator.predict(testing_samples)
|
19
|
+
#
|
20
|
+
# *Reference*
|
21
|
+
# - D. L. Shrestha and D. P. Solomatine, "Experiments with AdaBoost.RT, an Improved Boosting Scheme for Regression," Neural Computation 18 (7), pp. 1678--1710, 2006.
|
22
|
+
#
|
23
|
+
class AdaBoostRegressor
|
24
|
+
include Base::BaseEstimator
|
25
|
+
include Base::Regressor
|
26
|
+
include Validation
|
27
|
+
|
28
|
+
# Return the set of estimators.
|
29
|
+
# @return [Array<DecisionTreeRegressor>]
|
30
|
+
attr_reader :estimators
|
31
|
+
|
32
|
+
# Return the weight for each weak learner.
|
33
|
+
# @return [Numo::DFloat] (size: n_estimates)
|
34
|
+
attr_reader :estimator_weights
|
35
|
+
|
36
|
+
# Return the importance for each feature.
|
37
|
+
# @return [Numo::DFloat] (size: n_features)
|
38
|
+
attr_reader :feature_importances
|
39
|
+
|
40
|
+
# Return the random generator for random selection of feature index.
|
41
|
+
# @return [Random]
|
42
|
+
attr_reader :rng
|
43
|
+
|
44
|
+
# Create a new regressor with random forest.
|
45
|
+
#
|
46
|
+
# @param n_estimators [Integer] The numeber of decision trees for contructing random forest.
|
47
|
+
# @param threshold [Float] The threshold for delimiting correct and incorrect predictions. That is constrained to [0, 1]
|
48
|
+
# @param exponent [Float] The exponent for the weight of each weak learner.
|
49
|
+
# @param criterion [String] The function to evalue spliting point. Supported criteria are 'gini' and 'entropy'.
|
50
|
+
# @param max_depth [Integer] The maximum depth of the tree.
|
51
|
+
# If nil is given, decision tree grows without concern for depth.
|
52
|
+
# @param max_leaf_nodes [Integer] The maximum number of leaves on decision tree.
|
53
|
+
# If nil is given, number of leaves is not limited.
|
54
|
+
# @param min_samples_leaf [Integer] The minimum number of samples at a leaf node.
|
55
|
+
# @param max_features [Integer] The number of features to consider when searching optimal split point.
|
56
|
+
# If nil is given, split process considers all features.
|
57
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
58
|
+
# It is used to randomly determine the order of features when deciding spliting point.
|
59
|
+
def initialize(n_estimators: 10, threshold: 0.2, exponent: 1.0,
|
60
|
+
criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1,
|
61
|
+
max_features: nil, random_seed: nil)
|
62
|
+
check_params_type_or_nil(Integer, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
|
63
|
+
max_features: max_features, random_seed: random_seed)
|
64
|
+
check_params_integer(n_estimators: n_estimators, min_samples_leaf: min_samples_leaf)
|
65
|
+
check_params_float(threshold: threshold, exponent: exponent)
|
66
|
+
check_params_string(criterion: criterion)
|
67
|
+
check_params_positive(n_estimators: n_estimators, threshold: threshold, exponent: exponent,
|
68
|
+
max_depth: max_depth,
|
69
|
+
max_leaf_nodes: max_leaf_nodes, min_samples_leaf: min_samples_leaf,
|
70
|
+
max_features: max_features)
|
71
|
+
@params = {}
|
72
|
+
@params[:n_estimators] = n_estimators
|
73
|
+
@params[:threshold] = threshold
|
74
|
+
@params[:exponent] = exponent
|
75
|
+
@params[:criterion] = criterion
|
76
|
+
@params[:max_depth] = max_depth
|
77
|
+
@params[:max_leaf_nodes] = max_leaf_nodes
|
78
|
+
@params[:min_samples_leaf] = min_samples_leaf
|
79
|
+
@params[:max_features] = max_features
|
80
|
+
@params[:random_seed] = random_seed
|
81
|
+
@params[:random_seed] ||= srand
|
82
|
+
@estimators = nil
|
83
|
+
@feature_importances = nil
|
84
|
+
@rng = Random.new(@params[:random_seed])
|
85
|
+
end
|
86
|
+
|
87
|
+
# Fit the model with given training data.
|
88
|
+
#
|
89
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
90
|
+
# @param y [Numo::DFloat] (shape: [n_samples]) The target values to be used for fitting the model.
|
91
|
+
# @return [AdaBoostRegressor] The learned regressor itself.
|
92
|
+
def fit(x, y) # rubocop:disable Metrics/AbcSize
|
93
|
+
check_sample_array(x)
|
94
|
+
check_tvalue_array(y)
|
95
|
+
check_sample_tvalue_size(x, y)
|
96
|
+
# Check target values
|
97
|
+
raise ArgumentError, 'Expect target value vector to be 1-D arrray' unless y.shape.size == 1
|
98
|
+
# Initialize some variables.
|
99
|
+
n_samples, n_features = x.shape
|
100
|
+
@params[:max_features] = n_features unless @params[:max_features].is_a?(Integer)
|
101
|
+
@params[:max_features] = [[1, @params[:max_features]].max, n_features].min
|
102
|
+
observation_weights = Numo::DFloat.zeros(n_samples) + 1.fdiv(n_samples)
|
103
|
+
@estimators = []
|
104
|
+
@estimator_weights = []
|
105
|
+
@feature_importances = Numo::DFloat.zeros(n_features)
|
106
|
+
# Construct forest.
|
107
|
+
@params[:n_estimators].times do |_t|
|
108
|
+
# Fit weak learner.
|
109
|
+
ids = weighted_sampling(observation_weights)
|
110
|
+
tree = Tree::DecisionTreeRegressor.new(
|
111
|
+
criterion: @params[:criterion], max_depth: @params[:max_depth],
|
112
|
+
max_leaf_nodes: @params[:max_leaf_nodes], min_samples_leaf: @params[:min_samples_leaf],
|
113
|
+
max_features: @params[:max_features], random_seed: @rng.rand(int_max)
|
114
|
+
)
|
115
|
+
tree.fit(x[ids, true], y[ids])
|
116
|
+
p = tree.predict(x)
|
117
|
+
# Calculate errors.
|
118
|
+
abs_err = ((p - y) / y).abs
|
119
|
+
err = observation_weights[abs_err.gt(@params[:threshold])].sum
|
120
|
+
break if err <= 0.0
|
121
|
+
# Calculate weight.
|
122
|
+
beta = err**@params[:exponent]
|
123
|
+
weight = Math.log(1.fdiv(beta))
|
124
|
+
# Store model.
|
125
|
+
@estimators.push(tree)
|
126
|
+
@estimator_weights.push(weight)
|
127
|
+
@feature_importances += weight * tree.feature_importances
|
128
|
+
# Update observation weights.
|
129
|
+
update = Numo::DFloat.ones(n_samples)
|
130
|
+
update[abs_err.le(@params[:threshold])] = beta
|
131
|
+
observation_weights *= update
|
132
|
+
observation_weights = observation_weights.clip(1.0e-15, nil)
|
133
|
+
sum_observation_weights = observation_weights.sum
|
134
|
+
break if sum_observation_weights.zero?
|
135
|
+
observation_weights /= sum_observation_weights
|
136
|
+
end
|
137
|
+
@estimator_weights = Numo::DFloat.asarray(@estimator_weights)
|
138
|
+
@feature_importances /= @estimator_weights.sum
|
139
|
+
self
|
140
|
+
end
|
141
|
+
|
142
|
+
# Predict values for samples.
|
143
|
+
#
|
144
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
|
145
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_outputs]) Predicted value per sample.
|
146
|
+
def predict(x)
|
147
|
+
check_sample_array(x)
|
148
|
+
n_samples, = x.shape
|
149
|
+
predictions = Numo::DFloat.zeros(n_samples)
|
150
|
+
@estimators.size.times do |t|
|
151
|
+
predictions += @estimator_weights[t] * @estimators[t].predict(x)
|
152
|
+
end
|
153
|
+
sum_weight = @estimator_weights.sum
|
154
|
+
predictions / sum_weight
|
155
|
+
end
|
156
|
+
|
157
|
+
# Dump marshal data.
|
158
|
+
# @return [Hash] The marshal data about AdaBoostRegressor.
|
159
|
+
def marshal_dump
|
160
|
+
{ params: @params,
|
161
|
+
estimators: @estimators,
|
162
|
+
estimator_weights: @estimator_weights,
|
163
|
+
feature_importances: @feature_importances,
|
164
|
+
rng: @rng }
|
165
|
+
end
|
166
|
+
|
167
|
+
# Load marshal data.
|
168
|
+
# @return [nil]
|
169
|
+
def marshal_load(obj)
|
170
|
+
@params = obj[:params]
|
171
|
+
@estimators = obj[:estimators]
|
172
|
+
@estimator_weights = obj[:estimator_weights]
|
173
|
+
@feature_importances = obj[:feature_importances]
|
174
|
+
@rng = obj[:rng]
|
175
|
+
nil
|
176
|
+
end
|
177
|
+
|
178
|
+
private
|
179
|
+
|
180
|
+
def weighted_sampling(weights)
|
181
|
+
Array.new(weights.size) do
|
182
|
+
target = @rng.rand
|
183
|
+
chosen = 0
|
184
|
+
weights.each_with_index do |w, idx|
|
185
|
+
if target <= w
|
186
|
+
chosen = idx
|
187
|
+
break
|
188
|
+
end
|
189
|
+
target -= w
|
190
|
+
end
|
191
|
+
chosen
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def int_max
|
196
|
+
@int_max ||= 2**([42].pack('i').size * 16 - 2) - 1
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
data/lib/svmkit/version.rb
CHANGED
data/svmkit.gemspec
CHANGED
@@ -17,7 +17,7 @@ SVMKit is a machine learninig library in Ruby.
|
|
17
17
|
SVMKit provides machine learning algorithms with interfaces similar to Scikit-Learn in Python.
|
18
18
|
SVMKit currently supports Linear / Kernel Support Vector Machine,
|
19
19
|
Logistic Regression, Linear Regression, Ridge, Lasso, Factorization Machine,
|
20
|
-
Naive Bayes, Decision Tree, Random Forest, K-nearest neighbor algorithm,
|
20
|
+
Naive Bayes, Decision Tree, AdaBoost, Random Forest, K-nearest neighbor algorithm,
|
21
21
|
K-Means, DBSCAN, Principal Component Analysis, Non-negative Matrix Factorization
|
22
22
|
and cross-validation.
|
23
23
|
MSG
|
@@ -33,7 +33,7 @@ MSG
|
|
33
33
|
|
34
34
|
spec.required_ruby_version = '>= 2.1'
|
35
35
|
|
36
|
-
spec.add_runtime_dependency 'numo-narray', '>= 0.9.
|
36
|
+
spec.add_runtime_dependency 'numo-narray', '>= 0.9.1'
|
37
37
|
|
38
38
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
39
39
|
spec.add_development_dependency 'coveralls', '~> 0.8'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svmkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-narray
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.9.
|
19
|
+
version: 0.9.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: 0.9.
|
26
|
+
version: 0.9.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -85,7 +85,7 @@ description: |
|
|
85
85
|
SVMKit provides machine learning algorithms with interfaces similar to Scikit-Learn in Python.
|
86
86
|
SVMKit currently supports Linear / Kernel Support Vector Machine,
|
87
87
|
Logistic Regression, Linear Regression, Ridge, Lasso, Factorization Machine,
|
88
|
-
Naive Bayes, Decision Tree, Random Forest, K-nearest neighbor algorithm,
|
88
|
+
Naive Bayes, Decision Tree, AdaBoost, Random Forest, K-nearest neighbor algorithm,
|
89
89
|
K-Means, DBSCAN, Principal Component Analysis, Non-negative Matrix Factorization
|
90
90
|
and cross-validation.
|
91
91
|
email:
|
@@ -121,6 +121,8 @@ files:
|
|
121
121
|
- lib/svmkit/dataset.rb
|
122
122
|
- lib/svmkit/decomposition/nmf.rb
|
123
123
|
- lib/svmkit/decomposition/pca.rb
|
124
|
+
- lib/svmkit/ensemble/ada_boost_classifier.rb
|
125
|
+
- lib/svmkit/ensemble/ada_boost_regressor.rb
|
124
126
|
- lib/svmkit/ensemble/random_forest_classifier.rb
|
125
127
|
- lib/svmkit/ensemble/random_forest_regressor.rb
|
126
128
|
- lib/svmkit/evaluation_measure/accuracy.rb
|