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

📄 sequentialminingmodel.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
          sequenceRules.add(rs);
        };
    }

    // -----------------------------------------------------------------------
    //  Other export methods
    // -----------------------------------------------------------------------
    /**
     * Exports large sequences or sequence rules in transactional format.
     * Depending on the misExportType one of the following methods is called:
     * toMISLargeSequences or toMISSequenceRules. <p>
     *
     * Nice format to be stored in databases or for further selections
     * by the Multidimensional stream.
     *
     * @return sequence rules as mining input stream
     * @throws MiningException cannot export as mining input stream
     */
    public MiningInputStream toMiningInputStream() throws MiningException
    {
      if (misExportType == EXPORT_MIS_LARGE_SEQUENCES)
        return toMISLargeSequences() ;
      else if (misExportType == EXPORT_MIS_SEQUENCE_RULES)
        return toMISSequenceRules();
      else
        throw new MiningException("invalid misExportType specified");
    }

    /**
     * Exports large sequences in transactional format of the following
     * form:
     * 1. Attribute - sequenceNumber,
     * 2. Attribute - itemId,
     * 3. Attribute - position (like itemIndex),
     * [4. 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 sequences as mining input stream
     * @throws MiningException cannot export as mining input stream
     */
    public MiningInputStream toMISLargeSequences() 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 sequences");

      OrdinalAttribute sequenceNumber = new OrdinalAttribute("sequenceNumber");
      CategoricalAttribute itemId = (CategoricalAttribute)( (SequentialSettings) miningSettings ).getItemId();
      NumericAttribute position = new NumericAttribute("position");
      position.setDataType( NumericAttribute.INTEGER );
      metaData.addMiningAttribute(sequenceNumber);
      metaData.addMiningAttribute(itemId);
      metaData.addMiningAttribute(position);

      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 sequences:
      int nTrans = getNumberOfTransactions();
      int nSeq   = sequentialRules.size();
      for (int i = 0; i < nSeq; i++) {
        double ki = sequenceNumber.addCategory( new Category( String.valueOf(i) ) );

        // Get itemset:
        ItemSetSeq iss = (ItemSetSeq) sequentialRules.elementAt(i);
        int itemSize   = iss.getSize();

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

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

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

      return msd;
    }

    /**
     * Exports sequence rules in transactional format of the following
     * form:
     * 1. Attribute - ruleNumber,
     * 2. Attribute - itemId,
     * 3. Attribute - position (like itemIndex),
     * 4. Attribute - isConclusion,
     * [Attributes from set {support, confidence, coverage, lift}].
     * 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 sequence rules as mining input stream
     * @throws MiningException cannot export rules as mining input stream
     */
    public MiningInputStream toMISSequenceRules() throws MiningException
    {
      // Required characteristics:
      boolean useSupp = false;
      boolean useConf = false;
      boolean useCov  = false;
      boolean useLift = false;
      boolean useCos  = false;
      if ( (misExportCharType & EXPORT_MISCHAR_SUPPORT) == EXPORT_MISCHAR_SUPPORT ) useSupp = true;
      if ( (misExportCharType & EXPORT_MISCHAR_CONFIDENCE) == EXPORT_MISCHAR_CONFIDENCE ) useConf = true;
      if ( (misExportCharType & EXPORT_MISCHAR_COVERAGE) == EXPORT_MISCHAR_COVERAGE ) useCov  = true;
      if ( (misExportCharType & EXPORT_MISCHAR_LIFT) == EXPORT_MISCHAR_LIFT ) useLift = true;
      if ( (misExportCharType & EXPORT_MISCHAR_COSINE) == EXPORT_MISCHAR_COSINE ) useCos = true;

      // Create and fill meta data:
      MiningDataSpecification metaData = new MiningDataSpecification("sequence rules");

      OrdinalAttribute ruleNumber = new OrdinalAttribute("ruleNumber");
      CategoricalAttribute itemId = (CategoricalAttribute)( (SequentialSettings) miningSettings ).getItemId();
      NumericAttribute position = new NumericAttribute("position");
      position.setDataType( NumericAttribute.INTEGER );
      CategoricalAttribute conclusionFlag = new CategoricalAttribute("conclusionFlag");
      conclusionFlag.setDataType( CategoricalAttribute.BOOLEAN );
      metaData.addMiningAttribute(ruleNumber);
      metaData.addMiningAttribute(itemId);
      metaData.addMiningAttribute(position);
      metaData.addMiningAttribute(conclusionFlag);

      NumericAttribute support = new NumericAttribute("support");
      if (useSupp) metaData.addMiningAttribute( support );
      NumericAttribute confidence = new NumericAttribute("confidence");
      if (useConf) metaData.addMiningAttribute( confidence );
      NumericAttribute coverage = new NumericAttribute("coverage");
      if (useCov)  metaData.addMiningAttribute( coverage );
      NumericAttribute lift0 = new NumericAttribute("lift");
      if (useLift) metaData.addMiningAttribute( lift0 );
      NumericAttribute cosine = new NumericAttribute("cosine");
      if (useCos) metaData.addMiningAttribute( cosine );

      int nAtt = metaData.getAttributesNumber();

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

      // Fill mining input stream:

      // Get number of rules:
      int nRules          = sequenceRules.size();

      // Write all sequence rules:
      double k0 = conclusionFlag.addCategory( new Category("0") );
      double k1 = conclusionFlag.addCategory( new Category("1") );
      for (int i = 0; i < nRules; i++) {
        double ki = ruleNumber.addCategory( new Category( String.valueOf(i) ) );

        // Get rule:
        RuleSetSeq rss   = (RuleSetSeq) sequenceRules.elementAt(i);
        int itemSize = rss.getSize();

        // Get characteristics of rule:
        double supp = 0;
        if (useSupp) supp = rss.getSupport() * 100;
        double conf = 0;
        if (useConf) conf = rss.getConfidence() * 100;
        double cov  = 0;
        if (useCov)  cov  = coverage(rss) * 100;
        double lift = 0;
        if (useLift) lift = lift(rss);
        double cos  = 0;
        if (useCos) cos   = cosine(rss);

        // Premise part of rule:
        ItemSetSeq is = rss.getPremise();
        int nprem     = rss.getPremise().getSize();
        for (int j = 0; j < nprem; j++) {
          int pN          = is.getItemAt(j);
          double[] values = new double[nAtt];
          int ind         = 0;
          values[ind++]   = ki;
          values[ind++]   = pN;
          values[ind++]   = j;
          values[ind++]   = k0;
          if (useSupp) values[ind++] = supp;
          if (useConf) values[ind++] = conf;
          if (useCov)  values[ind++] = cov;
          if (useLift) values[ind++] = lift;
          if (useCos)  values[ind++] = cos;

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

        // Conclusion part of rule:
        for (int j = nprem; j < itemSize; j++) {
          int pN          = rss.getConclusion().getItemAt(j-nprem);
          double[] values = new double[nAtt];
          int ind         = 0;
          values[ind++]   = ki;
          values[ind++]   = pN;
          values[ind++]   = j-nprem;
          values[ind++]   = k1;
          if (useSupp) values[ind++] = supp;
          if (useConf) values[ind++] = conf;
          if (useCov)  values[ind++] = cov;
          if (useLift) values[ind++] = lift;
          if (useCos)  values[ind++] = cos;

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

      return msd;
    }

    /**
     * Writes sequence model as plain text.
     *
     * @param writer writer for plain text model
     * @exception MiningException cannot write text
     */
    public void writePlainText( Writer writer ) throws MiningException
    {
        System.out.println("Results: ");
        int size = sequentialRules.size();
        SequentialSettings sequentialSettings = (SequentialSettings)miningSettings;
        for( int i=0; i<size; i++)
        {
            ItemSetSeq iss = (ItemSetSeq)sequentialRules.get(i);
            for(int j=0; j<iss.getSize(); j++)
            {
                int item = iss.getItemAt(j);
                Category category = (Category) ((CategoricalAttribute)sequentialSettings.getItemId()).getCategory((double)item);
                System.out.print( category.getDisplayValue()+"\t" );
            }
            System.out.println(" - support count: " + iss.getSupportCount());
        }
    }

    /**
     * Returns string representation (just few words).
     *
     * @return string representation
     */
    public String toString()
    {
        return "Sequential mining model";
    }

    /**
     * Returns sequence model as HTML string.
     *
     * @return model as HTML string
     */
    public String toHtmlString()
    {
        SequentialSettings seqset = (SequentialSettings)miningSettings;
        int transNum = getNumberOfTransactions();

        CategoricalAttribute cattr = (CategoricalAttribute)seqset.getItemId();
        int itemsNum = cattr.getCategoriesNumber();
        int seqNum = sequentialRules.size();
        String html = "<br><br>sequence (" + seqNum + "):" +
        "<table width=100% border=1 cellpadding=0>" +
        "<tr><th>sequence</th>" +
        "<th>items</th></tr>";
        for(int i=0;i<seqNum;i++)
        {
            html = html + "<tr>";
            ItemSetSeq iss = (ItemSetSeq)sequentialRules.get(i);
            html = html + "<td width=30%><font color=blue>" + (i+1) + ";&nbsp;length=" + iss.getSize() + ";&nbsp;" +
            "support=" + (iss.getSupportCount()/(double)transNum) + "</font></td>";
            int issSize = iss.getSize();
            html = html + "<td>";
            for( int j=0; j < issSize; j++ )
            {
                html = html + "<a href=#" + cattr.getCategory( iss.getItemAt( j ) ) + ">" +
                "<font color=blue><b>" + cattr.getCategory( iss.getItemAt( j ) ) + "</b></color></a>" + "&nbsp;";
            }
            html = html + "</td>";
        }
        html = html + "</tr></table>";
        return html;
    }
}

⌨️ 快捷键说明

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