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

📄 associationoperator.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    }
	    
	    /* Assign mining settings */
	    miningSettings.setItemId( categoryItemId );
	    miningSettings.setTransactionId( categoryTransactId );
//	    miningSettings.setMinimumSupport( Integer.parseInt(support)/100.0 );
	    miningSettings.setMinimumSupport( Double.parseDouble(support)/100.0);
	    miningSettings.setMinimumConfidence( Integer.parseInt(confidence)/100.0 );
	    try {
	    	miningSettings.verifySettings();
	    } catch (Exception e)
		{
	    	m_SystemMessageHandler.appendMessage("Please specify all required parameters in building the Association model.");
	    	throw new AppException("Please specify all required parameters in building the Association model.");
	    }
	    /* Set MiningSettings */
		aOutputBIModel.setMiningSettings(miningSettings);
	    
		/* Get default mining algorithm specification from 'algorithms.xml' */
		MiningAlgorithmSpecification miningAlgorithmSpecification =
		  MiningAlgorithmSpecification.getMiningAlgorithmSpecification(ALGORITHM_NAME,getNodeInfo());
		if( miningAlgorithmSpecification == null )
		  throw new MiningException( "Can't find clustering method." );
	
		/* Get class name from algorithms specification */
		String className = miningAlgorithmSpecification.getClassname();
		if( className == null )
		  throw new MiningException( "classname attribute expected." );
	
		/* Set and display mining parameters */
		//<<Frank J. Xu, 10/05/2005
		//Using open source code from Xelopes1.2.4, instead of using the commercial version.	
		//miningAlgorithmSpecification.setMAPValue(MAP_APRIORYHYBRID_STEP, steps);
		//Frank J. Xu, 10/05/2005>>
		miningAlgorithmSpecification.setMAPValue(MAP_MIN_ITEM_SIZE, minSize);
		miningAlgorithmSpecification.setMAPValue(MAP_MAX_ITEM_SIZE, maxSize);
		miningAlgorithmSpecification.setMAPValue(MAP_GEN_RULES, generateRules);
		aOutputBIModel.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
		displayMiningAlgSpecParameters(miningAlgorithmSpecification);
	
		/* Create algorithm object with default values */
		AssociationRulesAlgorithm algorithm = (AssociationRulesAlgorithm)
			GeneralUtils.createMiningAlgorithmInstance(className, this.getClass().getClassLoader());
	
		/* Put it all together in the algorithm */
		algorithm.setMiningInputStream( aInputBIData.getMiningStoredData() );
		algorithm.setMiningSettings( miningSettings );
		algorithm.setMiningAlgorithmSpecification( miningAlgorithmSpecification );
	    algorithm.setExportTransactIds(true);
	    algorithm.setExportTransactItemNames( AssociationRulesMiningModel.EXPORT_PMML_NAME_TYPE_XELOPES );
	    try {
	    	algorithm.verify();
	    } catch (Exception e)
		{
	    	m_SystemMessageHandler.appendMessage("Please specify all algorithm settings correctly in building the Association model.");
	    	throw new AppException("Please specify all algorithm settings correctly in building the Association model.");
	    }
		
		/* Build the mining model */
		MiningModel model = algorithm.buildModel();
		m_SystemMessageHandler.appendMessage(Resource.srcStr("calculationtime")+" [s]: " + algorithm.getTimeSpentToBuildModel()+Resource.srcStr("ms"));
		m_SystemMessageHandler.nextLine();
		m_SystemMessageHandler.nextLine();		
		
		/* set output mining data and model to the output mining object */ 
		aOutputBIModel.setMiningModel(model);
		aOutputBIModel.setModelName("Association_"+a_OperatorNode.getNodeID());
		m_OutputBIObject.setBIData(aOutputBIData);	
		m_OutputBIObject.setBIModel(aOutputBIModel);
	
		/* set run time parameter value to the node object (It needs to be stored in the BIML) */
		//a_OperatorNode.setParameterValue("Temporary model", aOutputBIModel.getTempBIModelPath());		
	
		//aOutputBIModel.writeTempBIModel();
			
		showRules((AssociationRulesMiningModel)model);
		//showSelection((AssociationRulesMiningModel)model);
	}
	
	//TODO
	// TO BE REMOVED
	// FOR TESTING ONLY
	  /**
	   * Show association rules and large itemsets.
	   *
	   * @param ruleModel model of basket analysis
	   * @exception MiningException cannot show rules
	   */
	  private 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();
	      @SuppressWarnings("unused") CategoricalAttribute transactId = (CategoricalAttribute)( (AssociationRulesSettings) ruleModel.getMiningSettings() ).getTransactionId();

	      // Get number of rules, large itemsets, and transactions:
	      int nLITS  = LITS.size();
	      int nRules = rules.size();
	      @SuppressWarnings("unused") 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);
	      }
	  }

		//TODO
		// TO BE REMOVED
		// FOR TESTING ONLY
	  /**
	   * Demonstrates selection.
	   *
	   * @param ruleModel model of basket analysis
	   * @exception MiningException cannot show rules
	   */
	  @SuppressWarnings({ "unused", "unchecked" })
	private 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();

	      CategoricalAttribute itemId = (CategoricalAttribute)( (AssociationRulesSettings) ruleModel.getMiningSettings() ).getItemId();
	      // Select all rules containing items of the set {1,3}:
	      Vector vec = new Vector();
	      // Throw exception when "1", "3" are not valid valid category in itemId Attribute
	      vec.add("1");
	      vec.add("3");
	      SelectAttribute sa = new SelectAttribute(itemId.getName(), 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 + -