Java Reference
In-Depth Information
private static double
information(Map items){
Map frequencies
#
new
HashMap();
// compute number of occurrences of classes
Iterator it
#
items.keySet().iterator();
while
(it.hasNext()){
Item item
#
(Item)it.next();
String category
#
(String)items.get(item);
Long num_occur
#
(Long)frequencies.get(category);
if
(num_occur
##
null
)
frequencies.put(category,
new
Long(1));
else
frequencies.put(category,
new
Long(num_occur.longValue()
!
1));
}
// compute information
double
info
#
0;
double
numItems
#
items.size();
it
#
frequencies.values().iterator();
while
(it.hasNext()){
Long num_occurr
#
(Long)it.next();
double
freq
#
num_occurr.doubleValue() / numItems;
info
!#
freq * Math.log(freq) / Math.log(2.0);
}
return
-info;
}
Finally, we define a constructor for the
DecisionTree
class that invokes the
buildDecisionTree()
method to build the decision tree; the parameters of the
constructor must be the same as those of the method.
public
DecisionTree(Map items, Map features) {
root
#
buildDecisionTree(items,features);
}
4.6.4
Test
To test the decision tree construction algorithm we need to:
define a training set of items;
■
instantiate the decision tree based on this set; and
■
check if it classifies correctly some items.
■
If the training algorithm works correctly then a decision tree equivalent to
the one used in previous testing should be generated, therefore the same test
items can be used to check if the tree is correct. We will use the training set
of items described in Table 4.2.
The test method
testExample()
is made up the following steps:
create the set of features (
“AC”
and
“ABS”
both of type
yn
);
■