apriori.java

来自「数据挖掘关联规则算法:Apriori算法源代码」· Java 代码 · 共 1,465 行 · 第 1/4 页

JAVA
1,465
字号
      +"this value.";  }  /**   * Get the value of minConfidence.   *   * @return Value of minConfidence.   */  public double getMinMetric() {        return m_minMetric;  }    /**   * Set the value of minConfidence.   *   * @param v  Value to assign to minConfidence.   */  public void setMinMetric(double v) {        m_minMetric = v;  }  /**   * 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 numRules.   *   * @return Value of numRules.   */  public int getNumRules() {        return m_numRules;  }    /**   * Set the value of numRules.   *   * @param v  Value to assign to numRules.   */  public void setNumRules(int v) {        m_numRules = v;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String deltaTipText() {    return "Iteratively decrease support by this factor. Reduces support "      +"until min support is reached or required number of rules has been "      +"generated.";  }      /**   * Get the value of delta.   *   * @return Value of delta.   */  public double getDelta() {        return m_delta;  }    /**   * Set the value of delta.   *   * @param v  Value to assign to delta.   */  public void setDelta(double v) {        m_delta = v;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String significanceLevelTipText() {    return "Significance level. Significance test (confidence metric only).";  }  /**   * Get the value of significanceLevel.   *   * @return Value of significanceLevel.   */  public double getSignificanceLevel() {        return m_significanceLevel;  }    /**   * Set the value of significanceLevel.   *   * @param v  Value to assign to significanceLevel.   */  public void setSignificanceLevel(double v) {        m_significanceLevel = v;  }  /**   * Sets whether itemsets are output as well   * @param flag true if itemsets are to be output as well   */    public void setOutputItemSets(boolean flag){    m_outputItemSets = flag;  }    /**   * Gets whether itemsets are output as well   * @return true if itemsets are output as well   */    public boolean getOutputItemSets(){    return m_outputItemSets;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String outputItemSetsTipText() {    return "If enabled the itemsets are output as well.";  }  /**   * Sets verbose mode   * @param flag true if algorithm should be run in verbose mode   */    public void setVerbose(boolean flag){    m_verbose = flag;  }    /**   * Gets whether algorithm is run in verbose mode   * @return true if algorithm is run in verbose mode   */    public boolean getVerbose(){    return m_verbose;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String verboseTipText() {    return "If enabled the algorithm will be run in verbose mode.";  }  /**    * Method that finds all large itemsets for the given set of instances.   *   * @throws Exception if an attribute is numeric   */  private void findLargeItemSets() throws Exception {        FastVector kMinusOneSets, kSets;    Hashtable hashtable;    int necSupport, necMaxSupport,i = 0;                // Find large itemsets    // minimum support    necSupport = (int)(m_minSupport * (double)m_instances.numInstances()+0.5);    necMaxSupport = (int)(m_upperBoundMinSupport * (double)m_instances.numInstances()+0.5);       kSets = AprioriItemSet.singletons(m_instances);    AprioriItemSet.upDateCounters(kSets,m_instances);    kSets = AprioriItemSet.deleteItemSets(kSets, necSupport, necMaxSupport);    if (kSets.size() == 0)      return;    do {      m_Ls.addElement(kSets);      kMinusOneSets = kSets;      kSets = AprioriItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances());      hashtable = AprioriItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size());      m_hashtables.addElement(hashtable);      kSets = AprioriItemSet.pruneItemSets(kSets, hashtable);      AprioriItemSet.upDateCounters(kSets, m_instances);      kSets = AprioriItemSet.deleteItemSets(kSets, necSupport, necMaxSupport);      i++;    } while (kSets.size() > 0);  }    /**    * Method that finds all association rules and performs significance test.   *   * @throws Exception if an attribute is numeric   */  private void findRulesBruteForce() throws Exception {    FastVector[] rules;    // Build rules    for (int j = 1; j < m_Ls.size(); j++) {      FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);      Enumeration enumItemSets = currentItemSets.elements();      while (enumItemSets.hasMoreElements()) {	AprioriItemSet currentItemSet = (AprioriItemSet)enumItemSets.nextElement();        //AprioriItemSet currentItemSet = new AprioriItemSet((ItemSet)enumItemSets.nextElement());	rules=currentItemSet.generateRulesBruteForce(m_minMetric,m_metricType,				  m_hashtables,j+1,				  m_instances.numInstances(),				  m_significanceLevel);	for (int k = 0; k < rules[0].size(); k++) {	  m_allTheRules[0].addElement(rules[0].elementAt(k));	  m_allTheRules[1].addElement(rules[1].elementAt(k));	  m_allTheRules[2].addElement(rules[2].elementAt(k));	  m_allTheRules[3].addElement(rules[3].elementAt(k));	  m_allTheRules[4].addElement(rules[4].elementAt(k));	  m_allTheRules[5].addElement(rules[5].elementAt(k));	}      }    }  }  /**    * Method that finds all association rules.   *   * @throws Exception if an attribute is numeric   */  private void findRulesQuickly() throws Exception {    FastVector[] rules;    // Build rules    for (int j = 1; j < m_Ls.size(); j++) {      FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);      Enumeration enumItemSets = currentItemSets.elements();      while (enumItemSets.hasMoreElements()) {	AprioriItemSet currentItemSet = (AprioriItemSet)enumItemSets.nextElement();        //AprioriItemSet currentItemSet = new AprioriItemSet((ItemSet)enumItemSets.nextElement());	rules = currentItemSet.generateRules(m_minMetric, m_hashtables, j + 1);	for (int k = 0; k < rules[0].size(); k++) {	  m_allTheRules[0].addElement(rules[0].elementAt(k));	  m_allTheRules[1].addElement(rules[1].elementAt(k));	  m_allTheRules[2].addElement(rules[2].elementAt(k));	}      }    }  }        /**     *     * Method that finds all large itemsets for class association rules for the given set of instances.     * @throws Exception if an attribute is numeric     */    private void findLargeCarItemSets() throws Exception {		FastVector kMinusOneSets, kSets;	Hashtable hashtable;	int necSupport, necMaxSupport,i = 0;		// Find large itemsets		// minimum support        double nextMinSupport = m_minSupport*(double)m_instances.numInstances();        double nextMaxSupport = m_upperBoundMinSupport*(double)m_instances.numInstances();	if((double)Math.rint(nextMinSupport) == nextMinSupport){            necSupport = (int) nextMinSupport;        }        else{            necSupport = Math.round((float)(nextMinSupport+0.5));        }        if((double)Math.rint(nextMaxSupport) == nextMaxSupport){            necMaxSupport = (int) nextMaxSupport;        }        else{            necMaxSupport = Math.round((float)(nextMaxSupport+0.5));        }		//find item sets of length one	kSets = LabeledItemSet.singletons(m_instances,m_onlyClass);	LabeledItemSet.upDateCounters(kSets, m_instances,m_onlyClass);                //check if a item set of lentgh one is frequent, if not delete it	kSets = LabeledItemSet.deleteItemSets(kSets, necSupport, necMaxSupport);        if (kSets.size() == 0)	    return;	do {	    m_Ls.addElement(kSets);	    kMinusOneSets = kSets;	    kSets = LabeledItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances());	    hashtable = LabeledItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size());	    kSets = LabeledItemSet.pruneItemSets(kSets, hashtable);	    LabeledItemSet.upDateCounters(kSets, m_instances,m_onlyClass);	    kSets = LabeledItemSet.deleteItemSets(kSets, necSupport, necMaxSupport);	    i++;	} while (kSets.size() > 0);    }      /**    * Method that finds all class association rules.   *   * @throws Exception if an attribute is numeric   */   private void findCarRulesQuickly() throws Exception {    FastVector[] rules;    // Build rules    for (int j = 0; j < m_Ls.size(); j++) {      FastVector currentLabeledItemSets = (FastVector)m_Ls.elementAt(j);      Enumeration enumLabeledItemSets = currentLabeledItemSets.elements();      while (enumLabeledItemSets.hasMoreElements()) {	LabeledItemSet currentLabeledItemSet = (LabeledItemSet)enumLabeledItemSets.nextElement();	rules = currentLabeledItemSet.generateRules(m_minMetric,false);	for (int k = 0; k < rules[0].size(); k++) {	  m_allTheRules[0].addElement(rules[0].elementAt(k));	  m_allTheRules[1].addElement(rules[1].elementAt(k));	  m_allTheRules[2].addElement(rules[2].elementAt(k));	}      }    }  }  /**   * returns all the rules   *   * @return		all the rules   * @see		#m_allTheRules   */  public FastVector[] getAllTheRules() {    return m_allTheRules;  }    /**   * Returns the revision string.   *    * @return		the revision   */  public String getRevision() {    return RevisionUtils.extract("$Revision: 1.31 $");  }  /**   * Main method.   *    * @param args the commandline options   */  public static void main(String[] args) {    runAssociator(new Apriori(), args);  }}

⌨️ 快捷键说明

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