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

📄 decisiontreenode.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      return totalRecordCountOther;
    }

    /**
     * Set node total record count.
     *
     * @param totalRecordCountOther distribution
     */
    public void setTotalRecordCountOther(double[] totalRecordCountOther)
    {
      this.totalRecordCountOther = totalRecordCountOther;
    }

    /**
     *
     * Get node cumulated record count for target.
     *
     * @param target index of target
     * @return count
     */
    public double getCumulatedRecordCountThis(double target)
    {
      return cumulatedRecordCountThis[(int)target];
    }

    /**
     * Get node cumulated record count for not target
     *
     * @param target index of target
     * @return count
     */
    double getCumulatedRecordCountOther(double target)
    {
      return cumulatedRecordCountOther[(int)target];
    }

    /**
     *
     * Set node cumulated record counts.
     *
     * @param target index of target
     * @param countThis cumulated count for target
     * @param countOther cumulated count for not target
     */
    public void setCumulatedRecordCounts(double target, double countThis, double countOther)
    {
      cumulatedRecordCountThis[(int)target]  = countThis;
      cumulatedRecordCountOther[(int)target] = countOther;
    }

    // -----------------------------------------------------------------------
    //  Apply tree node to mining vector
    // -----------------------------------------------------------------------
    /**
     * Applies decision tree recursively to all child nodes
     * and returns score value.
     *
     * @param miningVector vector to be classified
     * @return score value of classfication
     * @throws MiningException could not run this method
     */
    public double apply( MiningVector miningVector ) throws MiningException
    {
      return ( applyForNode(miningVector) ).score;
    }
    
    //<<17/03/2005, Frank J. Xu
    //Do not add any changes for the current vesion.
    // -----------------------------------------------------------------------
    //  Apply tree node to mining vector
    // -----------------------------------------------------------------------
    /**
     * Applies decision tree recursively to all child nodes
     * and returns score value.
     *
     * @param miningVector vector to be classified
     * @return score value of classfication
     * @throws MiningException could not run this method
     */
    public double apply( MiningVector miningVector, Object a_wekaInstances) throws MiningException
    {
      return ( applyForNode(miningVector) ).score;
    }    
    //17/03/2005, Frank J. Xu>>

    /**
     * Applies decision tree recursively to all child nodes
     * and returns reference to scoring node.
     *
     * @param miningVector vector to be classified
     * @return node containing score information used for classification
     * @throws MiningException could not run this method
     */
    public DecisionTreeNode applyForNode(MiningVector miningVector) throws MiningException
    {
        if (leaf || children == null) return this;
        else
        {
          for(int i=0;i<children.length;i++)
          {
            DecisionTreeNode dtn = (DecisionTreeNode)children[i];
            if(dtn.predicate.evaluate(miningVector)) return dtn.applyForNode(miningVector);
          }

          throw new MiningException("bad tree model");
        }
    }

    // -----------------------------------------------------------------------
    //  Methods of PMML handling
    // -----------------------------------------------------------------------
    /**
     * Writes PMML Node.
     *
     * @return pmmlObject PMML Node
     * @exception MiningException could not create PMML object
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.Node
     */
    public Object createPmmlObject() throws MiningException
    {
        Node pmmlNode = new Node();

        // Set score value:
        String predictedScore = getScoreString();
        pmmlNode.setScore(predictedScore);

        // Add score distribution and record count:
        if (distribution != null) {
          int nclass             = distribution.length;
          double nvec            = 0;
          ScoreDistribution[] sd = new ScoreDistribution[nclass];
          for (int i = 0; i < nclass; i++) {
            sd[i] = new ScoreDistribution();
            sd[i].setRecordCount( String.valueOf(distribution[i]) );
            nvec  = nvec + distribution[i];

            String value = "";
            if (distribCategNames != null && distribCategNames.length == nclass)
              value = distribCategNames[i];
            else {
              if (target instanceof NumericAttribute)
                value = "v" + String.valueOf(i);
              else
                value = ((CategoricalAttribute)target).getCategory(i).getDisplayValue();
            }
            sd[i].setValue(value);
          };
          pmmlNode.setScoreDistribution(sd);
          pmmlNode.setRecordCount( String.valueOf(nvec) );
        };

        // Add predicate:
        addPredicate( pmmlNode );

        // Add regression model if regression mode, and child nodes:
        if( !isLeaf() )
        {
            // Add regression model if regression node:
            if ( this instanceof RegressionTreeNode ) {
              Object model = ((RegressionTreeNode) this).createPmmlObjectModel();
              RegressionTreeNode.setPmmlRegressionModel(model, pmmlNode);
            };

            // Add child nodes:
            int n = getChildCount();
            Node[] childNode = new Node[n];
            for( int i = 0; i < n; i++ )
            {
                DecisionTreeNode child = (DecisionTreeNode)getChildAt( i );
                childNode[i] = (Node)child.createPmmlObject();
            }
            pmmlNode.setNode( childNode );
        }
        else
        {
        };

        return pmmlNode;
    }

    /**
     * Reads PMML Node.
     *
     * @param pmmlObject PMML Node
     * @exception MiningException could not parse PMML object
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.Node
     */
    public void parsePmmlObject( Object pmmlObject ) throws MiningException
    {
        Node node = (Node)pmmlObject;

        // Get score value:
        String predictedScore = node.getScore();
        if (target instanceof CategoricalAttribute)
        {
          score = ((CategoricalAttribute)target).getKey( new Category(predictedScore) );
        }
        else
        {
          try {
            score = Double.parseDouble( predictedScore );
          }
          catch(NumberFormatException ex) {
            score = Category.MISSING_VALUE;
          }
        };

        // Get predicate:
        Predicate pred = null;
        Object pval    = null;
        com.prudsys.pdm.Adapters.PmmlVersion20.True tp = node.getTrue();
        com.prudsys.pdm.Adapters.PmmlVersion20.False fp = node.getFalse();
        com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate sp = node.getSimplePredicate();
        com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate ssp = node.getSimpleSetPredicate();
        com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate cp = node.getCompoundPredicate();
        if (tp != null) {
          pred = new ConstantPredicate();
          pval = tp;
        }
        else if (fp != null) {
          pred = new ConstantPredicate();
          pval = fp;
        }
        else if (sp != null)
        {
          pred = new com.prudsys.pdm.Input.Predicates.SimplePredicate();
          pval = sp;
        }
        else if (ssp != null)
        {
          pred = new com.prudsys.pdm.Input.Predicates.SimpleSetPredicate();
          pval = ssp;
        }
        else if (cp != null)
        {
          pred = new com.prudsys.pdm.Input.Predicates.CompoundPredicate();
          pval = cp;
        }
        pred.setMetaData(metaData);
        pred.parsePmmlObject(pval);
        setPredicate(pred);

        // Get score distribution:
        ScoreDistribution[] sd = node.getScoreDistribution();
        if (sd != null) {
          int nclass   = sd.length;
          distribCategNames = new String[nclass];
          distribution = new double[nclass];
          for (int i = 0; i < nclass; i++) {
              try {
                distribCategNames[i] = sd[i].getValue();
                distribution[i] =  Double.parseDouble( sd[i].getRecordCount() );
              }
              catch (Exception ex) {};
          };
        };

        // Parse all child nodes:
        Node[] child = node.getNode();
        if( child.length != 0 )
        {
            DecisionTreeNode[] treeNodes = new DecisionTreeNode[child.length];
            for (int i = 0; i < child.length; i++)
            {
              treeNodes[i] = new DecisionTreeNode();

              // Check if contains regression model => create regression node:
              Object model = RegressionTreeNode.getPmmlRegressionModel(child[i]);
              if (model != null) {
                treeNodes[i] = new RegressionTreeNode();
                decisionTreeModel.setNonlinear(true);
              };

              treeNodes[i].setParent( this );
              treeNodes[i].setDecisionTreeModel(decisionTreeModel);
              treeNodes[i].setMetaData(metaData);
              treeNodes[i].setTarget(target);
              if (model != null) ((RegressionTreeNode) treeNodes[i]).parsePmmlObjectModel(model);
              treeNodes[i].parsePmmlObject( child[i] );
            }
            children = treeNodes;
        }
        else
        {
            setLeaf( true );
        };
    }

    /**
     * Adds PMML PREDICATE to given PMML node.
     *
     * @param node node for predicate to add
     * @throws MiningException could not add predicate
     */
    private void addPredicate( Node node ) throws MiningException
    {
        if( getParent() == null )
        {
            node.setTrue( new True() );
        }
        else
        {
            com.borland.xml.toolkit.XmlObject pmml = (com.borland.xml.toolkit.XmlObject)predicate.createPmmlObject();
            if(predicate instanceof ConstantPredicate)
            {
              if(((ConstantPredicate)predicate).getConstant()) node.setTrue(new True());
              else node.setFalse(new False());
            }
            else if(predicate instanceof com.prudsys.pdm.Input.Predicates.SimplePredicate)
            {
              node.setSimplePredicate((com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate)pmml);
            }
            else if(predicate instanceof com.prudsys.pdm.Input.Predicates.SimpleSetPredicate)
            {
              node.setSimpleSetPredicate((com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate)pmml);
            }
            else if(predicate instanceof com.prudsys.pdm.Input.Predicates.CompoundPredicate)
            {
              node.setCompoundPredicate((com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate)pmml);
            }
        }
    }

    /**
     * Returns string representation of this node.
     *
     * @return string representation of this node
     */
    public String toString()
    {
        StringBuffer text = new StringBuffer();

        if( leaf )
        {
          text.append( "if "+predicate.toString()+" then class = '"+getScoreString()+"'");
        }
        else
        {
          if(parent == null) text.append("root");
          else
            text.append("if "+predicate.toString()+" then");
        }
        return text.toString();
    }
}

⌨️ 快捷键说明

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