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

📄 aode.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public double [] distributionForInstance(Instance instance)
                                                        throws Exception {
    double [] probs = new double[m_NumClasses];
    int pIndex, parentCount; 
    double [][] pointer1;
    double [] pointer2;
    
    // store instance's att values in an int array, so accessing them is more efficient in loop(s)
    int [] attIndex = new int[m_NumAttributes];
    for(int att = 0; att < m_NumAttributes; att++) {
       if(instance.isMissing(att) || att == m_ClassIndex)
          attIndex[att] = -1;
       else
          attIndex[att] = m_StartAttIndex[att] + (int)instance.value(att);
    }
    
    // calculate probabilities for each possible class value
    for(int classVal = 0; classVal < m_NumClasses; classVal++) {
            
       probs[classVal] = 0;
       double x = 0;
       
       pointer1 = m_CondiCounts[classVal];
       
       parentCount = 0;

       // each attribute has a turn of being the parent
       for(int parent = 0; parent < m_NumAttributes; parent++) {
          if(attIndex[parent] == -1)
             continue;

          // determine correct index for the parent in m_CondiCounts matrix
          pIndex = attIndex[parent];

          pointer2 = pointer1[pIndex];

          // check that the att value has a frequency of m_Limit or greater
	  if(m_Frequencies[pIndex] < m_Limit)
	     continue;
	     
          // block the parent from being its own child
	  attIndex[parent] = -1;
	  
          parentCount++;

          double classparentfreq = pointer2[pIndex];

          // calculate the prior probability -- P(parent & classVal)
          x = (classparentfreq + 1.0)
	     / (m_SumInstances + m_NumClasses * m_NumAttValues[parent]);

	  // take into account the value of each attribute
          for(int att = 0; att < m_NumAttributes; att++) {
             if(attIndex[att] == -1)
                continue;
 
             x *= (pointer2[attIndex[att]] + 1.0)
                 / (classparentfreq + m_NumAttValues[att]);
          }

	  // add this probability to the overall probability
          probs[classVal] += x;
       
          // unblock the parent
	  attIndex[parent] = pIndex;
       }
        
       // check that at least one att was a parent
       if(parentCount < 1) {

          // do plain naive bayes conditional prob
	  probs[classVal] = NBconditionalProb(instance, classVal);

       } else {
       
          // divide by number of parent atts to get the mean
          probs[classVal] /= (double)(parentCount);
       }
    }
 
    Utils.normalize(probs);
    return probs;
  }


  /**
   * Calculates the probability of the specified class for the given test
   * instance, using naive Bayes.
   *
   * @param instance the instance to be classified
   * @param classVal the class for which to calculate the probability
   * @return predicted class probability
   * @exception Exception if there is a problem generating the prediction
   */
  public double NBconditionalProb(Instance instance, int classVal) {
    
    double prob;
    int attIndex;
    double [][] pointer;

    // calculate the prior probability
    prob = (m_ClassCounts[classVal] + 1.0) / (m_SumInstances + m_NumClasses);
    
    pointer = m_CondiCounts[classVal];
    
    // consider effect of each att value
    for(int att = 0; att < m_NumAttributes; att++) {
       if(att == m_ClassIndex || instance.isMissing(att))
          continue;

       // determine correct index for att in m_CondiCounts
       attIndex = m_StartAttIndex[att] + (int)instance.value(att);

       prob *= (double)(pointer[attIndex][attIndex] + 1.0)
              / ((double)m_SumForCounts[classVal][att] + m_NumAttValues[att]);
    }

    return prob;
  }


  /**
   * Returns an enumeration describing the available options
   *
   * @return an enumeration of all the available options
   */
  public Enumeration<Option> listOptions() {
        
    Vector<Option> newVector = new Vector<Option>(2);
        
    newVector.addElement(
       new Option("\tOutput debugging information\n",
                  "D", 0,"-D"));
    newVector.addElement(
       new Option("\tImpose a frequency limit for superParents\n"
                  + "\t(default is 30)", "F", 1,"-F"));

    return newVector.elements();
  }

    
  /**
   * Parses a given list of options. Valid options are:<p>
   *
   * -D <br>
   * Debugging information is printed.<p>
   *
   * -F <br>
   * Specify the frequency limit for parent attributes.<p>
   *
   * @param options the list of options as an array of strings
   * @exception Exception if an option is not supported
   */
  public void setOptions(String[] options) throws Exception {

    m_Debug = Utils.getFlag('D', options);

    String Freq = Utils.getOption('F', options);
    if(Freq.length() != 0) 
       m_Limit = Integer.parseInt(Freq);
    else
       m_Limit = 30;
    
    Utils.checkForRemainingOptions(options);
  }
    
  /**
   * Gets the current settings of the classifier.
   *
   * @return an array of strings suitable for passing to setOptions
   */
  public String [] getOptions() {
        
    String [] options = new String [3];
    int current = 0;
        
    if (m_Debug) {
       options[current++] = "-D";
    }
        
    options[current++] = "-F"; options[current++] = ""+m_Limit;
    while (current < options.length) {
       options[current++] = "";
    }
    return options;
  }

  /**
   * Returns the tip text for this property
   * @return tip text for this property suitable for
   * displaying in the explorer/experimenter gui
   */
  public String frequencyLimitForParentAttributesTipText() {
    return "Minimum frequency count for parent attributes.";
  }

  /**
   * Set the frequency limit for parent attributes
   *
   * @param fl an <code>int</code> value
   */
  public void setFrequencyLimitForParentAttributes(int fl) {
    if (fl > 0) {
      m_Limit = fl;
    }
  }

  /**
   * Return the frequency limit for parent attributes
   *
   * @return an <code>int</code> value
   */
  public int getFrequencyLimitForParentAttributes() {
    return m_Limit;
  }
    
  /**
   * Returns a description of the classifier.
   *
   * @return a description of the classifier as a string.
   */
  public String toString() {
 
    StringBuffer text = new StringBuffer();
        
    text.append("The AODE Classifier");
    if (m_Instances == null) {
       text.append(": No model built yet.");
    } else {
       try {
          for (int i = 0; i < m_NumClasses; i++) {
             // print to string, the prior probabilities of class values
             text.append("\nClass " + m_Instances.classAttribute().value(i) +
                       ": Prior probability = " + Utils.
                          doubleToString(((m_ClassCounts[i] + 1)
                       /(m_SumInstances + m_NumClasses)), 4, 2)+"\n\n");
          }
                
          text.append("Dataset: " + m_Instances.relationName() + "\n"
                      + "Instances: " + m_NumInstances + "\n"
                      + "Attributes: " + m_NumAttributes + "\n"
		      + "Frequency limit for superParents: " + m_Limit + "\n");
                
       } catch (Exception ex) {
          text.append(ex.getMessage());
       }
    }
        
    return text.toString();
  }
    
    
  /**
   * Main method for testing this class.
   *
   * @param argv the options
   */
  public static void main(String [] argv) {
 
    try {
       System.out.println(Evaluation.evaluateModel(new AODE(), argv));
    } catch (Exception e) {
       e.printStackTrace();
       System.err.println(e.getMessage());
    }
  }
}

⌨️ 快捷键说明

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