Class: Rumale::Tree::DecisionTreeRegressor
- Inherits:
-
BaseDecisionTree
- Object
- Base::Estimator
- BaseDecisionTree
- Rumale::Tree::DecisionTreeRegressor
- Includes:
- Base::Regressor, ExtDecisionTreeRegressor
- Defined in:
- rumale-tree/lib/rumale/tree/decision_tree_regressor.rb
Overview
DecisionTreeRegressor is a class that implements decision tree for regression.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#feature_importances ⇒ Numo::DFloat
readonly
Return the importance for each feature.
-
#leaf_values ⇒ 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
-
#fit(x, y) ⇒ DecisionTreeRegressor
Fit the model with given training data.
-
#initialize(criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ DecisionTreeRegressor
constructor
Create a new regressor with decision tree algorithm.
-
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
Methods included from Base::Regressor
Methods inherited from BaseDecisionTree
Constructor Details
#initialize(criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ DecisionTreeRegressor
Create a new regressor with decision tree algorithm.
51 52 53 54 |
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 51 def initialize(criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) super end |
Instance Attribute Details
#feature_importances ⇒ Numo::DFloat (readonly)
Return the importance for each feature.
25 26 27 |
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 25 def feature_importances @feature_importances end |
#leaf_values ⇒ Numo::DFloat (readonly)
Return the values assigned each leaf.
37 38 39 |
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 37 def leaf_values @leaf_values end |
#rng ⇒ Random (readonly)
Return the random generator for random selection of feature index.
33 34 35 |
# File 'rumale-tree/lib/rumale/tree/decision_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/decision_tree_regressor.rb', line 29 def tree @tree end |
Instance Method Details
#fit(x, y) ⇒ DecisionTreeRegressor
Fit the model with given training data.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 61 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_target_value_array(y) ::Rumale::Validation.check_sample_size(x, y) n_samples, n_features = x.shape @params[:max_features] = n_features if @params[:max_features].nil? @params[:max_features] = [@params[:max_features], n_features].min @n_leaves = 0 @leaf_values = [] @feature_ids = Array.new(x.shape[1]) { |v| v } @sub_rng = @rng.dup build_tree(x, y) eval_importance(n_samples, n_features) @leaf_values = Numo::DFloat.cast(@leaf_values) @leaf_values = @leaf_values.flatten.dup if @leaf_values.shape[1] == 1 self end |
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
84 85 86 87 88 |
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 84 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) @leaf_values.shape[1].nil? ? @leaf_values[apply(x)].dup : @leaf_values[apply(x), true].dup end |