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

📄 wekasupervisedminingalgorithm.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 Michael Thess
  * @version 1.0
  */

package com.prudsys.pdm.Adapters.Weka;

import java.lang.reflect.Method;
import java.util.StringTokenizer;
import java.util.Vector;

import com.prudsys.pdm.Core.ApplicationAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Models.Supervised.Classifier;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningAlgorithm;

/**
  * Weka supervised mining algorithm.
  */
public class WekaSupervisedMiningAlgorithm extends SupervisedMiningAlgorithm
{

  private WekaClassifier wekaClassifier;

  private String wekaClassName;

  private String wekaClassParameters = null;

  /**
   * Default constructor.
   */
  public WekaSupervisedMiningAlgorithm()
  {
  }

  /**
   * Builds mining model using the runAlgorithm method.
   *
   * @return model of type WekaSupervisedMiningModel
   * @exception MiningException cannot build model
   */
  public MiningModel buildModel() throws MiningException
  {
      runAlgorithm();
      WekaSupervisedMiningModel model = new WekaSupervisedMiningModel();
      model.setMiningSettings( miningSettings );
      model.setInputSpec( applicationInputSpecification );
      model.setClassifier( getClassifier() );
      ApplicationAttribute target = applicationInputSpecification.getTargetApplicationAttribute();
      model.setTarget( target );
      this.miningModel = model;
      return model;
  }

  /**
   * Runs the Weka algorithm.
   *
   * @exception MiningException something went wrong
   */
  protected void runAlgorithm() throws MiningException {

    try {
      // Weka classifier class:
      Class wekaClassifierClass = Class.forName("weka.classifiers.Classifier");

      // Create Weka algorithm using Weka's Utils.forName method:
      Class wekaUtilsClass = Class.forName("weka.core.Utils");
      Object wekaUtils     = wekaUtilsClass.newInstance();

      String[] args = new String[10]; // or any other arbitrary number
      Class[] forNameArgumentTypes = { Class.class, String.class, args.getClass() };
      Method forNameMethod = wekaUtilsClass.getMethod("forName", forNameArgumentTypes);

      String[] params = null;
      if (wekaClassParameters != null)
        params = splitParameters( wekaClassParameters );

      Object[] forNameArgs = { wekaClassifierClass, wekaClassName, params };
      Object wekaAlgorithm = forNameMethod.invoke( wekaUtils, forNameArgs );

      // Get build classifier method:
      Class wekaInstancesClass             = Class.forName("weka.core.Instances");
      Class<? extends Object> wekaAlgoClass                  = wekaAlgorithm.getClass();
      Class[] buildClassifierArgumentTypes = { wekaInstancesClass };
      Method classMethod = wekaAlgoClass.getMethod("buildClassifier", buildClassifierArgumentTypes);

      // Get Weka instances from mining stream:
      Object wekaInstances = WekaCoreAdapter.PDMMiningInputStream2WekaInstances(
                             getMiningInputStream() );

      // Set classifying attribute taken from target attribute:
      Class[] setClassIndexArgumentTypes = { int.class };
      Method setClassIndexMethod         = wekaInstancesClass.getMethod("setClassIndex",
                                           setClassIndexArgumentTypes);
      int indTarget  = getMetaData().getAttributeIndex( target );
      Object[] index = { new Integer(indTarget) };
      setClassIndexMethod.invoke( wekaInstances, index );

      // Run Weka's buildClassifier method:
      long start = ( new java.util.Date() ).getTime();

      Object[] instances = { wekaInstances };
      classMethod.invoke(wekaAlgorithm, instances);

      long end = ( new java.util.Date() ).getTime();
      timeSpentToBuildModel = ( end - start ) / 1000.0;

      // Create classifier object:
      wekaClassifier = new WekaClassifier( wekaAlgorithm );
      wekaClassifier.setWekaInstances( wekaInstances );
    }
    catch (Exception ex) {
      ex.printStackTrace(); 
      //throw new MiningException( " could not run weka algorithm successfully ");
      throw new MiningException(ex.getCause().getMessage()); //Modify to be more specific. TWang. Apri 4, 2004.
    };

  }

  /**
   * Returns Weka classifier.
   *
   * @return classifier of type WekaClassifier
   */
  protected Classifier getClassifier() {

    return wekaClassifier;
  }

  /**
   * Returns class name of the weka algorithm.
   *
   * @return class name of weka algorithm
   */
  public String getWekaClassName() {

    return wekaClassName;
  }

  /**
   * Sets class name of the weka algorithm.
   *
   * @param wekaClassName new name of weka algorithm
   */
  public void setWekaClassName(String wekaClassName) {

    this.wekaClassName = wekaClassName;
  }

  /**
   * Returns class parameters of the weka algorithm.
   *
   * @return class parameters of weka algorithm
   */
  public String getWekaClassParameters() {

    return wekaClassParameters;
  }

  /**
   * Sets class parameters of the weka algorithm.
   *
   * @param wekaClassParameters new class parameters of weka algorithm
   */
  public void setWekaClassParameters(String wekaClassParameters) {

    this.wekaClassParameters = wekaClassParameters;
  }

  /**
   * Split up a string containing parameters into an array of strings,
   * one for each parameter.
   *
   * @param paramString the string containing the parameters of the weka class
   * @return the array of parameters
   */
  private String[] splitParameters(String paramString) {

    Vector<String> paramsVec   = new Vector<String>();
    StringTokenizer st = new StringTokenizer(paramString);
    while (st.hasMoreTokens())
      paramsVec.addElement(st.nextToken());

    String [] params = new String[paramsVec.size()];
    for (int i = 0; i < paramsVec.size(); i++) {
      params[i] = paramsVec.elementAt(i);
    };

    return params;
  }
}

⌨️ 快捷键说明

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