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

📄 associationrulesalgorithm.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    public final void setMiningSettings( MiningSettings miningSettings ) throws IllegalArgumentException
    {
        if( miningSettings instanceof AssociationRulesSettings )
        {
            super.setMiningSettings( miningSettings );
            AssociationRulesSettings associationRulesSettings = (AssociationRulesSettings)miningSettings;
            this.categoryItemId = (CategoricalAttribute)associationRulesSettings.getItemId();
            this.categoryTransactId = (CategoricalAttribute)associationRulesSettings.getTransactionId();
            this.minimumSupport = associationRulesSettings.getMinimumSupport();
            this.minimumConfidence = associationRulesSettings.getMinimumConfidence();
            this.itemIdTax = categoryItemId.getTaxonomy();
        }
        else
        {
            throw new IllegalArgumentException( "MiningSettings have to be AssociationRulesSettings." );
        }
    }

    /**
     * Returns number of transactions. Standard method uses number of
     * categories of transaction ID attribute. However, for algorithms
     * that can also handle transaction ID attributes which do not store
     * all categories (e.g. AssociationRulesDecompAlgorithm), this method
     * should be overwritten.
     *
     * @return number of transactions, -1 if unknown
     */
    public int getNumberOfTransactions()
    {
      int nTransact = -1;
      if ( categoryTransactId != null && !categoryTransactId.isUnstoredCategories() )
         nTransact = categoryTransactId.getCategoriesNumber();

      return nTransact;
    }

    /**
     * Returns association rules.
     *
     * @return association rules
     */
    protected abstract Vector getAssociationRules();

    /**
     * Returns large itemsets.
     *
     * @return large itemsets
     */
    protected abstract Vector getLargeItemSets();

    // -----------------------------------------------------------------------
    //  Run association rules algorithm and build mining model
    // -----------------------------------------------------------------------
    /**
     * Runs association rules algorithm.
     *
     * @exception MiningException cannot run algorithm
     */
    protected abstract void runAlgorithm() throws MiningException;

    /**
     * Builds mining model by running the association rules algorithm internally.
     *
     * @return association rules mining model generated by the algorithm
     * @exception MiningException cannot build model
     */
    public final MiningModel buildModel() throws MiningException
    {
        long start = ( new Date() ).getTime();
        runAlgorithm();

        AssociationRulesMiningModel model = new AssociationRulesMiningModel();
        model.setMiningSettings( miningSettings );
        model.setInputSpec( applicationInputSpecification );
        Vector rules = getAssociationRules();
        if (useTaxonomy) rules = pruneRulesTaxonomy(rules);
        model.setAssociationRules( rules );
        model.setLargeItemSets( getLargeItemSets() );
        model.setItemIdName( categoryItemId.getName() );
        model.setTransactIdName( categoryTransactId.getName() );
        model.setExportTransactIds(exportTransactIds);
        model.setExportTransactItemNames(exportTransactItemNames);
        if (getNumberOfTransactions() >= 0) model.setNumberOfTransactions( getNumberOfTransactions() );
        this.miningModel = model;
        long end = ( new Date() ).getTime();
        timeSpentToBuildModel = ( end - start ) / 1000.0;
        return model;
    }

  /**
   * Rule postprocessing for taxonomies. The returned
   * rules are new ruleset objects.
   *
   * Depends on pruneRuleTaxType parameter.
   * If type parameter is set to remove nothing from rule,
   * then returns the same rule set.
   * If type parameter is set to remove ancestors from rule (standard),
   * then remove all items B in the rules where A => B and
   * B is anchestor (parent) of A since they are obvious.
   * If type parameter is set to remove ancestors and parents,
   * then like standard plus all items are removed which are
   * parents of other items in the same premise and conclusion
   * set, respectively.
   *
   * @param assRules association rules vector to be pruned
   * @return vector of pruned rule set
   */
  public Vector pruneRulesTaxonomy(Vector assRules) {

    // Hashtable of new rules:
    Hashtable ruleHash = new Hashtable();

    // Process all rules:
    for (int i = 0; i < assRules.size(); i++) {

      // Get current rule set:
      RuleSet rs   = (RuleSet) assRules.elementAt(i);
      ItemSet prem = (ItemSet) rs.getPremise();
      ItemSet conc = (ItemSet) rs.getConclusion();

      // Find all parent items of premise and conclusion itemset:
      IntHashtable premPars = null; // only required if pruneRuleTaxType = 1,2
      if (pruneRuleTaxType == TAX_REMOVE_ANCEST_FROM_RULE ||
          pruneRuleTaxType == TAX_REMOVE_ANCEST_AND_PARENTS_FROM_RULE )
        premPars = getParentSet(prem);
      IntHashtable concPars = null; // only required if pruneRuleTaxType = 2
      if (pruneRuleTaxType == TAX_REMOVE_ANCEST_AND_PARENTS_FROM_RULE)
        concPars = getParentSet(conc);

      // Create new premise and conclusion itemsets:
      ItemSet premNew = new ItemSet();
      ItemSet concNew = new ItemSet();
      // Premise set:
      for (int j = 0; j < prem.getSize(); j++) {
        int item = prem.getItemAt(j);

        // If pruneRuleTaxType = 2 => remove if parent of premise item:
        if (pruneRuleTaxType == TAX_REMOVE_ANCEST_AND_PARENTS_FROM_RULE)
          if ( premPars.get(item) != null)
            continue;

        premNew.addItem(item);
      };
      // Conclusion set:
      for (int j = 0; j < conc.getSize(); j++) {
        int item = conc.getItemAt(j);

        // If pruneRuleTaxType = 2 => remove if parent of conclusion item:
        if (pruneRuleTaxType == TAX_REMOVE_ANCEST_AND_PARENTS_FROM_RULE)
          if ( concPars.get(item) != null)
            continue;

        // If pruneRuleTaxType = 1,2 => remove if parent of premise item:
        if (pruneRuleTaxType == TAX_REMOVE_ANCEST_FROM_RULE ||
            pruneRuleTaxType == TAX_REMOVE_ANCEST_AND_PARENTS_FROM_RULE )
          if ( premPars.get(item) != null )
            continue;

        concNew.addItem(item);
      };

      // Empty itemsets => no rule:
      if (premNew.getSize() == 0 || concNew.getSize() == 0)
        continue;

      // Add new rule to rule hashtable:
      RuleSet rsNew = new RuleSet(premNew, concNew, rs.getSupport(), rs.getConfidence());
      if (ruleHash.get(rsNew) == null)
        ruleHash.put(rsNew, new Integer(0));
    };

    // Copy rules from hashtable into vector:
    Vector redRules = new Vector();
    Enumeration rul = ruleHash.keys();
    while (rul.hasMoreElements())
      redRules.addElement( rul.nextElement() );

    System.out.println("#rules tax. pruned = " + (int)(assRules.size()-redRules.size())  );

    return redRules;
  }

  /**
   * Finds all parent items for a given itemset.
   *
   * @param childSet itemset whose parents are calculated
   * @return all parent items of childSet
   */
  private IntHashtable getParentSet(ItemSet childSet) {

    // Find all parents of child set:
    IntHashtable parents = new IntHashtable();
    for (int i = 0; i < childSet.getSize(); i++) {
      IntVector pars = getParentItemKeys( childSet.getItemAt(i) );
      for (int j = 0; j < pars.size(); j++)
        if (parents.get( pars.IntegerAt(j) ) == null)
          parents.put( pars.IntegerAt(j), new Integer(0) );
    };

    return parents;
  }

  /**
   * Get keys of parent items as integers for a given item key.
   *
   * @param itemId key if itemId
   * @return vector of int keys of parents
   */
  protected IntVector getParentItemKeys(int itemId) {

    IntVector parItems = new IntVector();

    if (itemIdTax == null)
      return parItems;

    // Get all parent categories of 'itemId':
    Category ItemId  = categoryItemId.getCategory(itemId);
    Vector parCItems = itemIdTax.getAllParents( ItemId );
    for (int i = 0; i < parCItems.size(); i++) {
      Category parItem = (Category) parCItems.elementAt(i);
      double key = categoryItemId.getKey( parItem );
      if ( Category.isMissingValue(key) )
        key = categoryItemId.addCategory(parItem);
      parItems.addElement( (int) key );
    };

    return parItems;
  }

}

⌨️ 快捷键说明

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