Class: SVMKit::Tree::DecisionTreeRegressor

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Regressor
Defined in:
lib/svmkit/tree/decision_tree_regressor.rb

Overview

DecisionTreeRegressor is a class that implements decision tree for regression.

Examples:

estimator =
  SVMKit::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)

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Methods included from Base::Regressor

#score

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 52

def initialize(criterion: 'mse', max_depth: nil, max_leaf_nodes: nil, min_samples_leaf: 1, max_features: nil,
               random_seed: nil)
  check_params_type_or_nil(Integer, max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
                                    max_features: max_features, random_seed: random_seed)
  check_params_integer(min_samples_leaf: min_samples_leaf)
  check_params_string(criterion: criterion)
  check_params_positive(max_depth: max_depth, max_leaf_nodes: max_leaf_nodes,
                        min_samples_leaf: min_samples_leaf, max_features: max_features)
  @params = {}
  @params[:criterion] = criterion
  @params[:max_depth] = max_depth
  @params[:max_leaf_nodes] = max_leaf_nodes
  @params[:min_samples_leaf] = min_samples_leaf
  @params[:max_features] = max_features
  @params[:random_seed] = random_seed
  @params[:random_seed] ||= srand
  @criterion = :mse
  @criterion = :mae if @params[:criterion] == 'mae'
  @tree = nil
  @feature_importances = nil
  @n_leaves = nil
  @leaf_values = nil
  @rng = Random.new(@params[:random_seed])
end

Instance Attribute Details

#feature_importancesNumo::DFloat (readonly)

Return the importance for each feature.

Returns:

  • (Numo::DFloat)

    (size: n_features)



26
27
28
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 26

def feature_importances
  @feature_importances
end

#leaf_valuesNumo::DFloat (readonly)

Return the values assigned each leaf.

Returns:

  • (Numo::DFloat)

    (shape: [n_leafs, n_outputs])



38
39
40
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 38

def leaf_values
  @leaf_values
end

#rngRandom (readonly)

Return the random generator for random selection of feature index.

Returns:

  • (Random)


34
35
36
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 34

def rng
  @rng
end

#treeNode (readonly)

Return the learned tree.

Returns:



30
31
32
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 30

def tree
  @tree
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 values.

Returns:

  • (Numo::Int32)

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



110
111
112
113
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 110

def apply(x)
  check_sample_array(x)
  Numo::Int32[*(Array.new(x.shape[0]) { |n| apply_at_node(@tree, x[n, true]) })]
end

#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:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 82

def fit(x, y)
  check_sample_array(x)
  check_tvalue_array(y)
  check_sample_tvalue_size(x, y)
  single_target = y.shape[1].nil?
  y = y.expand_dims(1) if single_target
  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
  build_tree(x, y)
  @leaf_values = @leaf_values[true] if single_target
  eval_importance(n_samples, n_features)
  self
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about DecisionTreeRegressor



117
118
119
120
121
122
123
124
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 117

def marshal_dump
  { params: @params,
    criterion: @criterion,
    tree: @tree,
    feature_importances: @feature_importances,
    leaf_values: @leaf_values,
    rng: @rng }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


128
129
130
131
132
133
134
135
136
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 128

def marshal_load(obj)
  @params = obj[:params]
  @criterion = obj[:criterion]
  @tree = obj[:tree]
  @feature_importances = obj[:feature_importances]
  @leaf_values = obj[:leaf_values]
  @rng = obj[:rng]
  nil
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.



101
102
103
104
# File 'lib/svmkit/tree/decision_tree_regressor.rb', line 101

def predict(x)
  check_sample_array(x)
  @leaf_values.shape[1].nil? ? @leaf_values[apply(x)] : @leaf_values[apply(x), true]
end