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

📄 predictiveapriori.java

📁 Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Gets the class attribute of all instances     * @return Instances containing only the class attribute     */      public Instances getInstancesOnlyClass() {            Instances onlyClass = null;      try{        onlyClass = LabeledItemSet.divide(m_instances,true);      }       catch(Exception e){        e.printStackTrace();        System.out.println("\n"+e.getMessage());      }      return onlyClass;        }    /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {    String string1 = "\tThe required number of rules. (default = " + (m_numRules-5) + ")",      string2 = "\tIf set class association rules are mined. (default = no)",      string3 = "\tThe class index. (default = last)";    FastVector newVector = new FastVector(3);    newVector.addElement(new Option(string1, "N", 1, 				    "-N <required number of rules output>"));    newVector.addElement(new Option(string2, "A", 0,				    "-A"));    newVector.addElement(new Option(string3, "c", 1,				    "-c <the class index>"));    return newVector.elements();  } /**   * Parses a given list of options. <p/>   *    <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -N &lt;required number of rules output&gt;   *  The required number of rules. (default = 100)</pre>   *    * <pre> -A   *  If set class association rules are mined. (default = no)</pre>   *    * <pre> -c &lt;the class index&gt;   *  The class index. (default = last)</pre>   *    <!-- options-end -->   *   * @param options the list of options as an array of strings   * @throws Exception if an option is not supported    */  public void setOptions(String[] options) throws Exception {        resetOptions();    String numRulesString = Utils.getOption('N', options),           classIndexString = Utils.getOption('c',options);        if (numRulesString.length() != 0) 	m_numRules = Integer.parseInt(numRulesString)+5;    else        m_numRules = Integer.MAX_VALUE;    if (classIndexString.length() != 0) 	m_classIndex = Integer.parseInt(numRulesString);    m_car = Utils.getFlag('A', options);  }  /**   * Gets the current settings of the PredictiveApriori object.   *   * @return an array of strings suitable for passing to setOptions   */  public String [] getOptions() {    String [] options = new String [10];    int current = 0;    options[current++] = "-N"; options[current++] = "" + (m_numRules-5);    options[current++] = "-A"; options[current++] = "" + m_car;    options[current++] = "-c"; options[current++] = "" + m_classIndex;    while (current < options.length) {      options[current++] = "";    }    return options;  }  /**   * Outputs the association rules.   *    * @return a string representation of the model   */  public String toString() {    StringBuffer text = new StringBuffer();    if (m_allTheRules[0].size() == 0)      return "\nNo large itemsets and rules found!\n";    text.append("\nPredictiveApriori\n===================\n\n");    text.append("\nBest rules found:\n\n");        for (int i = 0; i < m_allTheRules[0].size(); i++) {	    text.append(Utils.doubleToString((double)i+1, 					     (int)(Math.log(m_numRules)/Math.log(10)+1),0)+			". " + ((ItemSet)m_allTheRules[0].elementAt(i)).			toString(m_instances) 			+ " ==> " + ((ItemSet)m_allTheRules[1].elementAt(i)).			toString(m_instances) +"    acc:("+  			Utils.doubleToString(((Double)m_allTheRules[2].					      elementAt(i)).doubleValue(),5)+")");            text.append('\n');    }            return text.toString();  }   /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String numRulesTipText() {    return "Number of rules to find.";  }  /**   * Get the value of the number of required rules.   *   * @return Value of the number of required rules.   */  public int getNumRules() {          return m_numRules-5;  }    /**   * Set the value of required rules.   *   * @param v  Value to assign to number of required rules.   */  public void setNumRules(int v) {	       m_numRules = v+5;  }      /**   * Sets the class index   * @param index the index of the class attribute   */    public void setClassIndex(int index){            m_classIndex = index;  }    /**   * Gets the index of the class attribute   * @return the index of the class attribute   */    public int getClassIndex(){            return m_classIndex;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String classIndexTipText() {    return "Index of the class attribute.\n If set to -1, the last attribute will be taken as the class attribute.";  }    /**   * Sets class association rule mining   * @param flag if class association rules are mined, false otherwise   */    public void setCar(boolean flag){            m_car = flag;  }    /**   * Gets whether class association ruels are mined   * @return true if class association rules are mined, false otherwise   */    public boolean getCar(){            return m_car;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String carTipText() {    return "If enabled class association rules are mined instead of (general) association rules.";  }      /**   * Returns the metric string for the chosen metric type.   * Predictive apriori uses the estimated predictive accuracy.   * Therefore the metric string is "acc".   * @return string "acc"   */  public String metricString() {            return "acc";  }   /**    * Method that finds all large itemsets for the given set of instances.   *   * @param index the instances to be used   * @throws Exception if an attribute is numeric   */  private void findLargeItemSets(int index) throws Exception {        FastVector kMinusOneSets, kSets = new FastVector();    Hashtable hashtable;    int i = 0;    // Find large itemsets    //of length 1    if(index == 1){        kSets = ItemSet.singletons(m_instances);        ItemSet.upDateCounters(kSets, m_instances);        kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE);        if (kSets.size() == 0)            return;        m_Ls.addElement(kSets);    }    //of length > 1    if(index >1){        if(m_Ls.size() > 0)            kSets = (FastVector)m_Ls.lastElement();        m_Ls.removeAllElements();        i = index-2;        kMinusOneSets = kSets;        kSets = ItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances());        hashtable = ItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size());        m_hashtables.addElement(hashtable);        kSets = ItemSet.pruneItemSets(kSets, hashtable);        ItemSet.upDateCounters(kSets, m_instances);        kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE);        if(kSets.size() == 0)            return;        m_Ls.addElement(kSets);    }  }     /**    * Method that finds all association rules.   *   * @throws Exception if an attribute is numeric   */  private void findRulesQuickly() throws Exception {    RuleGeneration currentItemSet;        // Build rules    for (int j = 0; j < m_Ls.size(); j++) {      FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);      Enumeration enumItemSets = currentItemSets.elements();      while (enumItemSets.hasMoreElements()) {         currentItemSet = new RuleGeneration((ItemSet)enumItemSets.nextElement());        m_best = currentItemSet.generateRules(m_numRules, m_midPoints,m_priors,m_expectation,                                        m_instances,m_best,m_count);                  m_count = currentItemSet.m_count;        if(!m_bestChanged && currentItemSet.m_change)           m_bestChanged = true;        //update minimum expected predictive accuracy to get into the n best        if(m_best.size() >0)            m_expectation = ((RuleItem)m_best.first()).accuracy();        else m_expectation =0;      }    }  }      /**   * Method that finds all large itemsets for class association rule mining for the given set of instances.   * @param index the size of the large item sets   * @throws Exception if an attribute is numeric   */  private void findLargeCarItemSets(int index) throws Exception {        FastVector kMinusOneSets, kSets = new FastVector();    Hashtable hashtable;    int i = 0;    // Find large itemsets    if(index == 1){        kSets = CaRuleGeneration.singletons(m_instances);        ItemSet.upDateCounters(kSets, m_instances);        kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE);        if (kSets.size() == 0)            return;        m_Ls.addElement(kSets);    }        if(index >1){        if(m_Ls.size() > 0)            kSets = (FastVector)m_Ls.lastElement();        m_Ls.removeAllElements();        i = index-2;        kMinusOneSets = kSets;        kSets = ItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances());        hashtable = ItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size());        m_hashtables.addElement(hashtable);        kSets = ItemSet.pruneItemSets(kSets, hashtable);        ItemSet.upDateCounters(kSets, m_instances);        kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE);        if(kSets.size() == 0)          return;        m_Ls.addElement(kSets);    }  }     /**    * Method that finds all class association rules.   *   * @throws Exception if an attribute is numeric   */  private void findCaRulesQuickly() throws Exception {        CaRuleGeneration currentLItemSet;    // Build rules    for (int j = 0; j < m_Ls.size(); j++) {      FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);      Enumeration enumItemSets = currentItemSets.elements();      while (enumItemSets.hasMoreElements()) {        currentLItemSet = new CaRuleGeneration((ItemSet)enumItemSets.nextElement());        m_best = currentLItemSet.generateRules(m_numRules, m_midPoints,m_priors,m_expectation,                                        m_instances,m_best,m_count);        m_count = currentLItemSet.count();        if(!m_bestChanged && currentLItemSet.change())                m_bestChanged = true;        if(m_best.size() >0)            m_expectation = ((RuleItem)m_best.first()).accuracy();        else             m_expectation =0;      }    }  }  /**   * Main method.   *    * @param args the commandline parameters   */  public static void main(String[] args) {    String trainFileString;    StringBuffer text = new StringBuffer();    PredictiveApriori apriori = new PredictiveApriori();    Reader reader;    try {      text.append("\n\nPredictiveApriori options:\n\n");      text.append("-t <training file>\n");      text.append("\tThe name of the training file.\n");      Enumeration enu = apriori.listOptions();      while (enu.hasMoreElements()) {	Option option = (Option)enu.nextElement();	text.append(option.synopsis()+'\n');	text.append(option.description()+'\n');      }      trainFileString = Utils.getOption('t', args);      if (trainFileString.length() == 0) 	throw new Exception("No training file given!");      apriori.setOptions(args);      reader = new BufferedReader(new FileReader(trainFileString));      if(!apriori.getCar())        apriori.buildAssociations(new Instances(reader));      else        apriori.mineCARs(new Instances(reader));      System.out.println(apriori);    } catch(Exception e) {      e.printStackTrace();      System.out.println("\n"+e.getMessage()+text);    }  }  }

⌨️ 快捷键说明

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