📄 simplecart.java
字号:
return "CART Decision Tree\n" + toString(0)+"\n\n" +"Number of Leaf Nodes: "+numLeaves()+"\n\n" + "Size of the Tree: "+numNodes(); } /** * Outputs a tree at a certain level. * * @param level the level at which the tree is to be printed * @return a tree at a certain level */ protected String toString(int level) { StringBuffer text = new StringBuffer(); // if leaf nodes if (m_Attribute == null) { if (Instance.isMissingValue(m_ClassValue)) { text.append(": null"); } else { double correctNum = (int)(m_Distribution[Utils.maxIndex(m_Distribution)]*100)/ 100.0; double wrongNum = (int)((Utils.sum(m_Distribution) - m_Distribution[Utils.maxIndex(m_Distribution)])*100)/100.0; String str = "(" + correctNum + "/" + wrongNum + ")"; text.append(": " + m_ClassAttribute.value((int) m_ClassValue)+ str); } } else { for (int j = 0; j < 2; j++) { text.append("\n"); for (int i = 0; i < level; i++) { text.append("| "); } if (j==0) { if (m_Attribute.isNumeric()) text.append(m_Attribute.name() + " < " + m_SplitValue); else text.append(m_Attribute.name() + "=" + m_SplitString); } else { if (m_Attribute.isNumeric()) text.append(m_Attribute.name() + " >= " + m_SplitValue); else text.append(m_Attribute.name() + "!=" + m_SplitString); } text.append(m_Successors[j].toString(level + 1)); } } return text.toString(); } /** * Compute size of the tree. * * @return size of the tree */ public int numNodes() { if (m_isLeaf) { return 1; } else { int size =1; for (int i=0;i<m_Successors.length;i++) { size+=m_Successors[i].numNodes(); } return size; } } /** * Method to count the number of inner nodes in the tree. * * @return the number of inner nodes */ public int numInnerNodes(){ if (m_Attribute==null) return 0; int numNodes = 1; for (int i = 0; i < m_Successors.length; i++) numNodes += m_Successors[i].numInnerNodes(); return numNodes; } /** * Return a list of all inner nodes in the tree. * * @return the list of all inner nodes */ protected Vector getInnerNodes(){ Vector nodeList = new Vector(); fillInnerNodes(nodeList); return nodeList; } /** * Fills a list with all inner nodes in the tree. * * @param nodeList the list to be filled */ protected void fillInnerNodes(Vector nodeList) { if (!m_isLeaf) { nodeList.add(this); for (int i = 0; i < m_Successors.length; i++) m_Successors[i].fillInnerNodes(nodeList); } } /** * Compute number of leaf nodes. * * @return number of leaf nodes */ public int numLeaves() { if (m_isLeaf) return 1; else { int size=0; for (int i=0;i<m_Successors.length;i++) { size+=m_Successors[i].numLeaves(); } return size; } } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result; Enumeration en; result = new Vector(); en = super.listOptions(); while (en.hasMoreElements()) result.addElement(en.nextElement()); result.addElement(new Option( "\tThe minimal number of instances at the terminal nodes.\n" + "\t(default 2)", "M", 1, "-M <min no>")); result.addElement(new Option( "\tThe number of folds used in the minimal cost-complexity pruning.\n" + "\t(default 5)", "N", 1, "-N <num folds>")); result.addElement(new Option( "\tDon't use the minimal cost-complexity pruning.\n" + "\t(default yes).", "U", 0, "-U")); result.addElement(new Option( "\tDon't use the heuristic method for binary split.\n" + "\t(default true).", "H", 0, "-H")); result.addElement(new Option( "\tUse 1 SE rule to make pruning decision.\n" + "\t(default no).", "A", 0, "-A")); result.addElement(new Option( "\tPercentage of training data size (0-1].\n" + "\t(default 1).", "C", 1, "-C")); return result.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -S <num> * Random number seed. * (default 1)</pre> * * <pre> -D * If set, classifier is run in debug mode and * may output additional info to the console</pre> * * <pre> -M <min no> * The minimal number of instances at the terminal nodes. * (default 2)</pre> * * <pre> -N <num folds> * The number of folds used in the minimal cost-complexity pruning. * (default 5)</pre> * * <pre> -U * Don't use the minimal cost-complexity pruning. * (default yes).</pre> * * <pre> -H * Don't use the heuristic method for binary split. * (default true).</pre> * * <pre> -A * Use 1 SE rule to make pruning decision. * (default no).</pre> * * <pre> -C * Percentage of training data size (0-1]. * (default 1).</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an options is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; super.setOptions(options); tmpStr = Utils.getOption('M', options); if (tmpStr.length() != 0) setMinNumObj(Double.parseDouble(tmpStr)); else setMinNumObj(2); tmpStr = Utils.getOption('N', options); if (tmpStr.length()!=0) setNumFoldsPruning(Integer.parseInt(tmpStr)); else setNumFoldsPruning(5); setUsePrune(!Utils.getFlag('U',options)); setHeuristic(!Utils.getFlag('H',options)); setUseOneSE(Utils.getFlag('A',options)); tmpStr = Utils.getOption('C', options); if (tmpStr.length()!=0) setSizePer(Double.parseDouble(tmpStr)); else setSizePer(1); Utils.checkForRemainingOptions(options); } /** * Gets the current settings of the classifier. * * @return the current setting of the classifier */ public String[] getOptions() { int i; Vector result; String[] options; result = new Vector(); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); result.add("-M"); result.add("" + getMinNumObj()); result.add("-N"); result.add("" + getNumFoldsPruning()); if (!getUsePrune()) result.add("-U"); if (!getHeuristic()) result.add("-H"); if (getUseOneSE()) result.add("-A"); result.add("-C"); result.add("" + getSizePer()); return (String[]) result.toArray(new String[result.size()]); } /** * Return an enumeration of the measure names. * * @return an enumeration of the measure names */ public Enumeration enumerateMeasures() { Vector result = new Vector(); result.addElement("measureTreeSize"); return result.elements(); } /** * Return number of tree size. * * @return number of tree size */ public double measureTreeSize() { return numNodes(); } /** * Returns the value of the named measure. * * @param additionalMeasureName the name of the measure to query for its value * @return the value of the named measure * @throws IllegalArgumentException if the named measure is not supported */ public double getMeasure(String additionalMeasureName) { if (additionalMeasureName.compareToIgnoreCase("measureTreeSize") == 0) { return measureTreeSize(); } else { throw new IllegalArgumentException(additionalMeasureName + " not supported (Cart pruning)"); } } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String minNumObjTipText() { return "The minimal number of observations at the terminal nodes (default 2)."; } /** * Set minimal number of instances at the terminal nodes. * * @param value minimal number of instances at the terminal nodes */ public void setMinNumObj(double value) { m_minNumObj = value; } /** * Get minimal number of instances at the terminal nodes. * * @return minimal number of instances at the terminal nodes */ public double getMinNumObj() { return m_minNumObj; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numFoldsPruningTipText() { return "The number of folds in the internal cross-validation (default 5)."; } /** * Set number of folds in internal cross-validation. * * @param value number of folds in internal cross-validation. */ public void setNumFoldsPruning(int value) { m_numFoldsPruning = value; } /** * Set number of folds in internal cross-validation. * * @return number of folds in internal cross-validation. */ public int getNumFoldsPruning() { return m_numFoldsPruning; } /** * Return the tip text for this property * * @return tip text for this property suitable for displaying in * the explorer/experimenter gui. */ public String usePruneTipText() { return "Use minimal cost-complexity pruning (default yes)."; } /** * Set if use minimal cost-complexity pruning. * * @param value if use minimal cost-complexity pruning */ public void setUsePrune(boolean value) { m_Prune = value; } /** * Get if use minimal cost-complexity pruning. * * @return if use minimal cost-complexity pruning */ public boolean getUsePrune() { return m_Prune; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui. */ public String heuristicTipText() { return "If heuristic search is used for binary split for nominal attributes " + "in multi-class problems (default yes)."; } /** * Set if use heuristic search for nominal attributes in multi-class problems. * * @param value if use heuristic search for nominal attributes in * multi-class problems */ public void setHeuristic(boolean value) { m_Heuristic = value; } /** * Get if use heuristic search for nominal attributes in multi-class problems. * * @return if use heuristic search for nominal attributes in * multi-class problems */ public boolean getHeuristic() {return m_Heuristic;} /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui. */ public String useOneSETipText() { return "Use the 1SE rule to make pruning decisoin."; } /** * Set if use the 1SE rule to choose final model. * * @param value if use the 1SE rule to choose final model */ public void setUseOneSE(boolean value) { m_UseOneSE = value; } /** * Get if use the 1SE rule to choose final model. * * @return if use the 1SE rule to choose final model */ public boolean getUseOneSE() { return m_UseOneSE; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui. */ public String sizePerTipText() { return "The percentage of the training set size (0-1, 0 not included)."; } /** * Set training set size. * * @param value training set size */ public void setSizePer(double value) { if ((value <= 0) || (value > 1)) System.err.println( "The percentage of the training set size must be in range 0 to 1 " + "(0 not included) - ignored!"); else m_SizePer = value; } /** * Get training set size. * * @return training set size */ public double getSizePer() { return m_SizePer; } /** * Main method. * @param args the options for the classifier */ public static void main(String[] args) { runClassifier(new SimpleCart(), args); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -