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

📄 wekaclassifier.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.io.Serializable;
import java.lang.reflect.Method;

import weka.core.Instance;
import weka.core.Instances;

import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Models.Supervised.Classifier;

/**
  * Representation of Weka classifier.
  */
public class WekaClassifier implements Classifier, Serializable
{

  /**
	 * 
	 */
	private static final long serialVersionUID = -8041386236244931976L;
private Object wekaClassifier = null;
  private Object wekaInstances  = null;

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

  /**
   * Constructor with weka classifier object.
   *
   * @param wekaClassifier classifier obtained from weka
   * @exception Exception wekaClassifier is not assignable to weka.classifiers.Classifier
   */
  public WekaClassifier(Object wekaClassifier) throws Exception
  {
    // Check for assinable class::
    Class argClassType        = wekaClassifier.getClass();
    Class<?> wekaClassifierClass = Class.forName("weka.classifiers.Classifier");
    if (! wekaClassifierClass.isAssignableFrom(argClassType)) {
		throw new Exception(wekaClassifierClass.getName() + " is not assignable from "
				  + argClassType);
	}

    // Assign:
    this.wekaClassifier = wekaClassifier;
  }

  /**
   * Set Weka instances for applying vectors.
   *
   * @param wekaInstances weka instances to apply
   */
  public void setWekaInstances(Object wekaInstances) {

    this.wekaInstances = wekaInstances;
  }

  /**
   * Application of the classifier to a new mining vector.
   * The mining vector is converted to a Weka instance and then its
   * classifyInstance method is applied.
   *
   * @param miningVector mining vector to be classified
   * @return resulting score value of classification/regression for presented vector
   * @exception MiningException mining vector could not be classified accurately
   */
  public double apply(MiningVector miningVector) throws MiningException {

    try {
      Object wekaInstance = WekaCoreAdapter.PDMMiningVector2WekaInstance( miningVector,
                            wekaInstances );

      Class wekaClassifierClass = Class.forName("weka.classifiers.Classifier");
      Class wekaInstanceClass   = Class.forName("weka.core.Instance");

      Class[] methodArgumentTypes = { wekaInstanceClass };
      Method classMethod = wekaClassifierClass.getMethod("classifyInstance", methodArgumentTypes);

      Object[] instance = { wekaInstance };
      Double res = (Double) classMethod.invoke(wekaClassifier, instance);

      return res.doubleValue();

    }
    catch (Exception ex) {
      ex.printStackTrace();
      throw new MiningException(" mining vector could not be classified accurately ");
    }
  }

  //<<17/03/2005, Frank J. Xu
  /**
   * Application of the classifier to a new mining vector.
   * The mining vector is converted to a Weka instance and then its
   * classifyInstance method is applied.
   *
   * @param miningVector mining vector to be classified
   * @return resulting score value of classification/regression for presented vector
   * @exception MiningException mining vector could not be classified accurately
   */
  public double apply(MiningVector miningVector, Object a_wekaInstances) throws MiningException {

    try {
      Object wekaInstance = WekaCoreAdapter.PDMMiningVector2WekaInstance( miningVector,
      																		a_wekaInstances );

      Class wekaClassifierClass = Class.forName("weka.classifiers.Classifier");
      Class wekaInstanceClass   = Class.forName("weka.core.Instance");

      Class[] methodArgumentTypes = { wekaInstanceClass };
      Method classMethod = wekaClassifierClass.getMethod("classifyInstance", methodArgumentTypes);
      
      //set DataSet before invoke the classify method.
      ((Instance)wekaInstance).setDataset((Instances)wekaInstances);

      Object[] instance = { wekaInstance };
      Double res = (Double) classMethod.invoke(wekaClassifier, instance);

      return res.doubleValue();

    }
    catch (Exception ex) {
      ex.printStackTrace();
      throw new MiningException(" mining vector could not be classified accurately ");
    }
  }  
  
  public Object getWekaInstancesofClassifier(){
    return wekaInstances;  	
  }
  //17/03/2005, Frank J. Xu>>
  
  /**
	 * @return Returns the wekaClassifier.
	 */
	public Object getWekaClassifier() {
		return wekaClassifier;
	}
}

⌨️ 快捷键说明

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