This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtree.h
69 lines (53 loc) · 2.87 KB
/
tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef TREE_H_
#define TREE_H_
#include "types.h"
// Initialize some global variables.
void InitializeTreeData(const vector<Example>& examples, float normalizer);
// Return root node for a tree.
Node MakeRootNode(const vector<Example>& examples);
// Return a tree trained on examples.
Tree TrainTree(const vector<Example>& examples);
// Make child nodes using split feature/value and add them to the tree. Also
// update info in the parent node, like child pointers.
void MakeChildNodes(Feature split_feature, Value split_value, Node* parent,
Tree* tree);
// Return a map from each value of feature to a pair of weights. The first
// weight in the pair is the total weight of positive examples at node that have
// that value for feature, and the second weight in the pair is the total weight
// of negative examples at node that have that value for feature. This map is
// used to determine the best split feature/value.
map<Value, pair<Weight, Weight>> MakeValueToWeightsMap(const Node& node,
Feature feature);
// Given a value-to-weights map for a feature (constructed by
// MakeValueToWeightsMap()), determine the best split value for the feature and
// the improvement in the gradient of the objective if we split on that value.
// Note that delta_gradient <= 0 indicates that we should not split on this
// feature.
void BestSplitValue(const map<Value, pair<Weight, Weight>>& value_to_weights,
const Node& node, int tree_size, Value* split_value,
float* delta_gradient);
// Given an example and a tree, classify the example with the tree.
// NB: This function assumes that if an example has a feature value that is
// _less than or equal to_ a node's split value then the example should be sent
// to the left child, and otherwise sent to the right child.
Label ClassifyExample(const Example& example, const Tree& tree);
// Return the (sub)gradient of the objective with respect to a tree.
float Gradient(float wgtd_error, int tree_size, float alpha, int sign_edge);
// Given a set of examples and a tree, return the weighted error of tree on
// the examples.
float EvaluateTreeWgtd(const vector<Example>& examples, const Tree& tree);
// Return complexity penalty.
float ComplexityPenalty(int tree_size);
#endif // TREE_H_