lib/svmkit/ensemble/random_forest_regressor.rb in svmkit-0.7.0 vs lib/svmkit/ensemble/random_forest_regressor.rb in svmkit-0.7.1
- old
+ new
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require 'svmkit/validation'
+require 'svmkit/values'
require 'svmkit/base/base_estimator'
require 'svmkit/base/regressor'
require 'svmkit/tree/decision_tree_regressor'
module SVMKit
@@ -44,11 +45,12 @@
# @param min_samples_leaf [Integer] The minimum number of samples at a leaf node.
# @param max_features [Integer] The number of features to consider when searching optimal split point.
# If nil is given, split process considers all features.
# @param random_seed [Integer] The seed value using to initialize the random generator.
# It is used to randomly determine the order of features when deciding spliting point.
- def initialize(n_estimators: 10, criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1,
+ def initialize(n_estimators: 10,
+ criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1,
max_features: nil, random_seed: nil)
check_params_type_or_nil(Integer, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
max_features: max_features, random_seed: random_seed)
check_params_integer(n_estimators: n_estimators, min_samples_leaf: min_samples_leaf)
check_params_string(criterion: criterion)
@@ -87,11 +89,11 @@
# Construct forest.
@estimators = Array.new(@params[:n_estimators]) do
tree = Tree::DecisionTreeRegressor.new(
criterion: @params[:criterion], max_depth: @params[:max_depth],
max_leaf_nodes: @params[:max_leaf_nodes], min_samples_leaf: @params[:min_samples_leaf],
- max_features: @params[:max_features], random_seed: @rng.rand(int_max)
+ max_features: @params[:max_features], random_seed: @rng.rand(SVMKit::Values::int_max)
)
bootstrap_ids = Array.new(n_samples) { @rng.rand(0...n_samples) }
tree.fit(x[bootstrap_ids, true], single_target ? y[bootstrap_ids] : y[bootstrap_ids, true])
@feature_importances += tree.feature_importances
tree
@@ -133,15 +135,9 @@
@params = obj[:params]
@estimators = obj[:estimators]
@feature_importances = obj[:feature_importances]
@rng = obj[:rng]
nil
- end
-
- private
-
- def int_max
- @int_max ||= 2**([42].pack('i').size * 16 - 2) - 1
end
end
end
end