⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifiertree.java

📁 一个数据挖掘系统的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   *
   * @param className the classname that this static classifier has
   * @return an array containing two stringbuffers, the first string containing
   * assignment code, and the second containing source for support code.
   * @exception Exception if something goes wrong
   */
  public StringBuffer [] toSource(String className) throws Exception {

    StringBuffer [] result = new StringBuffer [2];
    if (m_isLeaf) {
      result[0] = new StringBuffer("    p = "
	+ m_localModel.distribution().maxClass(0) + ";\n");
      result[1] = new StringBuffer("");
    } else {
      StringBuffer text = new StringBuffer();
      String nextIndent = "      ";
      StringBuffer atEnd = new StringBuffer();

      long printID = ClassifierTree.nextID();

      text.append("  static double N")
	.append(Integer.toHexString(m_localModel.hashCode()) + printID)
	.append("(Object []i) {\n")
	.append("    double p = Double.NaN;\n");

      text.append("    if (")
	.append(m_localModel.sourceExpression(-1, m_train))
	.append(") {\n");
      text.append("      p = ")
	.append(m_localModel.distribution().maxClass(0))
	.append(";\n");
      text.append("    } ");
      for (int i = 0; i < m_sons.length; i++) {
	text.append("else if (" + m_localModel.sourceExpression(i, m_train)
		    + ") {\n");
	if (m_sons[i].m_isLeaf) {
	  text.append("      p = "
		      + m_localModel.distribution().maxClass(i) + ";\n");
	} else {
	  StringBuffer [] sub = m_sons[i].toSource(className);
	  text.append(sub[0]);
	  atEnd.append(sub[1]);
	}
	text.append("    } ");
	if (i == m_sons.length - 1) {
	  text.append('\n');
	}
      }

      text.append("    return p;\n  }\n");

      result[0] = new StringBuffer("    p = " + className + ".N");
      result[0].append(Integer.toHexString(m_localModel.hashCode()) +  printID)
	.append("(i);\n");
      result[1] = text.append(atEnd);
    }
    return result;
  }

  /**
   * Returns number of leaves in tree structure.
   */
  public int numLeaves() {

    int num = 0;
    int i;

    if (m_isLeaf)
      return 1;
    else
      for (i=0;i<m_sons.length;i++)
	num = num+m_sons[i].numLeaves();

    return num;
  }

  /**
   * Returns number of nodes in tree structure.
   */
  public int numNodes() {

    int no = 1;
    int i;

    if (!m_isLeaf)
      for (i=0;i<m_sons.length;i++)
	no = no+m_sons[i].numNodes();

    return no;
  }

  /**
   * Prints tree structure.
   */
  public String toString() {

    try {
      StringBuffer text = new StringBuffer();

      if (m_isLeaf) {
	text.append(": ");
        text.append(m_localModel.dumpLabel(0,m_train));
      }
      else
      pmmlDocument(0);
      dumpTree(0,text);
      text.append("\n\nNumber of Leaves : \t"+numLeaves()+"\n");
      text.append("\nSize of the tree : \t"+numNodes()+"\n");
      return text.toString();

    } catch (Exception e) {
      System.out.print(e.getMessage());
      return "Can't print classification tree.";
    }
  }

  /**
   * Returns a newly created tree.
   *
   * @param data the training data
   * @exception Exception if something goes wrong
   */
  protected ClassifierTree getNewTree(Instances data) throws Exception{

    ClassifierTree newTree = new ClassifierTree(m_toSelectModel);
    newTree.buildTree(data, false);

    return newTree;
  }

  /**
   * Returns a newly created tree.
   *
   * @param data the training data
   * @param test the pruning data.
   * @exception Exception if something goes wrong
   */
  protected ClassifierTree getNewTree(Instances train, Instances test)
       throws Exception{

    ClassifierTree newTree = new ClassifierTree(m_toSelectModel);
    newTree.buildTree(train, test, false);

    return newTree;
  }

  /**
   * Creates the PMML Document
   * @param depth that specified the depth of the decision tree we are currently traversing
   * @exception e if the PMML document is not correctly constructed
   */

   private void pmmlDocument (int depth) {
      try{
        Element versionElement = pmmlIntro();
        Element headerElement = header ();
        Element dataDictionaryElement = dataDictionary () ;
        Element treeModelElement = treeModel();
        Element miningSchemaElement = miningSchema ();
        versionElement.addContent(headerElement);
        versionElement.addContent(dataDictionaryElement);
        versionElement.addContent(treeModelElement);
        treeModelElement.addContent(miningSchemaElement);
        pmmlPredicate(depth);
        treeModelElement.addContent(createPredicates());
       // Create the XML document
        DocType dtd = new DocType("pmml_2_0.dtd");
        pmmlDocument = new Document (versionElement,dtd);
     }
      catch (Exception e){
        log.error("PMML Document Exception: " + e);
      }
   }

   /**
    * Creates the pmml Element
    * @exception e if the initial node creation for the PMML document goes wrong
    */
   private Element pmmlIntro () throws Exception {

      Element pmmlIntro = new Element ("PMML");
      return pmmlIntro;
   }

  /**
   * Create the Header Element
   */
   private Element header () throws Exception {

      Element header = new Element ("Header");
      String headerString = " The tree model of Data Mined Data";
//      headerString  = headerString.concat(m_trainrelationName());
//      headerString  += m_train.relationName();


      header.setAttribute("copyright", "issel.ee.auth.gr");
      header.setAttribute("description", headerString);
      Element applicationNameElement = new Element ("Application");
      applicationNameElement.setAttribute("name", "Agent Academy Data Miner");
      applicationNameElement.setAttribute("version", "0.3");
      header.addContent(applicationNameElement);

   return header;
   }

   /**
    *  Create the DataDictionary for the pre - specified XML file
    *  @exception e if the DataDictionary is not correctly created
    */
    private Element dataDictionary () throws Exception {

      Element dataDictionary = new Element ("DataDictionary");
      Element dataFieldElement;
      Element attributeValueElement;
      FastVector headerVector = m_train.getVector();
      String attributeName;
      String attributeType;
      String attributeValueString;
      int numberOfFields = m_train.numAttributes();
      String numberOfFieldsString = String.valueOf(numberOfFields);
      dataDictionary.setAttribute("numberOfFields", numberOfFieldsString );
      for (int i=0; i < headerVector.size();i++){
        dataFieldElement = new Element ("DataField");
        org.agentacademy.modules.dataminer.core.Attribute a = null;
        a = (org.agentacademy.modules.dataminer.core.Attribute) headerVector.elementAt(i);
        attributeName = a.name();
        dataFieldElement.setAttribute("name", attributeName);
        if (a.isNominal()){
          attributeType = "categorical";
          dataFieldElement.setAttribute("optype",attributeType);
          //
          Enumeration enumerateValues = a.enumerateValues();
          while (enumerateValues.hasMoreElements()){
              attributeValueString = (String)enumerateValues.nextElement();
              attributeValueElement = new Element ("Value");
              attributeValueElement.setAttribute("value", attributeValueString);
              dataFieldElement.addContent(attributeValueElement);
          }
        }
        else if (a.isNumeric()){
          attributeType = "continuous";
          dataFieldElement.setAttribute("optype",attributeType);
        }
        else if (a.isRegular()){
          attributeType = "ordinal";
          dataFieldElement.setAttribute("optype",attributeType);
        }
        else {
          attributeType = "string";
          dataFieldElement.setAttribute("optype",attributeType);
        }
      dataDictionary.addContent(dataFieldElement);
      }
    return dataDictionary;
    }

   /**
    * Creates the TreeModel Element
    */
    private Element treeModel () throws Exception{

        Element treeModel = new Element ("TreeModel");
        String modelNameString = "Data Mined Data";
        treeModel.setAttribute("modelName", modelNameString);

    return treeModel;
    }

   /**
    * Creates the Mining Schema Element
    */

   private Element miningSchema () throws Exception {

       Element miningSchema = new Element ("MiningSchema");
       String attributeName;
       FastVector miningSchemaVector = m_train.getVector();
       for (int i=0; i < miningSchemaVector.size();i++){
          Element miningFieldElement = new Element ("MiningField");
          org.agentacademy.modules.dataminer.core.Attribute a = (org.agentacademy.modules.dataminer.core.Attribute) miningSchemaVector.elementAt(i);
          attributeName = a.name();
          miningFieldElement.setAttribute("name", attributeName);
          if (m_train.classIndex() == i){
            miningFieldElement.setAttribute("usageType", "predicted");
          }
          else{
            miningFieldElement.setAttribute("usageType", "active");
          }
          miningSchema.addContent(miningFieldElement);
      }
   return miningSchema;
   }

   /**
    *   Create Predicates using a Vector that finds that last leaf and returns
    *
    */
   private Element createPredicates () throws Exception {
      Element predicateElement = newNode();
      Element elementFather;
      int ruleDepthIndex;
      int nextRuleDepthIndex;
      String field;
      String operator;
      String value;
      String resultSubstring;
      String field1, operator1, value1, resultSubstring1;
      Vector propatores = new Vector();
      int firstDepth = Integer.parseInt((String) ruleVector.elementAt(3));
      int secondDepth = Integer.parseInt((String) ruleVector.elementAt(8));
      int routing;
      field1 = (String)ruleVector.elementAt(0);
      operator1 = (String)ruleVector.elementAt(1);
      value1 = (String)ruleVector.elementAt(2);
      resultSubstring1 = (String)ruleVector.elementAt(4);
      // If the first Element has a Leaf then create the SimplePredicate, the Leaf
      // and input all these to the elementFather

      if (firstDepth == secondDepth){

        Element prwtoElement = pmmlSimplePredicate (field1,operator1, value1);
        predicateElement.addContent(prwtoElement);
        predicateElement.addContent(newLeaf(resultSubstring1));
        elementFather = predicateElement;
        routing = 5;
//        System.out.println("Ama pearsw apo edw malakia mou");
      }
      else {

        Element prwtoElement = pmmlSimplePredicate (field1,operator1, value1);
        predicateElement.addContent(prwtoElement);
        elementFather = predicateElement;
        routing = 5;
//        System.out.println("Kanw ena mesa");
      }
      for (int counter = routing; counter < ruleVector.size(); counter = counter + 5){
            ruleDepthIndex = counter + 3;
            nextRuleDepthIndex = counter + 8;
            field = (String)ruleVector.elementAt(counter);
//            System.out.print(field);
            operator = (String)ruleVector.elementAt(counter + 1);
//            System.out.print(operator);
            value = (String)ruleVector.elementAt(counter + 2);
//            System.out.print(value);
            resultSubstring = (String)ruleVector.elementAt(counter + 4);
//            System.out.print("\n");
            int nextRuleDepth;
            int ruleDepth = Integer.parseInt((String) ruleVector.elementAt(ruleDepthIndex)) ;
            if (nextRuleDepthIndex < ruleVector.size()){
               nextRuleDepth = Integer.parseInt((String) ruleVector.elementAt(nextRuleDepthIndex)) ;
            }
            else{
               nextRuleDepth = 0;
            }
            int substract = ruleDepth - nextRuleDepth ;
            if (substract == 0){
                Element babisElement = newNode();
                Element temPredicate = pmmlSimplePredicate (field, operator, value);
                Element tempLeaf = newLeaf(resultSubstring);
                babisElement.addContent(temPredicate);
                babisElement.addContent(tempLeaf);
                elementFather.addContent(babisElement);
            }
            else if (substract == -1){
                Element babisElement = newNode();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -