/**
********************
* Build the tree recursively.
********************
*/
public void buildTree() {
if (pureJudge(availableInstances)) {
return;
}//of if
if (availableInstances.length <= smallBlockThreshold) {
return;
}//of if
selectBestAttribute();
int[][] tempSubBlocks = splitData(splitAttribute);
children = new ID3[tempSubBlocks.length];
// Construct the remaining attribute set.
int[] tempRemainingAttributes = new int[availableAttributes.length - 1];
for (int i = 0; i < availableAttributes.length; i++) {
if (availableAttributes[i] < splitAttribute) {
tempRemainingAttributes[i] = availableAttributes[i];
} else if (availableAttributes[i] > splitAttribute) {
tempRemainingAttributes[i - 1] = availableAttributes[i];
}//of if
}//of for i
// Construct children.
for (int i = 0; i < children.length; i++) {
if ((tempSubBlocks[i] == null) || (tempSubBlocks[i].length == 0)) {
children[i] = null;
continue;
} else {
// System.out.println("Building children #" + i + " with
// instances " + Arrays.toString(tempSubBlocks[i]));
children[i] = new ID3(dataset, tempSubBlocks[i], tempRemainingAttributes);
// Important code: do this recursively
children[i].buildTree();
}//of if
}//of for i
}//of buildTree
/**
********************
* Classify an instance.
*
* @param paraInstance
* The given instance.
* @return The prediction.
********************
*/
public int classify(Instance paraInstance) {
if (children == null) {
return label;
}//of if
ID3 tempChild = children[(int) paraInstance.value(splitAttribute)];
if (tempChild == null) {
return label;
}//of if
return tempChild.classify(paraInstance);
}//of classify
/**
********************
* Test on a testing set.
*
* @param paraDataset
* The given testing data.
* @return The accuracy.
********************
*/
public double test(Instances paraDataset) {
double tempCorrect = 0;
for (int i = 0; i < paraDataset.numInstances(); i++) {
if (classify(paraDataset.instance(i)) == (int) paraDataset.instance(i).classValue()) {
tempCorrect++;
}//of i
}//of for i
return tempCorrect / paraDataset.numInstances();
}//of test
/**
********************
* Test on the training set.
*
* @return The accuracy.
********************
*/
public double selfTest() {
return test(dataset);
}//of selfTest
/**
*******************
* Overrides the method claimed in Object.
*
* @return The tree structure.
*******************
*/
public String toString() {
String resultString = "";
String tempAttributeName = dataset.attribute(splitAttribute).name();
if (children == null) {
resultString += "class = " + label;
} else {
for (int i = 0; i < children.length; i++) {
if (children[i] == null) {
resultString += tempAttributeName + " = "
+ dataset.attribute(splitAttribute).value(i) + ":" + "class = " + label
+ "\r\n";
} else {
resultString += tempAttributeName + " = "
+ dataset.attribute(splitAttribute).value(i) + ":" + children[i]
+ "\r\n";
}//of if
}//of for i
}//of if
return resultString;
}//of toString
/**
********************
* Test this class.
*
* Not used now.
********************
*/
public static void id3Test() {
ID3 tempID3 = new ID3("D:/mitchelles/data/weather.arff");
// ID3 tempID3 = new ID3("D:/mitchelles/data/mushroom.arff");
ID3.smallBlockThreshold = 3;
tempID3.buildTree();
System.out.println("The tree is: \r\n" + tempID3);
double tempAccuracy = tempID3.selfTest();
System.out.println("The accuracy is: " + tempAccuracy);
}//of id3Test
/**
********************
* Test this class.
*
* @param args
* Not used now.
********************
*/
public static void main(String[] args) {
id3Test();
}//of main
day62
最新推荐文章于 2024-05-31 15:46:23 发布