📄 decisiontree.java
字号:
if (classResult!=0) {
short classActual = getLastElement(testDataArray[index]);
if (classResult == classActual) correctClassCounter++;
}
}
// Output classification results
double accuracy = ((double) correctClassCounter*100.0/(double) index);
// Return
return(accuracy);
}
/* CLASSIFY RECORD (HIGHEST CONFIDENCE AND DEFAULT RULE) */
/** Searches through rule data looking for a rule antecedent which is a
subset of the input set or the default rule (last rule). <P> Copy of
method in AprioriTclass.
@param itemset the record to be classified.
@return the classification. */
protected short classifyRecordDefault(short[] itemSet) {
return(classifyRecordDefault(itemSet,startRulelist));
}
/** Continues process of searching through rule data looking for a rule
antecedent which is a subset of the input set or the default rule (last
rule).
@param itemset the record to be classified.
@param node the currentNode.
@return the classification. */
protected short classifyRecordDefault(short[] itemSet, RuleNode node) {
// Process node
if (node != null) {
// Left branch
short consClass = classifyRecordDefault(itemSet,node.leftBranch);
if (consClass!=0) return(consClass);
// Node
if ((node.ruleNumber==numRules) ||
(isSubset(node.antecedent,itemSet))) return(node.consequent[0]);
// Right branch
return(classifyRecordDefault(itemSet,node.rightBranch));
}
// Return
return(0);
}
/* -------------------------------------------------------------------- */
/* */
/* RULE LINKED LIST ORDERED ACCORDING TO SIZE OF ANTECEDENT */
/* */
/* -------------------------------------------------------------------- */
/* Methods for inserting rules into a linked list of rules ordered
according to size of antecedent (largest first) and confidence (most
confident first). Each rule described in
terms of 3 fields: 1) Antecedent (an item set), 2) a consequent (an item
set), 3) a confidence value (double). <P> The support field is not used. */
/* INSERT (ASSOCIATION/CLASSIFICATION) RULE INTO RULE LINKED LIST (ORDERED
ACCORDING CONFIDENCE). */
/** Inserts an (association/classification) rule into the linkedlist of
rules pointed at by <TT>startRulelist</TT>. <P> The list is ordered so that
rules with highest confidence are listed first. If two rules have the same
confidence the new rule will be placed after the existing rule. Thus, if
using an Apriori approach to generating rules, more general rules will
appear first in the list with more specific rules (i.e. rules with a larger
antecedent) appearing later as the more general rules will be generated
first.
@param antecedent the antecedent (LHS) of the rule.
@param consequent the consequent (RHS) of the rule.
@param supportForRule the associated support value. */
protected void insertRuleIntoRulelist(short[] antecedent,
short[] consequent, double confidenceForRule) {
// Check for empty tree.
if (startRulelist == null) startRulelist = new RuleNode(antecedent,
consequent,confidenceForRule,0.0);
// Otherwise "walk" tree
else insertRuleIntoRulelist(startRulelist,antecedent,consequent,
confidenceForRule);
}
/** Continues process of adding rule to binary tree according to size of
antecedent
@param currentNode the current location in the bin tree.
@param antecedent the antecedent (LHS) of the rule.
@param consequent the consequent (RHS) of the rule.
@param orderingValue the associated support value. */
private void insertRuleIntoRulelist(RuleNode currentNode,
short[] antecedent, short[] consequent, double orderingValue) {
boolean addToLeftBranch = false;
// Calculate selector
if (antecedent.length>currentNode.antecedent.length)
addToLeftBranch=true;
else if (antecedent.length==currentNode.antecedent.length &&
orderingValue>currentNode.confidenceForRule)
addToLeftBranch=true;
// Left branch
if (addToLeftBranch) {
if (currentNode.leftBranch==null) currentNode.leftBranch = new
RuleNode(antecedent,consequent,orderingValue,0.0);
else insertRuleIntoRulelist(currentNode.leftBranch,antecedent,
consequent,orderingValue);
}
// Right branch
else {
if (currentNode.rightBranch==null) currentNode.rightBranch = new
RuleNode(antecedent,consequent,orderingValue,0.0);
else insertRuleIntoRulelist(currentNode.rightBranch,antecedent,
consequent,orderingValue);
}
}
/* ---------------------------------------------------------------- */
/* */
/* OUTPUT */
/* */
/* ---------------------------------------------------------------- */
/* OUTPUT MENU */
/** Outputs menu for command line arguments. (Overides higher level method)
*/
protected void outputMenu() {
System.out.println();
System.out.println("-F = Training file name");
System.out.println("-N = Number of classes");
//System.out.println("-T = Test file name (if any)");
System.out.println();
// Exit
System.exit(1);
}
/* OUTPUT SETTINGS */
/** Outputs command line values provided by user. (Overides higher level
method.) */
protected void outputSettings() {
System.out.println("SETTINGS\n--------");
System.out.println("Training File name = " + fileName);
System.out.println("Test File name (optional) = " + fileName);
System.out.println("Number of classes = " + numClasses);
System.out.println();
}
/* OUTPUT NUMBER OF CLASSES */
/** Outputs number of classes. */
public void outputNumClasses() {
System.out.println("Number of classes = " + numClasses);
}
/* OUTPUT TEST DATA TABLE */
/** Outputs stored test set input data set read from input data file. */
public void outputTestDataArray() {
System.out.println("TEST DATA ARRAY\n---------------");
for(int index=0;index<testDataArray.length;index++) {
System.out.print("Test rec #" + (index+1) + ": ");
outputItemSet(testDataArray[index]);
System.out.println();
}
// End
System.out.println();
}
/** Outputs a given integer array.
@param s the string to go with it.
@param input the given array. */
private void outputIntArray(String s, int[] input) {
System.out.print(s + " = {");
if (input==null) {
System.out.println("}\n");
return;
}
// Loop
int puncCounter = 0;
int maxPerLine = 20;
for (int i=0;i<input.length;i++) {
if (puncCounter==maxPerLine) {
System.out.print(",\n\t");
puncCounter = 0;
}
else {
if (puncCounter>0) System.out.print(",");
puncCounter++;
}
System.out.print(input[i]);
}
// End
System.out.println("}\n");
}
/** Outputs the decision tree. */
public void outputDecTree() {
String nodeNum = "1";
outputDecTree(nodeNum,root);
}
/** Outpurts a decision tree node.
@param nodeNum the currentNumber.
@param node the current node. */
private void outputDecTree(String nodeNum, DecisionTreeNode node) {
if (node.posBranch==null) {
System.out.print(nodeNum + " Class = ");
if (node.attNumber==0) System.out.println("unknown");
else System.out.println(node.attNumber);
}
else {
System.out.println(nodeNum + " Node = " + node.attNumber);
outputDecTree(nodeNum+".1",node.posBranch);
outputDecTree(nodeNum+".2",node.negBranch);
}
}
/** Outputs a given short array.
@param s the string to go with it.
@param input the given array. */
private void outputIntArray(String s, short[] input) {
System.out.print(s + " = {");
if (input==null) {
System.out.println("}\n");
return;
}
// Loop
int puncCounter = 0;
int maxPerLine = 20;
for (int i=0;i<input.length;i++) {
if (puncCounter==maxPerLine) {
System.out.print(",\n\t");
puncCounter = 0;
}
else {
if (puncCounter>0) System.out.print(",");
puncCounter++;
}
System.out.print(input[i]);
}
// End
System.out.println("}\n");
}
/** Continues provess of outputting contents of rule binary (if any),
asuming that the list represents a set of CRs, such that last rule is the
default rule. <P>If field <TT>numRules</TT> is temporarily altered this
method can be used to output a particular rule as the default. Overides
similar method in AssocRuleMining.java.
@param linkRuleNode the currentNode. */
public void outputRulesWithDefault(RuleNode linkRuleNode) {
// Process node
if (linkRuleNode != null) {
// Check if at end
if (linkRuleNode.ruleNumber>numRules) return;
// Left branch
outputRulesWithDefault(linkRuleNode.leftBranch);
// Node
System.out.print("(" + linkRuleNode.ruleNumber + ") ");
if (linkRuleNode.ruleNumber==numRules) System.out.print("Default");
else outputItemSet(linkRuleNode.antecedent);
System.out.print(" -> ");
outputItemSet(linkRuleNode.consequent);
System.out.println();
// Right branch
outputRulesWithDefault(linkRuleNode.rightBranch);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -