📄 wekacoreadapter.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.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.CategoryProperty;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Input.Records.Arff.MiningArffStream;
/**
* Contains transformation from PDM to basic Weka elements:
* Attribute, Instance, Instances.
*/
public class WekaCoreAdapter {
/**
* Creates WEKA Attribute from MiningAttribute.
*
* @param attribute mining attribute to be converted
* @return Weka attribute
* @exception Exception cannot create WEKA attribute
*/
public static Object PDMAttribute2WekaAttribute(MiningAttribute attribute)
throws Exception {
// New weka attribute:
Object wekaAttribute = null;
// Weka's attribute class:
Class wekaAttClass = Class.forName("weka.core.Attribute");
// Numeric attribute
if (attribute instanceof NumericAttribute) {
// Get constructor for numeric attributes:
Class[] constructorArgumentTypes = { String.class };
Constructor classConstructor = wekaAttClass.getConstructor(
constructorArgumentTypes);
// Set constructor argument 'name':
Object[] constructorArgs = { attribute.getName() };
// Create Weka Attribute:
wekaAttribute = classConstructor.newInstance(constructorArgs);
}
// Categorical attribute:
else {
// Weka's fast vector class:
Class wekaFastVectorClass = Class.forName("weka.core.FastVector");
// Create fast vector object:
Object fastVector = wekaFastVectorClass.newInstance();
// Get addElement method:
Class[] methodArgumentTypes = { Object.class };
Method classMethod = wekaFastVectorClass.getMethod("addElement", methodArgumentTypes);
// Get all values of categorical attribute:
ArrayList values = ((CategoricalAttribute) attribute).getValues();
// Copy to fast vector:
for (int i = 0; i < values.size(); i++) {
Object[] newValue = { ((Category) values.get(i)).getDisplayValue() };
classMethod.invoke(fastVector, newValue);
};
if (values.size() == 0)
fastVector = null;
// Get constructor for categorical attributes:
Class[] constructorArgumentTypes = { String.class, wekaFastVectorClass };
Constructor classConstructor = wekaAttClass.getConstructor(
constructorArgumentTypes);
// Set constructor arguments 'name', values:
Object[] constructorArgs = { attribute.getName(), fastVector };
// Create Weka Attribute:
wekaAttribute = classConstructor.newInstance(constructorArgs);
};
return wekaAttribute;
}
/**
* Creates WEKA Instance from MiningVector.
*
* @param vector mining vector to be converted
* @param instances Weka's dataset of Instances class
* @return Weka instance
* @exception Exception cannot create WEKA instance
*/
public static Object PDMMiningVector2WekaInstance(MiningVector vector, Object instances)
throws Exception {
// New weka instance:
Object wekaInstance = null;
// Weka's instance class:
Class wekaInstClass = Class.forName("weka.core.Instance");
// Get constructor for instance:
Class[] constructorArgumentTypes = { int.class };
Constructor classConstructor = wekaInstClass.getConstructor(
constructorArgumentTypes);
// Set constructor argument number of attributes:
int numbAtt = vector.getValues().length;
Object[] constructorArgs = { new Integer(numbAtt) };
// Create Weka Instance:
wekaInstance = classConstructor.newInstance(constructorArgs);
// Get setDataset method:
Class[] methodArgTypes = { Class.forName("weka.core.Instances") };
Method classMethod = wekaInstClass.getMethod("setDataset", methodArgTypes);
if (instances != null) {
Object[] dataset = { instances };
classMethod.invoke(wekaInstance, dataset);
};
// Get setValue method:
Class[] methodArgumentTypes0 = { int.class };
Method classMethod0 = wekaInstClass.getMethod("setMissing", methodArgumentTypes0);
Class[] methodArgumentTypes1 = { int.class, double.class };
Method classMethod1 = wekaInstClass.getMethod("setValue", methodArgumentTypes1);
Class[] methodArgumentTypes2 = { int.class, String.class };
Method classMethod2 = wekaInstClass.getMethod("setValue", methodArgumentTypes2);
// Copy values to instance:
for (int i = 0; i < numbAtt; i++) {
// Get value:
double value = vector.getValue(i);
// Set missing value:
if ( Category.isMissingValue( value ) ) {
Object[] newArgs = { new Integer(i) };
classMethod0.invoke(wekaInstance, newArgs);
}
// Copy data of numeric attribute:
else if (vector.getMetaData() == null ||
vector.getMetaData().getMiningAttribute(i) instanceof NumericAttribute) {
Object[] newArgs = { new Integer(i), new Double( value ) };
classMethod1.invoke(wekaInstance, newArgs);
}
// Copy data of categorical attribute:
else {
Category categ = ((CategoricalAttribute) vector.getMetaData().getMiningAttribute(i)).getCategory( value);
Object[] newArgs = { new Integer(i), categ.getDisplayValue() };
classMethod2.invoke(wekaInstance, newArgs);
};
};
return wekaInstance;
}
/**
* Creates Weka Instances from MiningInputStream.
*
* @param mis mining input stream to be converted
* @return Weka instances
* @exception Exception cannot create Weka instances
*/
public static Object PDMMiningInputStream2WekaInstances(MiningInputStream mis)
throws Exception {
// New weka instances:
Object wekaInstances = null;
// Weka's fast vector class:
Class wekaFastVectorClass = Class.forName("weka.core.FastVector");
// Create fast vector object:
Object fastVector = wekaFastVectorClass.newInstance();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -