Class: Rumale::Tree::DecisionTreeClassifier

Inherits:
BaseDecisionTree show all
Includes:
Base::Classifier, ExtDecisionTreeClassifier
Defined in:
rumale-tree/lib/rumale/tree/decision_tree_classifier.rb

Overview

DecisionTreeClassifier is a class that implements decision tree for classification.

Examples:

require 'rumale/tree/decision_tree_classifier'

estimator =
  Rumale::Tree::DecisionTreeClassifier.new(
    criterion: 'gini', max_depth: 3, max_leaf_nodes: 10, min_samples_leaf: 5, random_seed: 1)
estimator.fit(training_samples, traininig_labels)
results = estimator.predict(testing_samples)

Direct Known Subclasses

ExtraTreeClassifier

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::Classifier

#score

Methods inherited from BaseDecisionTree

#apply

Constructor Details

#initialize(criterion: 'gini', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil, random_seed: nil) ⇒ DecisionTreeClassifier

Create a new classifier with decision tree algorithm.

Parameters:

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

    The function to evaluate spliting point. Supported criteria are ‘gini’ and ‘entropy’.

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



55
56
57
58
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 55

def initialize(criterion: 'gini', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil,
               random_seed: nil)
  super
end

Instance Attribute Details

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



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

def classes
  @classes
end

#feature_importancesNumo::DFloat (readonly)

Return the importance for each feature.

Returns:

  • (Numo::DFloat)

    (size: n_features)



29
30
31
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 29

def feature_importances
  @feature_importances
end

#leaf_labelsNumo::Int32 (readonly)

Return the labels assigned each leaf.

Returns:

  • (Numo::Int32)

    (size: n_leafs)



41
42
43
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 41

def leaf_labels
  @leaf_labels
end

#rngRandom (readonly)

Return the random generator for random selection of feature index.

Returns:

  • (Random)


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

def rng
  @rng
end

#treeNode (readonly)

Return the learned tree.

Returns:



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

def tree
  @tree
end

Instance Method Details

#fit(x, y) ⇒ DecisionTreeClassifier

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::Int32)

    (shape: [n_samples]) The labels to be used for fitting the model.

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 65

def fit(x, y)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  y = ::Rumale::Validation.check_convert_label_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
  y = Numo::Int32.cast(y) unless y.is_a?(Numo::Int32)
  uniq_y = y.to_a.uniq.sort
  @classes = Numo::Int32.asarray(uniq_y)
  @n_leaves = 0
  @leaf_labels = []
  @feature_ids = Array.new(n_features) { |v| v }
  @sub_rng = @rng.dup
  build_tree(x, y.map { |v| uniq_y.index(v) })
  eval_importance(n_samples, n_features)
  @leaf_labels = Numo::Int32[*@leaf_labels]
  self
end

#predict(x) ⇒ Numo::Int32

Predict class labels for samples.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted class label per sample.



90
91
92
93
94
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 90

def predict(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  @leaf_labels[apply(x)].dup
end

#predict_proba(x) ⇒ Numo::DFloat

Predict probability for samples.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted probability of each class per sample.



100
101
102
103
104
# File 'rumale-tree/lib/rumale/tree/decision_tree_classifier.rb', line 100

def predict_proba(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  Numo::DFloat[*(Array.new(x.shape[0]) { |n| partial_predict_proba(@tree, x[n, true]) })]
end