Class: Rumale::Tree::DecisionTreeRegressor

Inherits:
BaseDecisionTree show all
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.

Examples:

require 'rumale/tree/decision_tree_regressor'

estimator =
  Rumale::Tree::DecisionTreeRegressor.new(
    max_depth: 3, max_leaf_nodes: 10, min_samples_leaf: 5, random_seed: 1)
estimator.fit(training_samples, traininig_values)
results = estimator.predict(testing_samples)

Direct Known Subclasses

ExtraTreeRegressor

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::Regressor

#score

Methods inherited from BaseDecisionTree

#apply

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.

Parameters:

  • criterion (String) (defaults to: 'mse')

    The function to evaluate spliting point. Supported criteria are ‘mae’ and ‘mse’.

  • max_depth (Integer) (defaults to: nil)

    The maximum depth of the tree. If nil is given, decision tree grows without concern for depth.

  • max_leaf_nodes (Integer) (defaults to: nil)

    The maximum number of leaves on decision tree. If nil is given, number of leaves is not limited.

  • min_samples_leaf (Integer) (defaults to: 1)

    The minimum number of samples at a leaf node.

  • max_features (Integer) (defaults to: nil)

    The number of features to consider when searching optimal split point. If nil is given, split process considers all features.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator. It is used to randomly determine the order of features when deciding spliting point.



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_importancesNumo::DFloat (readonly)

Return the importance for each feature.

Returns:

  • (Numo::DFloat)

    (size: n_features)



25
26
27
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 25

def feature_importances
  @feature_importances
end

#leaf_valuesNumo::DFloat (readonly)

Return the values assigned each leaf.

Returns:

  • (Numo::DFloat)

    (shape: [n_leafs, n_outputs])



37
38
39
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 37

def leaf_values
  @leaf_values
end

#rngRandom (readonly)

Return the random generator for random selection of feature index.

Returns:

  • (Random)


33
34
35
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 33

def rng
  @rng
end

#treeNode (readonly)

Return the learned tree.

Returns:



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::DFloat)

    (shape: [n_samples, n_outputs]) The taget values to be used for fitting the model.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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 = []
  @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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the values.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_outputs]) Predicted values per sample.



83
84
85
86
87
# File 'rumale-tree/lib/rumale/tree/decision_tree_regressor.rb', line 83

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