Class: Rumale::Tree::GradientTreeRegressor
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Tree::GradientTreeRegressor
- Includes:
- Base::Regressor, ExtGradientTreeRegressor
- Defined in:
- rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb
Overview
GradientTreeRegressor is a class that implements decision tree for regression with exact gredy algorithm. This class is used internally for estimators with gradient tree boosting.
Reference
-
Friedman, J H., “Greedy Function Approximation: A Gradient Boosting Machine,” Annals of Statistics, 29 (5), pp. 1189–1232, 2001.
-
Friedman, J H., “Stochastic Gradient Boosting,” Computational Statistics and Data Analysis, 38 (4), pp. 367–378, 2002.
-
Chen, T., and Guestrin, C., “XGBoost: A Scalable Tree Boosting System,” Proc. KDD’16, pp. 785–794, 2016.
Instance Attribute Summary collapse
-
#feature_importances ⇒ Numo::DFloat
readonly
Return the importance for each feature.
-
#leaf_weights ⇒ Numo::DFloat
readonly
Return the values assigned each leaf.
-
#rng ⇒ Random
readonly
Return the random generator for random selection of feature index.
-
#tree ⇒ Node
readonly
Return the learned tree.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#apply(x) ⇒ Numo::Int32
Return the index of the leaf that each sample reached.
-
#fit(x, y, g, h) ⇒ GradientTreeRegressor
Fit the model with given training data.
-
#initialize(reg_lambda: 0.0, shrinkage_rate: 1.0, max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ GradientTreeRegressor
constructor
Initialize a gradient tree regressor.
-
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
Methods included from Base::Regressor
Constructor Details
#initialize(reg_lambda: 0.0, shrinkage_rate: 1.0, max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ GradientTreeRegressor
Initialize a gradient tree regressor
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 52 def initialize(reg_lambda: 0.0, shrinkage_rate: 1.0, max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) super() @params = { reg_lambda: reg_lambda, shrinkage_rate: shrinkage_rate, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes, min_samples_leaf: min_samples_leaf, max_features: max_features, random_seed: random_seed || srand } @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#feature_importances ⇒ Numo::DFloat (readonly)
Return the importance for each feature. The feature importances are calculated based on the numbers of times the feature is used for splitting.
25 26 27 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 25 def feature_importances @feature_importances end |
#leaf_weights ⇒ Numo::DFloat (readonly)
Return the values assigned each leaf.
37 38 39 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 37 def leaf_weights @leaf_weights end |
#rng ⇒ Random (readonly)
Return the random generator for random selection of feature index.
33 34 35 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 33 def rng @rng end |
#tree ⇒ Node (readonly)
Return the learned tree.
29 30 31 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 29 def tree @tree end |
Instance Method Details
#apply(x) ⇒ Numo::Int32
Return the index of the leaf that each sample reached.
106 107 108 109 110 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 106 def apply(x) x = ::Rumale::Validation.check_convert_sample_array(x) Numo::Int32[*(Array.new(x.shape[0]) { |n| partial_apply(@tree, x[n, true]) })] end |
#fit(x, y, g, h) ⇒ GradientTreeRegressor
Fit the model with given training data.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 74 def fit(x, y, g, h) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_target_value_array(y) ::Rumale::Validation.check_sample_size(x, y) # Initialize some variables. n_features = x.shape[1] @params[:max_features] ||= n_features @n_leaves = 0 @leaf_weights = [] @feature_importances = Numo::DFloat.zeros(n_features) @sub_rng = @rng.dup # Build tree. build_tree(x, y, g, h) @leaf_weights = Numo::DFloat[*@leaf_weights] self end |
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
96 97 98 99 100 |
# File 'rumale-tree/lib/rumale/tree/gradient_tree_regressor.rb', line 96 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) @leaf_weights[apply(x)].dup end |