-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathClassifyByTree.m
49 lines (43 loc) · 1.76 KB
/
ClassifyByTree.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
43
44
45
46
47
48
49
function [classifications] = ClassifyByTree(tree, attributes, instance)
% ClassifyByTree Classifies data instance by given tree
% args:
% tree - tree data structure
% attributes - cell array of attribute strings (no CLASS)
% instance - data including correct classification (end col.)
% return:
% classifications - 2 numbers, first given by tree, 2nd given by
% instance's last column
% tree struct:
% value - will be the string for the splitting
% attribute, or 'true' or 'false' for leaf
% left - left pointer to another tree node (left means
% the splitting attribute was false)
% right - right pointer to another tree node (right
% means the splitting attribute was true)
% Store the actual classification
actual = instance(1, length(instance));
% Recursion with 3 cases
% Case 1: Current node is labeled 'true'
% So trivially return the classification as 1
if (strcmp(tree.value, 'true'));
classifications = [1, actual];
return
end
% Case 2: Current node is labeled 'false'
% So trivially return the classification as 0
if (strcmp(tree.value, 'false'));
classifications = [0, actual];
return
end
% Case 3: Current node is labeled an attribute
% Follow correct branch by looking up index in attributes, and recur
index = find(ismember(attributes,tree.value)==1);
if (instance(1, index)); % attribute is true for this instance
% Recur down the right side
classifications = ClassifyByTree(tree.right, attributes, instance);
else
% Recur down the left side
classifications = ClassifyByTree(tree.left, attributes, instance);
end
return
end