-
Notifications
You must be signed in to change notification settings - Fork 1
/
createTree.m
42 lines (34 loc) · 1.46 KB
/
createTree.m
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
%% Create the tree recursively
% Description: Use the best split index and value of all features to create the tree recursively.
% Args:
% dataSet: The dataset to train/build the tree
% tolS: Tolerate(Min) decreased sum of variances
% tolN: Tolerate(Min) number of nodes in dataSet
% feature_used: store the index of features which have been used
% algori: only support 'CART' and 'ID3'
% Return:
% tree: The decision tree in struct type
function [ tree ] = createTree( dataSet,tolS,tolN,feature_used,algori )
feature_name = {'AT','V','AP','RH'};
tree = struct('op', [], 'kids', [], 'class', [], 'attribute', [], 'threshold', []);
[fIndex,val] = chooseBestSplit(dataSet, tolS, tolN, feature_used,algori);
% fIndex == 0 means it is a leaf node
if fIndex == 0
tree.op = [];
tree.attribute = [];
tree.class = val;
tree.threshold = [];
tree.kids = cell(0);
return
else
tree.op = feature_name{fIndex};
tree.attribute = fIndex;
tree.class = [];
tree.threshold = val;
feature_used = [feature_used, fIndex];
end
% Use the best split index and value to split the dataset to left/right DataSet
[lSet,rSet] = binSplitDataSet(dataSet, fIndex, val);
% Use tree.kids save the kid nodes recursively
tree.kids = {createTree( lSet,tolS,tolN,feature_used,algori ), createTree( rSet,tolS,tolN,feature_used,algori )};
end