Decision Tree Algorithm
Decision Tree Algorithm
http://www.kdkeys.net/forums/thread/6401.aspx
import java.io.*;
import java.util.*;
int numAttributes;
String []attributeNames;
Vector []domains;
of attributes */
class DataPoint {
};
*/
class TreeNode {
public TreeNode() {
}
};
if (index < 0) {
domains[attribute].addElement(symbol);
return index;
String symbol =
(String)domains[attribute].elementAt(point.attributes[attribute] );
if (index < 0) {
values.addElement(symbol);
values = null;
return array;
return subset;
if (numdata == 0) return 0;
double sum = 0;
int count=0;
if (point.attributes[attribute] == i) count++;
return sum;
if (node.children != null) {
if (node.decompositionAttribute == attribute )
return true;
double bestEntropy=0;
boolean selected=false;
int selectedAttribute=0;
node.entropy = calculateEntropy(node.data);
if (node.entropy == 0) return;
if ( alreadyUsedToDecompose(node, i) ) continue;
double averageentropy = 0;
if (subset.size() == 0) continue;
averageentropy += subentropy *
subset.size();
if (selected == false) {
selected = true;
bestEntropy = averageentropy;
selectedAttribute = i;
} else {
selected = true;
bestEntropy = averageentropy;
selectedAttribute = i;
node.decompositionAttribute = selectedAttribute;
node.children[j].parent = node;
node.children[j].data = getSubset(node.data,
selectedAttribute, j);
node.children[j].decompositionValue = j;
node.data = null;
FileInputStream in = null;
try {
in = new FileInputStream(inputFile);
} catch ( Exception e) {
return 0;
String input;
while(true) {
input = bin.readLine();
if (input == null) {
"\n");
return 0;
if (input.startsWith("//")) continue;
if (input.equals("")) continue;
break;
}
StringTokenizer tokenizer = new StringTokenizer(input);
numAttributes = tokenizer.countTokens();
if (numAttributes <= 1) {
line");
output attribute");
return 0;
for (int i=0; i < numAttributes; i++) domainsIdea [I] = new Vector();
while(true) {
input = bin.readLine();
if (input.startsWith("//")) continue;
if (input.equals("")) continue;
if (numtokens != numAttributes) {
);
root.data.addElement(point);
bin.close();
return 1;
if (node.children == null) {
if (values.length == 1) {
domains[outputattr].elementAt(values[0]) + "\";");
return;
");
domains[node.decompositionAttribute].elementAt(i)
+ "\") {" );
decomposeNode(root);
printTree(root, "");
/* main function */
me.createDecisionTree();
}