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

📄 customersequentialbuild.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

 /**
  * Title: XELOPES Data Mining Library
  * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
  * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
  * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
  * @author Carsten Weisse
  * @author Michael Thess
  * @version 1.0
  */
package com.prudsys.pdm.Examples;

import java.io.FileWriter;
import java.util.Vector;

import com.prudsys.pdm.Automat.MiningAutomationAssignment;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAlgorithmSpecification;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.Records.Arff.MiningArffStream;
import com.prudsys.pdm.Models.AssociationRules.ItemSet;
import com.prudsys.pdm.Models.CustomerSeq.CustomRuleSetSeq;
import com.prudsys.pdm.Models.CustomerSeq.CustomSequence;
import com.prudsys.pdm.Models.CustomerSeq.CustomerSeqNumberAssessment;
import com.prudsys.pdm.Models.CustomerSeq.CustomerSeqNumberCallback;
import com.prudsys.pdm.Models.CustomerSeq.CustomerSequentialAlgorithm;
import com.prudsys.pdm.Models.CustomerSeq.CustomerSequentialMiningModel;
import com.prudsys.pdm.Models.CustomerSeq.CustomerSequentialSettings;
import com.prudsys.pdm.Utils.GeneralUtils;
import com.prudsys.pdm.Utils.PmmlUtils;

/**
 * Builds a sequential basket analysis model and writes it to the
 * PMML file 'CustomerSequentialModel.xml'.
 */
public class CustomerSequentialBuild extends BasisExample {

  /**
   * Empty constructor.
   */
  public CustomerSequentialBuild() {
  }

  /**
   * Run the example of this class.
   *
   * @throws Exception error while example is running
   */
  public void runExample() throws Exception {

    // Open data source and get metadata:
    MiningInputStream inputData = new MiningArffStream( "data/arff/custom-transact.arff" );
    MiningDataSpecification metaData = inputData.getMetaData();

    // Get transactional attributes:
    CategoricalAttribute categoryItemId = (CategoricalAttribute)metaData.getMiningAttribute( "itemId" );
    CategoricalAttribute categoryCustomerId = (CategoricalAttribute)metaData.getMiningAttribute( "customerId" );
    NumericAttribute categoryTransactionPosition = (NumericAttribute)metaData.getMiningAttribute( "transactionPosition" );

    // Create MiningSettings object and assign metadata:
    CustomerSequentialSettings miningSettings = new CustomerSequentialSettings();
    miningSettings.setDataSpecification( metaData );

    // Assign settings:
    miningSettings.setItemId( categoryItemId );
    miningSettings.setCustomerId( categoryCustomerId );
    miningSettings.setTransactionPosition( categoryTransactionPosition );
    miningSettings.setMinimumSupport( 0.35 );
    miningSettings.setGenerateRules(true);
    miningSettings.setMinimumConfidence(0.3);
    miningSettings.verifySettings();

    // Get default mining algorithm specification from 'algorithms.xml':
    MiningAlgorithmSpecification miningAlgorithmSpecification =
      MiningAlgorithmSpecification.getMiningAlgorithmSpecification( "Customer", null );
    if( miningAlgorithmSpecification == null )
      throw new MiningException( "Can't find application Customer." );

    // Get class name from algorithms specification:
    String className = miningAlgorithmSpecification.getClassname();
    if( className == null )
      throw new MiningException( "classname attribute expected." );

    // Set and display mining algorithm specification parameters:
    miningAlgorithmSpecification.setMAPValue("minimumItemSize", "1");
    miningAlgorithmSpecification.setMAPValue("maximumItemSize", "-1");
    miningAlgorithmSpecification.setMAPValue("doPruning", "false");
    GeneralUtils.displayMiningAlgSpecParameters(miningAlgorithmSpecification);

    // Create automation parameter, if automation is required:
    MiningAutomationAssignment maa = new MiningAutomationAssignment();
    maa.setMiningModelAssessment( new CustomerSeqNumberAssessment() );
    maa.setMiningAutomationCallback( new CustomerSeqNumberCallback() );
    maa.setMinAssessment(10);
    maa.setMaxAssessment(150);

    // Create algorithm object with default values:
    CustomerSequentialAlgorithm algorithm = (CustomerSequentialAlgorithm)
      GeneralUtils.createMiningAlgorithmInstance(className);

    // Put it all together:
    algorithm.setMiningInputStream( inputData );
    algorithm.setMiningSettings( miningSettings );
    algorithm.setMiningAlgorithmSpecification( miningAlgorithmSpecification );
    algorithm.setMiningAutomationAssignment( maa );
    algorithm.verify();

    // Build the mining model:
    MiningModel model = algorithm.buildModelWithAutomation();
    System.out.println("calculation time [s]: " + algorithm.getTimeSpentToBuildModel());

    // Show results:
    showRules((CustomerSequentialMiningModel) model);

    // Write to PMML:
    FileWriter writer = new FileWriter("data/pmml/CustomerSequentialModel.xml");
    model.writePmml(writer);

    // Show in browser:
    if (debug == 2) PmmlUtils.openPmmlBrowser("CustomerSequentialModel.xml");
  }

  /**
   * Example of building a sequential basket analysis model.
   *
   * @param args arguments (ignored)
   */
  public static void main(String[] args) {

    try {
      new CustomerSequentialBuild().runExample();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Show customer sequences.
   *
   * @param custModel model of customer sequential analysis
   * @exception MiningException cannot show rules
   */
  public static void showRules(CustomerSequentialMiningModel custModel)
    throws MiningException {

      // Get all sequences from model:
      Vector seq   = custModel.getSequentialRules();
      Vector rules = custModel.getSequenceRules();

      // Get item and transaction attributes:
      CategoricalAttribute itemId = (CategoricalAttribute)( (CustomerSequentialSettings) custModel.getMiningSettings() ).getItemId();
      // Get number of sequences and transactions:
      int nSeq            = seq.size();
      int nRules          = 0;
      if (rules != null) nRules = rules.size();
      int transactsNumber = custModel.getNumberOfTransactions();

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

        // Get and show rule:
        CustomRuleSetSeq crss = (CustomRuleSetSeq) rules.elementAt(i);
        // Premise part of rule:
        CustomSequence cs = (CustomSequence) crss.getPremise();
        int seqSize       = cs.getSize();

        // Go through all itemsets of premise sequence:
        for (int j = 0; j < seqSize; j++) {
          ItemSet is = (ItemSet) cs.getItemSet(j);
          // Go through sequence:
          for (int k = 0; k < is.getSize(); k++) {
            int pN        = is.getItemAt(k);
            Category cat  = (Category) itemId.getCategory(pN);
            System.out.print(cat.getValue() + " ");
          };
          if (j < seqSize - 1)
            System.out.print("| ");
        };

        System.out.print("=> ");

        // Conclusion part of rule:
        cs      = crss.getConclusion();
        seqSize = cs.getSize();

        // Go through all itemsets of premise sequence:
        for (int j = 0; j < seqSize; j++) {
          ItemSet is = (ItemSet) cs.getItemSet(j);
          // Go through sequence:
          for (int k = 0; k < is.getSize(); k++) {
            int pN        = is.getItemAt(k);
            Category cat  = (Category) itemId.getCategory(pN);
            System.out.print(cat.getValue() + " ");
          };
          if (j < seqSize - 1)
            System.out.print("| ");
        };

        // Show support and confidence of rule:
        double Support    = crss.getSupport() * 100.0;
        double Confidence = crss.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:
        custModel.buildLargeSequences();
        double Coverage = custModel.coverage(crss) * 100.0;
        double Lift     = custModel.lift(crss);
        System.out.print("Cov = " + Math.round(Coverage*100)/100.0 + ", ");
        System.out.println("Lift = " + Math.round(Lift*100)/100.0);
      };

      // Show all sequences:
      System.out.println();
      System.out.println("Number of customer sequences found: " + nSeq);
      for (int i = 0; i < nSeq; i++) {
        // New customer sequence:
        System.out.print(i + ": ");

        // Get customer sequence:
        CustomSequence cs = (CustomSequence) seq.elementAt(i);
        int seqSize       = cs.getSize();

        // Go through all itemsets of customer sequence:
        for (int j = 0; j < seqSize; j++) {
          ItemSet is = (ItemSet) cs.getItemSet(j);
          // Go through sequence:
          for (int k = 0; k < is.getSize(); k++) {
            int pN        = is.getItemAt(k);
            Category cat  = (Category) itemId.getCategory(pN);
            System.out.print(cat.getValue() + " ");
          };
          if (j < seqSize - 1)
            System.out.print("| ");
        };

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

}

⌨️ 快捷键说明

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