Package weka.classifiers.bayes.net

Examples of weka.classifiers.bayes.net.ParentSet


   * @param nNode node for which the score is calculate
   * @return log score
   */
  private double calcNodeScoreADTree(int nNode) {
    Instances instances = m_BayesNet.m_Instances;
    ParentSet oParentSet = m_BayesNet.getParentSet(nNode);
    // get set of parents, insert iNode
    int nNrOfParents = oParentSet.getNrOfParents();
    int[] nNodes = new int[nNrOfParents + 1];
    for (int iParent = 0; iParent < nNrOfParents; iParent++) {
      nNodes[iParent] = oParentSet.getParent(iParent);
    }
    nNodes[nNrOfParents] = nNode;

    // calculate offsets
    int[] nOffsets = new int[nNrOfParents + 1];
    int nOffset = 1;
    nOffsets[nNrOfParents] = 1;
    nOffset *= instances.attribute(nNode).numValues();
    for (int iNode = nNrOfParents - 1; iNode >= 0; iNode--) {
      nOffsets[iNode] = nOffset;
      nOffset *= instances.attribute(nNodes[iNode]).numValues();
    }

    // sort nNodes & offsets
    for (int iNode = 1; iNode < nNodes.length; iNode++) {
      int iNode2 = iNode;
      while (iNode2 > 0 && nNodes[iNode2] < nNodes[iNode2 - 1]) {
        int h = nNodes[iNode2];
        nNodes[iNode2] = nNodes[iNode2 - 1];
        nNodes[iNode2 - 1] = h;
        h = nOffsets[iNode2];
        nOffsets[iNode2] = nOffsets[iNode2 - 1];
        nOffsets[iNode2 - 1] = h;
        iNode2--;
      }
    }

    // get counts from ADTree
    int nCardinality = oParentSet.getCardinalityOfParents();
    int numValues = instances.attribute(nNode).numValues();
    int[] nCounts = new int[nCardinality * numValues];
    //if (nNrOfParents > 1) {

    m_BayesNet.getADTree().getCounts(nCounts, nNodes, nOffsets, 0, 0, false);
View Full Code Here


    return calcScoreOfCounts(nCounts, nCardinality, numValues, instances);
  } // CalcNodeScore

  private double calcNodeScorePlain(int nNode) {
    Instances instances = m_BayesNet.m_Instances;
    ParentSet oParentSet = m_BayesNet.getParentSet(nNode);

    // determine cardinality of parent set & reserve space for frequency counts
    int nCardinality = oParentSet.getCardinalityOfParents();
    int numValues = instances.attribute(nNode).numValues();
    int[] nCounts = new int[nCardinality * numValues];

    // initialize (don't need this?)
    for (int iParent = 0; iParent < nCardinality * numValues; iParent++) {
      nCounts[iParent] = 0;
    }

    // estimate distributions
    Enumeration enumInsts = instances.enumerateInstances();

    while (enumInsts.hasMoreElements()) {
      Instance instance = (Instance) enumInsts.nextElement();

      // updateClassifier;
      double iCPT = 0;

      for (int iParent = 0; iParent < oParentSet.getNrOfParents(); iParent++) {
        int nParent = oParentSet.getParent(iParent);

        iCPT = iCPT * instances.attribute(nParent).numValues() + instance.value(nParent);
      }

      nCounts[numValues * ((int) iCPT) + (int) instance.value(nNode)]++;
View Full Code Here

   * @param nNode node for which the score is calculate
   * @param nCandidateParent candidate parent to add to the existing parent set
   * @return log score
   */
  public double calcScoreWithExtraParent(int nNode, int nCandidateParent) {
    ParentSet oParentSet = m_BayesNet.getParentSet(nNode);

    // sanity check: nCandidateParent should not be in parent set already
    if (oParentSet.contains(nCandidateParent)) {
        return -1e100;
    }

    // set up candidate parent
    oParentSet.addParent(nCandidateParent, m_BayesNet.m_Instances);

    // calculate the score
    double logScore = calcNodeScore(nNode);

    // delete temporarily added parent
    oParentSet.deleteLastParent(m_BayesNet.m_Instances);

    return logScore;
  } // CalcScoreWithExtraParent
View Full Code Here

   * @param nNode node for which the score is calculate
   * @param nCandidateParent candidate parent to delete from the existing parent set
   * @return log score
   */
  public double calcScoreWithMissingParent(int nNode, int nCandidateParent) {
    ParentSet oParentSet = m_BayesNet.getParentSet(nNode);

    // sanity check: nCandidateParent should be in parent set already
    if (!oParentSet.contains( nCandidateParent)) {
        return -1e100;
    }

    // set up candidate parent
    int iParent = oParentSet.deleteParent(nCandidateParent, m_BayesNet.m_Instances);

    // calculate the score
    double logScore = calcNodeScore(nNode);

    // restore temporarily deleted parent
    oParentSet.addParent(nCandidateParent, iParent, m_BayesNet.m_Instances);

    return logScore;
  } // CalcScoreWithMissingParent
View Full Code Here

    // reserve memory
    m_ParentSets = new ParentSet[m_Instances.numAttributes()];

    for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {
      m_ParentSets[iAttribute] = new ParentSet(m_Instances.numAttributes());
    }
  } // initStructure
View Full Code Here

    // reserve memory
    m_ParentSets = new ParentSet[m_Instances.numAttributes()];

    for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {
      m_ParentSets[iAttribute] = new ParentSet(m_Instances.numAttributes());
    }
  } // initStructure
View Full Code Here

     * As a side effect, the parent sets are set
     */
    void calcGlobalScore() {
      // clear current network
      for (int iNode = 0; iNode < m_nNodes; iNode++) {
        ParentSet parentSet = m_BayesNet.getParentSet(iNode);
        while (parentSet.getNrOfParents() > 0) {
          parentSet.deleteLastParent(m_BayesNet.m_Instances);
        }
      }
      // insert arrows
      for (int iNode = 0; iNode < m_nNodes; iNode++) {
        ParentSet parentSet = m_BayesNet.getParentSet(iNode);
        for (int iNode2 = 0; iNode2 < m_nNodes; iNode2++) {
          if (m_bits[iNode2 + iNode * m_nNodes]) {
            parentSet.addParent(iNode2, m_BayesNet.m_Instances);
          }
        }
      }
      // calc score
      try {
View Full Code Here

   */
  void generateRandomNet(BayesNet bayesNet, Instances instances) {
    int nNodes = instances.numAttributes();
    // clear network
    for (int iNode = 0; iNode < nNodes; iNode++) {
      ParentSet parentSet = bayesNet.getParentSet(iNode);
      while (parentSet.getNrOfParents() > 0) {
        parentSet.deleteLastParent(instances);
      }
    }
   
    // initialize as naive Bayes?
    if (getInitAsNaiveBayes()) {
View Full Code Here

   * @param iHead
   * @param iTail
   * @param instances
   */
  void applyArcAddition(BayesNet bayesNet, int iHead, int iTail, Instances instances) {
    ParentSet bestParentSet = bayesNet.getParentSet(iHead);
    bestParentSet.addParent(iTail, instances);
  } // applyArcAddition
View Full Code Here

   * @param iHead
   * @param iTail
   * @param instances
   */
  void applyArcDeletion(BayesNet bayesNet, int iHead, int iTail, Instances instances) {
    ParentSet bestParentSet = bayesNet.getParentSet(iHead);
    bestParentSet.deleteParent(iTail, instances);
  } // applyArcAddition
View Full Code Here

TOP

Related Classes of weka.classifiers.bayes.net.ParentSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.