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

📄 associationrulesminingmodel.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.AssociationModel
     */
    public Object createPmmlObject() throws MiningException
    {
        int numberOfItemsets = largeItemSets.size();
        int numberOfRules = associationRules.size();

        double minimumSupport = ((AssociationRulesSettings)miningSettings).getMinimumSupport();
        double minimumConfidence = ((AssociationRulesSettings)miningSettings).getMinimumConfidence();

        CategoricalAttribute productsId = (CategoricalAttribute)((AssociationRulesSettings)miningSettings).getItemId();
        CategoricalAttribute transactId = (CategoricalAttribute)((AssociationRulesSettings)miningSettings).getTransactionId();

        int productsNumber = productsId.getCategoriesNumber();
        int transactsNumber = getNumberOfTransactions();

        AssociationModel associationModel = new AssociationModel();
        associationModel.setNumberOfTransactions( "" + transactsNumber );
        //assocInputStats.setMaxNumberOfItemsPerTA( "" );
        //assocInputStats.setAvgNumberOfItemsPerTA( "" );
        associationModel.setMinimumSupport( "" + minimumSupport );
        associationModel.setMinimumConfidence( "" + minimumConfidence );
        //assocInputStats.setLengthLimit( "" );
        associationModel.setNumberOfItems( "" + productsNumber );
        associationModel.setNumberOfItemsets( "" + numberOfItemsets  );
        associationModel.setNumberOfRules( "" + numberOfRules );
//        associationModel.setAssocInputStats( assocInputStats );
        associationModel.setModelName("Association rules");
        associationModel.setFunctionName("associationRules");
        associationModel.setAlgorithmName(algorithm);

        if (exportTransactItemNames == EXPORT_PMML_NAME_TYPE_XELOPES) {
          associationModel.setItemIdName(productsId.getName());
          associationModel.setTransactIdName(transactId.getName());
        };

        if( inputSpec != null )
        {
            associationModel.setMiningSchema( (MiningSchema)inputSpec.createPmmlObject() );
        }

        Item[] assocItem = new Item[productsNumber];
        for(int i = 0; i < productsNumber; i++)
        {
            assocItem[i] = new Item();
            assocItem[i].setId( "" + /*productsId.getValue( */i/* )*/ );
            assocItem[i].setValue( "" + productsId.getCategory( i ) );
        }
        associationModel.setItem( assocItem );

        Hashtable is2id = new Hashtable();
        long idCounter = 1;

        Itemset[] assocItemset = new Itemset[numberOfItemsets];
        for( int i = 0; i < numberOfItemsets; i++ )
        {
            assocItemset[i] = new Itemset();
            ItemSet is = (ItemSet)largeItemSets.elementAt(i);
            double support = (double) is.getSupportCount() / (double) getNumberOfTransactions();
            int itemSize = is.getSize();
            ItemRef[] assocItemRef = new ItemRef[itemSize];
            for( int j = 0; j < itemSize; j++ )
            {
                assocItemRef[j] = new ItemRef();
                int pN = is.getItemAt(j);
                assocItemRef[j].setItemRef( "" + pN );
            }
            String id = "id"+idCounter;
            idCounter++;
            is2id.put(is,id);
            assocItemset[i].setItemRef( assocItemRef );
            assocItemset[i].setId( id );
            assocItemset[i].setSupport( "" + support );
            assocItemset[i].setNumberOfItems( "" + itemSize );
        }
        associationModel.setItemset( assocItemset );

        AssociationRule[] assocRule = new AssociationRule[numberOfRules];
        for(int i = 0; i < numberOfRules; i++)
        {
            RuleSet rs = (RuleSet) associationRules.elementAt(i);
            double support    = rs.getSupport();
            double confidence = rs.getConfidence();
            int itemSize = rs.getSize();
            ItemSet premise = rs.getPremise();
            ItemSet conclusion = rs.getConclusion();
            int nprem    = premise.getSize();
            int nconcl = conclusion.getSize();

            assocRule[i] = new AssociationRule();
            assocRule[i].setSupport( "" + support );
            assocRule[i].setConfidence( "" + confidence );
            String id = (String)is2id.get(premise);
            assocRule[i].setAntecedent( id );
            id = (String)is2id.get(conclusion);
            assocRule[i].setConsequent( id );
        }
        associationModel.setAssociationRule( assocRule );

        return associationModel;
    }

    /**
     * Reads PMML document of association rule model.
     * PMMLs AssociationModel is used.
     *
     * @param reader reader for PMML model
     * @exception MiningException cannot read PMML document
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.AssociationModel
     */
    public void readPmml( Reader reader ) throws MiningException
    {
//        com.borland.xml.toolkit.XmlUtil.setEncoding( "UTF-8" );
        PMML pmml = PMML.unmarshal( reader );
        if (pmml.getAssociationModelCount()==0)
          throw new MiningException("no association model found");

        // Read data dictionary:
        DataDictionary dictionary = pmml.getDataDictionary();
        MiningDataSpecification newMetaData = new MiningDataSpecification();
        newMetaData.parsePmmlObject( dictionary );

        // Read transformation dictionary:
        TransformationDictionary transDict = pmml.getTransformationDictionary();
        if (transDict != null) {
          MiningTransformationActivity mta = new MiningTransformationActivity();
          mta.parsePmmlObject(transDict);
          MiningDataSpecification tmds = mta.transform(newMetaData);
          tmds.setPretransformedMetaData(newMetaData);
          newMetaData = tmds;
          newMetaData.setMiningTransformationActivity( mta );
          newMetaData.setTransformed(true);
        };

        // Init settings:
        AssociationRulesSettings ars = new AssociationRulesSettings();
        ars.setDataSpecification(newMetaData);
        miningSettings = ars;

        // Read association rules model:
        AssociationModel model = pmml.getAssociationModel(0);
        parsePmmlObject(model);
    }

    /**
     * Reads PMML AssociationModel.
     *
     * @param pmmlObject object of AssociationModel
     * @exception MiningException cannot parse PMML object
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.AssociationModel
     */
    public void parsePmmlObject( Object pmmlObject ) throws MiningException
    {
        largeItemSets = new Vector();
        associationRules = new Vector();
        AssociationModel model = (AssociationModel)pmmlObject;
        AssociationRulesSettings ars = (AssociationRulesSettings)miningSettings;
        ars.setMinimumConfidence(Double.parseDouble(model.getMinimumConfidence()));
        ars.setMinimumSupport(Double.parseDouble(model.getMinimumSupport()));
        if (model.getItemIdName() != null) {
          itemIdName = model.getItemIdName();
          exportTransactItemNames = this.EXPORT_PMML_NAME_TYPE_XELOPES;
        };
        if (model.getTransactIdName() != null) {
          transactIdName = model.getTransactIdName();
          exportTransactItemNames = this.EXPORT_PMML_NAME_TYPE_XELOPES;
        };
        MiningDataSpecification metaData = ars.getDataSpecification();
        CategoricalAttribute itemId = (CategoricalAttribute)metaData.getMiningAttribute(itemIdName);
        ars.setItemId(itemId);
        CategoricalAttribute transactId = (CategoricalAttribute)metaData.getMiningAttribute(transactIdName);
        ars.setTransactionId(transactId);
        MiningSchema schema = model.getMiningSchema();
        if( schema != null )
        {
            inputSpec = new ApplicationInputSpecification();
            inputSpec.parsePmmlObject( schema );
            inputSpec.initApplicationInputSpecification( ars );
        }
        else
        {
            inputSpec = new ApplicationInputSpecification( ars.getDataSpecification() );
            inputSpec.initApplicationInputSpecification( ars );
        }
        Hashtable itemsMap = new Hashtable();
        Hashtable itemsetsMap = new Hashtable();
        Itemset[] itemsets = model.getItemset();
        Item[] items = model.getItem();
        int transNum = Integer.parseInt(model.getNumberOfTransactions());
        setNumberOfTransactions(transNum);
        for(int i=0;i<itemsets.length;i++)
        {
          ItemSet is = new ItemSet();
          double supp = Double.parseDouble(itemsets[i].getSupport());
          is.setSupportCount((int)(transNum*supp));
          String iid = itemsets[i].getId();
          itemsetsMap.put(iid,is);
          ItemRef[] itemRefs = itemsets[i].getItemRef();
          for(int j=0;j<itemRefs.length;j++)
          {
            String ref = itemRefs[j].getItemRef();
            Integer value = (Integer)itemsMap.get(ref);
            if(value == null)
            {
              int k;
              for(k=0;k<items.length;k++)
                if(items[k].getId().equals(ref)) break;
              if(k == items.length)  throw new MiningException("itemset has bad item reference");
              Category cat = new Category( items[k].getValue(), items[k].getValue(), new CategoryProperty() );
              double dbl = itemId.getKey(cat);
              value = new Integer((int)dbl);
              itemsMap.put(ref,value);
            }
            is.addItem(value.intValue());
          }
          largeItemSets.add(is);
        }
        AssociationRule[] rules = model.getAssociationRule();
        for(int i=0;i<rules.length;i++)
        {
          ItemSet antecedent = (ItemSet)itemsetsMap.get(rules[i].getAntecedent());
          ItemSet consequent = (ItemSet)itemsetsMap.get(rules[i].getConsequent());
          if(antecedent == null || consequent == null)
            throw new MiningException("bad itemset reference in association rule");
          double supp = Double.parseDouble(rules[i].getSupport());
          double conf = Double.parseDouble(rules[i].getConfidence());
          RuleSet rs = new RuleSet(antecedent,consequent,supp,conf);
          associationRules.add(rs);
        }
    }

    // -----------------------------------------------------------------------
    //  Other export methods
    // -----------------------------------------------------------------------
    /**
     * Exports large itemsets or association rules in transactional format.
     * Depending on the misExportType one of the following methods is called:
     * toMISLargeItemSets, toMISAssociationRules, toMISAssociationRulesPairwise. <p>
     *
     * Nice format to be stored in databases or for further selections
     * by the Multidimensional stream.
     *
     * @return association rules as mining input stream
     * @throws MiningException cannot export into mining input stream
     */
    public MiningInputStream toMiningInputStream() throws MiningException
    {
      if (misExportType == EXPORT_MIS_LARGE_ITEMSETS)
        return toMISLargeItemSets() ;
      else if (misExportType == EXPORT_MIS_ASSOCIATION_RULES)
        return toMISAssociationRules();
      else if (misExportType == EXPORT_MIS_ASSOCIATION_RULES_FLAT)
        return toMISAssociationRulesFlat();
      else
        throw new MiningException("invalid misExportType specified");
    }

    /**
     * Exports large itemsets in transactional format of the following
     * form:
     *  1. Attribute - itemSetNumber,
     *  2. Attribute - itemId,
     * [3. Attribute - support].
     * Inclusion of support attriute depends on misExportCharType. <p>
     *
     * Nice format to be stored in databases or for further selections
     * by the Multidimensional stream.
     *
     * @return large itemsets as mining input stream
     * @throws MiningException cannot export into mining input stream
     */
    public MiningInputStream toMISLargeItemSets() throws MiningException
    {
      // Required characteristics:
      boolean useSupp = false;
      if ( (misExportCharType & EXPORT_MISCHAR_SUPPORT) == EXPORT_MISCHAR_SUPPORT ) useSupp = true;

      // Create and fill meta data:
      MiningDataSpecification metaData = new MiningDataSpecification("large itemsets");

      OrdinalAttribute isNumber = new OrdinalAttribute("itemSetNumber");
      CategoricalAttribute itemId = (CategoricalAttribute)( (AssociationRulesSettings) miningSettings ).getItemId();
      metaData.addMiningAttribute(isNumber);
      metaData.addMiningAttribute(itemId);

      NumericAttribute support = new NumericAttribute("support");
      if (useSupp) metaData.addMiningAttribute(support);
      int nAtt = metaData.getAttributesNumber();

      // Create mining input stream and assign meta data:
      MiningStoredData msd = new MiningStoredData();
      msd.setMetaData(metaData);

      // Fill mining input stream:

      // Write all large itemsets:
      int nTrans = getNumberOfTransactions();
      int nLITS  = largeItemSets.size();
      for (int i = 0; i < nLITS; i++) {
        double ki = isNumber.addCategory( new Category( String.valueOf(i) ) );
        // Get itemset:
        ItemSet is   = (ItemSet) largeItemSets.elementAt(i);
        int itemSize = is.getSize();

        // Get support of LITS:
        double supp = 0;
        if (useSupp) {
          supp = is.getSupportCount();
          if (nTrans > 0) supp = 100*supp/nTrans;
        }

        // Add itemset to MIS:
        for (int j = 0; j < itemSize; j++) {
          int pN          = is.getItemAt(j);
          double[] values = new double[nAtt];
          values[0]       = ki;
          values[1]       = pN;
          if (useSupp) values[2] = supp;

          MiningVector mv = new MiningVector(values);
          mv.setMetaData(metaData);
          msd.add(mv);
        };
      }

      return msd;
    }

    /**
     * Exports association rules in transactional format of the following
     * form:
     * 1. Attribute - ruleNumber,
     * 2. Attribute - itemId,
     * 3. Attribute - isConclusion,
     * [Attributes from set {support, confidence, coverage, lift, cosine}].
     * The last included attriutes of rule characteristics depend
     * on misExportCharType. <p>
     *
     * Nice format to be stored in databases or for further selections
     * by the Multidimensional stream.
     *
     * @return association rules as mining input stream
     * @throws MiningException cannot export into mining input stream
     */
    public MiningInputStream toMISAssociationRules() throws MiningException
    {
      // Required characteristics:
      boolean useSupp = false;
      boolean useConf = false;
      boolean useCov  = false;
      boolean useLift = false;
      boolean useCos  = false;

⌨️ 快捷键说明

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