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

📄 wekaclusteringalgorithm.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.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Models.Clustering.ClusteringAlgorithm;

/**
  * Weka clustering algorithm.
  */
public class WekaClusteringAlgorithm extends ClusteringAlgorithm
{
  private Object wekaClusterer;

  private Object wekaInstances;

  private String wekaClassName;

  private String wekaClassParameters = null;

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

  /**
   * Builds mining model using the runAlgorithm method.
   *
   * @return model of type WekaClusteringMiningModel
   * @exception MiningException cannot build model
   */
  public MiningModel buildModel() throws MiningException
  {
      runAlgorithm();
      WekaClusteringMiningModel model = new WekaClusteringMiningModel( wekaClusterer);
      model.setWekaInstances( wekaInstances );
      model.setMiningSettings( miningSettings );
      model.setInputSpec( applicationInputSpecification );
      model.setClusters( getClusters() );
      this.miningModel = model;
      return model;
  }

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

    try {
      // Weka clusterer class:
      Class wekaClustererClass = Class.forName("weka.clusterers.Clusterer");

      // 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 = { wekaClustererClass, wekaClassName, params };
      Object wekaAlgorithm = forNameMethod.invoke( wekaUtils, forNameArgs );

      // Get build clustering method:
      Class wekaInstancesClass             = Class.forName("weka.core.Instances");
      Class wekaAlgoClass                  = wekaAlgorithm.getClass();
      Class[] buildClustererArgumentTypes  = { wekaInstancesClass };
      Method classMethod = wekaAlgoClass.getMethod("buildClusterer", buildClustererArgumentTypes);

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

      // Run Weka's buildClusterer 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 clusterer object:
      wekaClusterer = wekaAlgorithm;
    }
    catch (Exception ex) {
      ex.printStackTrace();
        throw new MiningException( " could not run weka algorithm successfully ");
    };

  }

  /**
   * 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
   */
  @SuppressWarnings("unchecked")
private String[] splitParameters(String paramString) {

    Vector paramsVec   = new Vector();
    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] = (String) paramsVec.elementAt(i);
    };

    return params;
  }
}

⌨️ 快捷键说明

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