📄 wekaclusteringminingmodel.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 com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningMatrixElement;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Models.Clustering.ClusteringMiningModel;
/**
* Representation of a Weka clustering model.
*/
public class WekaClusteringMiningModel extends ClusteringMiningModel
{
private Object wekaClusterer = null;
private Object wekaInstances = null;
/**
* Empty constructor.
*/
public WekaClusteringMiningModel()
{
}
/**
* Constructor with weka clusterer object.
*
* @param wekaClusterer clusterer obtained from weka
* @exception MiningException wekaClusterer is not assignable to weka.clusterers.Clusterer
*/
public WekaClusteringMiningModel(Object wekaClusterer) throws MiningException
{
try {
// Check for assinable class:
Class argClassType = wekaClusterer.getClass();
Class<?> wekaClustererClass = Class.forName("weka.clusterers.Clusterer");
if (! wekaClustererClass.isAssignableFrom(argClassType))
throw new Exception(wekaClustererClass.getName() + " is not assignable from "
+ argClassType);
// Assign:
this.wekaClusterer = wekaClusterer;
}
catch (Exception ex) {
throw new MiningException( "Weka class exception." );
};
}
/**
* Set Weka instances for applying vectors.
*
* @param wekaInstances weka instances to apply
*/
public void setWekaInstances(Object wekaInstances) {
this.wekaInstances = wekaInstances;
}
/**
* Applies Weka clustering model to mining vector returning the number
* of the cluster it belongs to.
*
* @param miningVector mining vector to be assigned to a cluster
* @return number of the cluster the vector belongs to
* @throws MiningException if there are some errors when model is applied
* @deprecated since version 1.1, use applyModel instead
*/
public int apply(MiningVector miningVector) throws MiningException
{
return (int) applyModelFunction(miningVector);
}
/**
* Applies function of Weka clustering model to a mining vector.
* The mining vector is converted to a Weka instance and then its
* clusterInstance method is applied. The meta data of the mining vector
* should be similar to the metaData of this class. This ensures
* compatibility of training and application data.
*
* @param miningVector mining vector where the model should be applied
* @return function value of the mining vector
* @throws MiningException if there are some errors when model is applied
*/
public double applyModelFunction(MiningVector miningVector)
throws MiningException
{
try {
Object wekaInstance = WekaCoreAdapter.PDMMiningVector2WekaInstance( miningVector,
wekaInstances );
Class wekaClustererClass = Class.forName("weka.clusterers.Clusterer");
Class wekaInstanceClass = Class.forName("weka.core.Instance");
Class[] methodArgumentTypes = { wekaInstanceClass };
Method classMethod = wekaClustererClass.getMethod("clusterInstance", methodArgumentTypes);
Object[] instance = { wekaInstance };
Integer res = (Integer) classMethod.invoke(wekaClusterer, instance);
return res.intValue();
}
catch (Exception ex) {
ex.printStackTrace();
throw new MiningException("Mining vector could not be clustered accurately.");
}
}
/**
* Returns the cluster object of the mining vector given as argument.
* Not implemented since in Weka there is no general Cluster class.
*
* @param miningData mining data where the model should be applied
* @return data resulting from the model application
* @throws MiningException always thrown
*/
public MiningMatrixElement applyModel(MiningMatrixElement miningData)
throws MiningException {
throw new MiningException("Not implemented.");
}
/**
* Returns the number of clusters calling Weka's numberOftClusters
* method.
*
* @return number of clusters
* @exception MiningException could not get number of clusters
*/
public int getNumberOfClusters() throws MiningException {
try {
Class wekaClustererClass = Class.forName("weka.clusterers.Clusterer");
Class parameterTypes = null;
Method classMethod = wekaClustererClass.getMethod("numberOfClusters", parameterTypes);
Integer res = (Integer) classMethod.invoke(wekaClusterer, parameterTypes);
return res.intValue();
}
catch (Exception ex) {
ex.printStackTrace();
throw new MiningException("Could not get the number of clusters.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -