Class: Rumale::Tree::BaseDecisionTree

Inherits:
Base::Estimator show all
Defined in:
rumale-tree/lib/rumale/tree/base_decision_tree.rb

Overview

BaseDecisionTree is an abstract class for implementation of decision tree-based estimator. This class is used internally.

Instance Attribute Summary

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(criterion: nil, max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ BaseDecisionTree

Initialize a decision tree-based estimator.

Parameters:

  • criterion (String) (defaults to: nil)

    The function to evalue spliting point.

  • 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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'rumale-tree/lib/rumale/tree/base_decision_tree.rb', line 25

def initialize(criterion: nil, max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil,
               random_seed: nil)
  super()
  @params = {
    criterion: criterion,
    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 Method Details

#apply(x) ⇒ Numo::Int32

Return the index of the leaf that each sample reached.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Leaf index for sample.



43
44
45
46
47
# File 'rumale-tree/lib/rumale/tree/base_decision_tree.rb', line 43

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