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

📄 classifiertree.java

📁 一个数据挖掘系统的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                propatores.add(elementFather);
                Element temPredicate = pmmlSimplePredicate (field, operator, value);
                babisElement.addContent(temPredicate);
                elementFather = babisElement;
            }
            else {
                Element babisElement = newNode();
                Element temPredicate = pmmlSimplePredicate (field, operator, value);
                Element tempLeaf = newLeaf(resultSubstring);
                babisElement.addContent(temPredicate);
                babisElement.addContent(tempLeaf);
                elementFather.addContent(babisElement);
//              Ean Oi propatores einai kenoi tote min tre3eis to loop
                if (propatores.size() != 0){
                  Element newChild = elementFather;
//                  System.out.println("Diafora = " + substract);
//                  System.out.println("Vector size = " + propatores.size());
                  int afair;
                  if (substract < propatores.size()){
                    afair = substract;
                  }
                  else {
                    afair= propatores.size();

                  }
                  for (int i = 0 ; i<afair; i++){
                      elementFather = (Element) propatores.lastElement();
                      elementFather.addContent(newChild);
                      propatores.removeElementAt(propatores.indexOf(propatores.lastElement()));
                      newChild = elementFather;
                  }
                }
            } // and of else
      } //end of for
   predicateElement = elementFather;
   return predicateElement;
   }

/**
 * Create new Node Element
 */
 private Element newNode () {
    Element newNodeElement = new Element ("Node");
    newNodeElement.setAttribute("score", "play");
    return newNodeElement;
 }

 /**
  * Create new leaf
  */
 private Element newLeaf (String resultSubstring) throws Exception {

            String result = resultSubstring;
            Element leafElement = new Element ("Node");
            Element trueElement = new Element ("TRUE");
            leafElement.setAttribute("score",result);
            leafElement.addContent(trueElement);
            return leafElement;
   }

  /**
   * New class for creating a PMML output
   * @exception Exception if something goes wrong
   *
   */
  private void pmmlPredicate(int depth) throws Exception {

    int j = depth;
    String pmmlTreeSimplePredicateString = "";
    String pmmlTreeCompoundPredicateString = "";
    String pmmlTreeField = "";
    String pmmlTreeOperator = "";
    String pmmlTreeValue = "";
    String attributeValueRight = "";
    String resultOutcome = "";
    String resultOutcomeSubstring = "";

    for (int i=0;i<m_sons.length;i++) {
        pmmlTreeField = m_localModel.leftSide(m_train);
        attributeValueRight =m_localModel.rightSide(i, m_train);
        resultOutcome = m_localModel.dumpLabel(i, m_train);
        resultOutcomeSubstring = resultOutcome.substring(0,(resultOutcome.indexOf("(")-1));
        if (attributeValueRight.startsWith(" =")){
          pmmlTreeOperator = "equal";
          pmmlTreeValue = attributeValueRight.substring(3);
        }
        else if (attributeValueRight.startsWith(" <=")){
          pmmlTreeOperator = "lessOrEqual";
          pmmlTreeValue = attributeValueRight.substring(4);
        }
        else if (attributeValueRight.startsWith(" <")){
          pmmlTreeOperator = "lessThan";
          pmmlTreeValue = attributeValueRight.substring(3);
        }
        else if (attributeValueRight.startsWith(" >=")){
          pmmlTreeOperator = "greaterOrEqual";
          pmmlTreeValue = attributeValueRight.substring(4);
        }
        else if (attributeValueRight.startsWith(" >")){
          pmmlTreeOperator = "greaterThan";
          pmmlTreeValue = attributeValueRight.substring(3);
        }
        else {
          pmmlTreeOperator = "notEqual";
          pmmlTreeValue = attributeValueRight.substring(4);
        }
//        System.out.println("Predicate Rule =" + pmmlTreeField + pmmlTreeOperator + pmmlTreeValue);

//        System.out.println("Mia fora");
        ruleVector.addElement(pmmlTreeField);
        ruleVector.addElement(pmmlTreeOperator);
        ruleVector.addElement(pmmlTreeValue);
        ruleVector.addElement(String.valueOf(depth));
        ruleVector.addElement(resultOutcomeSubstring);
//        System.out.println("Mia fora");

        // If depth ==0 then we have finished with the first subtrees

        if (depth ==0){
//          System.out.println("Twra allazw kanona");
        }

        // In order to construct the treeModel document we have to check whether it is the first child
        // or not and call the appropriate method
/*

          if (currentNodeElement == null){
              System.out.println("MPOUFO!!!");
          }
          else{
//            currentNodeElement.addContent(pmmlNextElement(pmmlTreeField, pmmlTreeOperator, pmmlTreeValue, i, depth));
          }
*/
      //If the node is not a Leaf

       if (m_sons[i].m_isLeaf == false) {
          m_sons[i].pmmlPredicate(depth+1);
       }

    }

  }

 /**
   * Creates a Simple Predicate for the first rule
   */

  private Element pmmlSimplePredicateFirst (String Field, String Operator, String Value) {

      String pmmlTreeField = Field;
      String pmmlTreeOperator = Operator;
      String pmmlTreeValue = Value;

      Element simplePredicateElementFirst = new Element ("Node");

//      simplePredicateElementFirst.setAttribute("field", pmmlTreeField);
//      simplePredicateElementFirst.setAttribute("operator", pmmlTreeOperator);
//      simplePredicateElementFirst.setAttribute("value", pmmlTreeValue);

      return simplePredicateElementFirst;
      }
  /**
   * Creates a Simple Predicate for the next rules
   */

  private Element pmmlSimplePredicate (String Field, String Operator, String Value) {

      String pmmlTreeField = Field;
      String pmmlTreeOperator = Operator;
      String pmmlTreeValue = Value;

      Element simplePredicateElement = new Element ("SimplePredicate");
      simplePredicateElement.setAttribute("field", pmmlTreeField);
      simplePredicateElement.setAttribute("operator", pmmlTreeOperator);
      simplePredicateElement.setAttribute("value", pmmlTreeValue);

      return simplePredicateElement;
      }
  /**
   * Help method for printing tree structure.
   *
   * @exception Exception if something goes wrong
   */
  private void dumpTree(int depth,StringBuffer text)
       throws Exception {

    int i,j;
    for (i=0;i<m_sons.length;i++) {
      text.append("\n");;
      for (j=0;j<depth;j++)
	text.append("|   ");
      text.append(m_localModel.leftSide(m_train));
      text.append(m_localModel.rightSide(i, m_train));

      if (m_sons[i].m_isLeaf) {
	text.append(": ");
	text.append(m_localModel.dumpLabel(i,m_train));

      }
      else
	m_sons[i].dumpTree(depth+1,text);
    }
  }


  /**
   * Help method for printing tree structure as a graph.
   *
   * @exception Exception if something goes wrong
   */
  private void graphTree(StringBuffer text) throws Exception {

    for (int i = 0; i < m_sons.length; i++) {
      text.append("N" + m_id
		  + "->" +
		  "N" + m_sons[i].m_id +
		  " [label=\"" + m_localModel.rightSide(i,m_train).trim() +
		  "\"]\n");
      if (m_sons[i].m_isLeaf) {
	text.append("N" + m_sons[i].m_id +
		    " [label=\""+m_localModel.dumpLabel(i,m_train)+"\" "+
		    "shape=box style=filled ");
	if (m_train != null && m_train.numInstances() > 0) {
	  text.append("data =\n" + m_sons[i].m_train + "\n");
	  text.append(",\n");
	}
	text.append("]\n");
      } else {
	text.append("N" + m_sons[i].m_id +
		    " [label=\""+m_sons[i].m_localModel.leftSide(m_train) +
		    "\" ");
	if (m_train != null && m_train.numInstances() > 0) {
	  text.append("data =\n" + m_sons[i].m_train + "\n");
	  text.append(",\n");
	}
	text.append("]\n");
	m_sons[i].graphTree(text);
      }
    }
  }

  /**
   * Prints the tree in prefix form
   */
  private void prefixTree(StringBuffer text) throws Exception {

    text.append("[");
    text.append(m_localModel.leftSide(m_train)+":");
    for (int i = 0; i < m_sons.length; i++) {
      if (i > 0) {
	text.append(",\n");
      }
      text.append(m_localModel.rightSide(i, m_train));
    }
    for (int i = 0; i < m_sons.length; i++) {
      if (m_sons[i].m_isLeaf) {
	text.append("[");
	text.append(m_localModel.dumpLabel(i,m_train));
	text.append("]");
      } else {
	m_sons[i].prefixTree(text);
      }
    }
    text.append("]");
  }

  /**
   * Help method for computing class probabilities of
   * a given instance.
   *
   * @exception Exception if something goes wrong
   */
  private double getProbsLaplace(int classIndex, Instance instance, double weight)
    throws Exception {

    double [] weights;
    double prob = 0;
    int treeIndex;
    int i,j;

    if (m_isLeaf) {
      return weight * localModel().classProbLaplace(classIndex, instance, -1);
    } else {
      treeIndex = localModel().whichSubset(instance);
      if (treeIndex == -1) {
	weights = localModel().weights(instance);
	for (i = 0; i < m_sons.length; i++) {
	  if (!son(i).m_isEmpty) {
	    if (!son(i).m_isLeaf) {
	      prob += son(i).getProbsLaplace(classIndex, instance,
					     weights[i] * weight);
	    } else {
	      prob += weight * weights[i] *
		localModel().classProbLaplace(classIndex, instance, i);
	    }
	  }
	}
	return prob;
      } else {
	if (son(treeIndex).m_isLeaf) {
	  return weight * localModel().classProbLaplace(classIndex, instance,
							treeIndex);
	} else {
	  return son(treeIndex).getProbsLaplace(classIndex, instance, weight);
	}
      }
    }
  }

  /**
   * Help method for computing class probabilities of
   * a given instance.
   *
   * @exception Exception if something goes wrong
   */
  private double getProbs(int classIndex, Instance instance, double weight)
    throws Exception {

    double [] weights;
    double prob = 0;
    int treeIndex;
    int i,j;

    if (m_isLeaf) {
      return weight * localModel().classProb(classIndex, instance, -1);
    } else {
      treeIndex = localModel().whichSubset(instance);
      if (treeIndex == -1) {
	weights = localModel().weights(instance);
	for (i = 0; i < m_sons.length; i++) {
	  if (!son(i).m_isEmpty) {
	    prob += son(i).getProbs(classIndex, instance,
				    weights[i] * weight);
	  }
	}
	return prob;
      } else {
	if (son(treeIndex).m_isEmpty) {
	  return weight * localModel().classProb(classIndex, instance,
						 treeIndex);
	} else {
	  return son(treeIndex).getProbs(classIndex, instance, weight);
	}
      }
    }
  }

  /**
   * Method just exists to make program easier to read.
   */
  private ClassifierSplitModel localModel() {

    return (ClassifierSplitModel)m_localModel;
  }

  /**
   * Method just exists to make program easier to read.
   */
  private ClassifierTree son(int index) {

    return (ClassifierTree)m_sons[index];
  }
}

⌨️ 快捷键说明

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