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

📄 associationrulesbuild.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    {
        String show = "";
        MiningAlgorithmParameter[] attribute = miningAlgorithmSpecification.getInputAttribute();
        if( attribute.length > 0 )
        {
            System.out.println("Algorithm parameters for " +  miningAlgorithmSpecification.getName());
        }
        String name, desc, method, type, value;
        for (int i = 0; i < attribute.length; i++)
        {
            name = attribute[i].getName();
            desc = attribute[i].getDescr();
            method = attribute[i].getMethod();
            type = attribute[i].getType();
            value = attribute[i].getValue();

            show = show + "Name = " + name + "; " + "Descr = " + desc + "; " + "Method = " + method + "; " + "Value = " + value + "; Type = " + type + "\n";
        }
        System.out.println( show );
    }

    /**
     * Create instance of MiningAlgorithm.
     *
     * @param className class name of algorithm object to be created
     * @return created algorithm instance
     * @throws MiningException could not create instance of MiningAlgorithm
     */
    private static MiningAlgorithm initAlgorithm( String className ) throws MiningException
    {
        Class algorithmClass = null;
        Object algorithm = null;
        try
        {
            algorithmClass = Class.forName( className );
            algorithm = algorithmClass.newInstance();
        }
        catch( ClassNotFoundException ex)
        {
            throw new MiningException( "Class not found." );
        }
        catch( InstantiationException ie )
        {
            throw new MiningException( "Can't create class instance." );
        }
        catch ( IllegalAccessException iae )
        {
            throw new MiningException( "Illegal access exception." );
        }
        if( !(algorithm instanceof MiningAlgorithm) )
        {
            throw new MiningException( "Each algorithm have to implement MiningAlgorithm interface" );
        }
        MiningAlgorithm miningAlgorithm = (MiningAlgorithm)algorithm;
        return miningAlgorithm;
    }

    /**
     * Add taxonomy to itemId attribute in the most simple way.
     * This only works for the file 'transact.arff'.
     *
     * @param itemId categorical attribute to enrich with taxonomy
     * @throws MiningException cannot add taxonomy
     */
    public static void addTaxonomy(CategoricalAttribute itemId)
      throws MiningException {

      // Hierarchy for attribute itemId:
      CategoryHierarchy cah = new CategoryHierarchy();

      // Define categories:
      Category c1 = (Category) itemId.getCategory(1);
      Category c2 = (Category) itemId.getCategory(2);
      Category c3 = (Category) itemId.getCategory(3);
      Category c4 = (Category) itemId.getCategory(4);
      Category c5 = (Category) itemId.getCategory(5);

      // Relations:
      cah.addRelationship(c1, c2);
      cah.addRelationship(c1, c3);
      cah.addRelationship(c2, c4);
      cah.addRelationship(c2, c5);

      itemId.setTaxonomy(cah);
  }

  /**
   * Show association rules and large itemsets.
   *
   * @param ruleModel model of basket analysis
   * @exception MiningException cannot show rules
   */
  public static void showRules(AssociationRulesMiningModel ruleModel)
    throws MiningException {

      // Get rules and large itemsets from model:
      Vector rules = ruleModel.getAssociationRules();
      Vector LITS  = ruleModel.getLargeItemSets();

      // Get item and transaction attributes:
      CategoricalAttribute itemId     = (CategoricalAttribute)( (AssociationRulesSettings) ruleModel.getMiningSettings() ).getItemId();
      CategoricalAttribute transactId = (CategoricalAttribute)( (AssociationRulesSettings) ruleModel.getMiningSettings() ).getTransactionId();

      // Get number of rules, large itemsets, and transactions:
      int nLITS  = LITS.size();
      int nRules = rules.size();
      int itemNumber = itemId.getCategoriesNumber();
      int transactsNumber = ruleModel.getNumberOfTransactions();

      // Show all association rules:
      System.out.println();
      System.out.println("Number of association rules found: " + nRules);
      for (int i = 0; i < nRules; i++) {
        // New rule:
        System.out.print(i + ": ");

        // Get and show rule:
        RuleSet rs   = (RuleSet) rules.elementAt(i);
        int itemSize = rs.getSize();

        // Premise part of rule:
        ItemSet is   = rs.getPremise();
        int nprem    = rs.getPremise().getSize();
        for (int j = 0; j < nprem; j++) {
          int pN        = is.getItemAt(j);
          Category cat  = (Category) itemId.getCategory(pN);
          System.out.print(cat.getValue() + " ");
        };
        System.out.print("=> ");

        // Conclusion part of rule:
        for (int j = nprem; j < itemSize; j++) {
          int pN        = rs.getConclusion().getItemAt(j-nprem);
          Category cat  = (Category) itemId.getCategory(pN);
          System.out.print(cat.getValue() + " ");
        }

        // Show support and confidence of rule:
        double Support    = rs.getSupport() * 100.0;
        double Confidence = rs.getConfidence() * 100.0;
        System.out.print("Supp = " + Math.round(Support*100)/100.0 + ", ");
        System.out.print("Conf = " + Math.round(Confidence*100)/100.0 + ", ");

        // Additional measures:
        ruleModel.buildLargeItemSets();
        double Coverage = ruleModel.coverage(rs) * 100.0;
        double Lift     = ruleModel.lift(rs);
        double Cosine   = ruleModel.cosine(rs);
        System.out.print("Cov = " + Math.round(Coverage*100)/100.0 + ", ");
        System.out.print("Lift = " + Math.round(Lift*100)/100.0+ ", ");
        System.out.println("Cos = " + Math.round(Cosine*100)/100.0);
      }

      // Show large itemsets:
      System.out.println();
      System.out.println("Number of large itemsets found: " + nLITS);
      for (int i = 0; i < nLITS; i++) {
        // New large itemset:
        System.out.print(i + ": ");

        // Get and show large itemset:
        ItemSet is   = (ItemSet) LITS.elementAt(i);
        int itemSize = is.getSize();
        for (int j = 0; j < itemSize; j++) {
          int pN       = is.getItemAt(j);
          Category cat  = (Category) itemId.getCategory(pN);
          System.out.print(cat.getValue() + " ");
        };

        // Show support of large itemset:
        double Support = 100.0 * ((double) is.getSupportCount()) /
                                 transactsNumber;
        System.out.println(" Supp = " + Math.round(Support*100)/100.0);
      }
  }

  /**
   * Demonstrates selection.
   *
   * @param ruleModel model of basket analysis
   * @exception MiningException cannot show rules
   */
  public static void showSelection(AssociationRulesMiningModel ruleModel)
    throws MiningException {

      // Convert rules as stream:
      ruleModel.setMisExportType(AssociationRulesMiningModel.EXPORT_MIS_ASSOCIATION_RULES );
      ruleModel.setMisExportCharType( AssociationRulesMiningModel.EXPORT_MISCHAR_SUPPORT +
                                      AssociationRulesMiningModel.EXPORT_MISCHAR_CONFIDENCE +
                                      AssociationRulesMiningModel.EXPORT_MISCHAR_COVERAGE +
                                      AssociationRulesMiningModel.EXPORT_MISCHAR_LIFT +
                                      AssociationRulesMiningModel.EXPORT_MISCHAR_COSINE);
      MiningInputStream mis = ruleModel.toMiningInputStream();
      System.out.println();
      System.out.println("Rules as mining input stream: " + mis);

      // Create multidimensional stream:
      mis.reset();
      MultidimensionalStream mds = new MultidimensionalStream(mis);
      mds.readMultidimensionalStreamData();

      // Select all rules containing items of the set {1,3}:
      Vector vec = new Vector();
      vec.add("1");
      vec.add("3");
      SelectAttribute sa = new SelectAttribute("itemId", vec);
      SelectAttribute[] selAtt = {sa};
      mds.runSelections(selAtt);
      System.out.println("Selected rules as mining input stream: " + mds);

      // Get rule numbers:
      Hashtable ruleNumbs = new Hashtable();
      mds.reset();
      while ( mds.next() ) {
        MiningVector mv = mds.read();
        Integer RNR = new Integer ( (int) mv.getValue("ruleNumber") );
        if ( ruleNumbs.get(RNR) == null )
          ruleNumbs.put(RNR, RNR);
      }

      // Show rule numbers:
      System.out.println("Numbers of selected rules: ");
      Enumeration values = ruleNumbs.elements();
      while ( values.hasMoreElements() )
        System.out.print( values.nextElement() + " " );
      System.out.println();
    }

}

⌨️ 快捷键说明

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