📄 wekatools.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.tools;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.example.ListDataRowReader;
import edu.udo.cs.yale.example.DoubleArrayDataRow;
import edu.udo.cs.yale.example.ExampleReader;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.io.IOException;
/** This class contains static methods for converting
* <a href="http://www.cs.waikato.ac.nz/~ml/weka/">Weka</a> Instances
* to Yale ExampleSet and vice versa.
*
* @version $Id: WekaTools.java,v 2.18 2004/09/01 16:04:46 ingomierswa Exp $
*/
public class WekaTools {
/** Invokes toYaleExampleSet(instances, null). */
public static ExampleSet toYaleExampleSet(Instances instances) {
return toYaleExampleSet(instances, null);
}
/** Creates a Yale example set from Weka instances. Only a label can be used as special attributes, other
* types of special attributes are not supported. If attributeNames is not null, the string is used as
* prefix plus a number as attribute names. */
public static ExampleSet toYaleExampleSet(Instances instances, String attributeNames) {
int classIndex = instances.classIndex();
// create example table
List attributes = new LinkedList();
int number = 1; // use for attribute names
for (int i = 0; i < instances.numAttributes(); i++) {
weka.core.Attribute wekaAttribute = instances.attribute(i);
Attribute attribute = new Attribute(wekaAttribute.name(),
(wekaAttribute.isNominal() ? Ontology.NOMINAL : Ontology.REAL),
Ontology.SINGLE_VALUE,
Attribute.UNDEFINED_BLOCK_NR,
null);
if ((i != classIndex) && (attributeNames != null)) {
attribute.setName(attributeNames + "_" + (number++));
}
attributes.add(attribute);
}
Attribute label = (Attribute)attributes.get(classIndex);
label.setName("label");
MemoryExampleTable table = new MemoryExampleTable(attributes);
// create data
List dataList = new LinkedList();
int numberOfYaleAttributes = instances.numInstances() - ((classIndex >= 0) ? 1 : 0);
for (int i = 0; i < instances.numInstances(); i++) {
Instance instance = instances.instance(i);
double[] data = new double[numberOfYaleAttributes];
for (int a = 0; a < instances.numAttributes(); a++) {
double wekaValue = instance.value(a);
if (table.getAttribute(a).isNominal()) {
String nominalValue = instances.attribute(a).value((int)wekaValue);
data[a] = table.getAttribute(a).mapString(nominalValue);
} else {
data[a] = wekaValue;
}
}
dataList.add(new DoubleArrayDataRow(data));
}
// handle label extra
table.readExamples(new ListDataRowReader(dataList.iterator()));
// create and return example set
return table.createCompleteExampleSet(label, null, null, null);
}
/** Gets an example and creates a Weka instance. */
public static Instance toWekaInstance(Example example,
Instances instances,
Attribute label,
boolean labelKnown) {
Instance instance = new Instance(example.getNumberOfAttributes() + (label != null ? 1 : 0));
instance.setDataset(instances);
if (example.getWeightAttribute() != null)
instance.setWeight(example.getWeight());
for (int a = 0; a < example.getNumberOfAttributes(); a++) {
double v = example.getValue(a);
if (Double.isNaN(v)) {
instance.setMissing(a);
} else {
if (example.getAttribute(a).isNominal()) {
try {
instance.setValue(a, example.getValueAsString(a));
} catch (Exception e) {
throw new RuntimeException("Cannot set attribute value: " + e);
}
} else {
instance.setValue(a, v);
}
}
}
if (label != null) {
if (!labelKnown) instance.setClassMissing();
else {
double v = example.getValue(label);
if (Double.isNaN(v)) {
instance.setClassMissing();
} else {
if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(label.getValueType(),
Ontology.NOMINAL)) {
try {
instance.setClassValue(example.getAsString(label, v));
} catch (Exception e) {
throw new RuntimeException("Cannot set class value: " + e);
}
} else {
try {
instance.setClassValue(v);
} catch (Exception e) {
throw new RuntimeException("Cannot set class value: " + e);
}
}
}
}
}
return instance;
}
/** Gets an ExampleSet and creates Weka instances. The label attribute has must be the true label (for
* regular learning), the predicted label (for prediction with learned models) or null (for association
* learning, clustering, etc.) The parameter labelKnown indicates if the label is known (e.g. for learning)
* or missing (e.g. for prediction). */
public static Instances toWekaInstances(ExampleSet exampleSet,
String name,
Attribute label,
boolean labelKnown) {
FastVector attributeVector = new FastVector(exampleSet.getNumberOfAttributes());
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
attributeVector.addElement(toWekaAttribute(exampleSet.getAttribute(i)));
}
if (label != null) attributeVector.addElement(toWekaAttribute(label));
Instances instances = new Instances(name, attributeVector, exampleSet.getSize());
if (label != null) instances.setClassIndex(attributeVector.size()-1);
else instances.setClassIndex(-1);
ExampleReader r = exampleSet.getExampleReader();
while (r.hasNext()) {
instances.add(toWekaInstance(r.next(), instances, label, labelKnown));
}
return instances;
}
/** Converts an {@link Attribute} to a Weka attribute. */
public static weka.core.Attribute toWekaAttribute(Attribute attribute) {
if (attribute == null) return null;
if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NOMINAL)) {
FastVector nominalValues = new FastVector(attribute.getValues().size());
Iterator c = attribute.getValues().iterator();
while (c.hasNext()) {
nominalValues.addElement((String)c.next());
}
return new weka.core.Attribute(attribute.getName(), nominalValues);
} else {
return new weka.core.Attribute(attribute.getName());
}
}
public static JarFile getWekaJar() throws IOException {
return new JarFile(ParameterService.getLibraryFile("weka.jar"));
}
public static String[] getWekaClasses(Class superclass) {
return getWekaClasses(superclass, null, true);
}
public static String[] getWekaClasses(Class superclass, String packageName, boolean includePackage) {
JarFile jar = null;
try {
jar = getWekaJar();
} catch (IOException e) {
return new String[0];
}
List classes = new LinkedList();
Tools.findImplementationsInJar(jar, superclass, classes);
if (packageName != null) {
Iterator i = classes.iterator();
while (i.hasNext()) {
if (includePackage && (((String)i.next()).indexOf(packageName) == -1)) i.remove();
if (!includePackage && (((String)i.next()).indexOf(packageName) != -1)) i.remove();
}
}
String[] names = new String[classes.size()];
classes.toArray(names);
return names;
}
public static String[] getWekaParameters(List yaleParameters) {
String[] parameters = new String[yaleParameters.size()*2];
Iterator i = yaleParameters.iterator();
int j = 0;
while (i.hasNext()) {
Object[] parameter = (Object[])i.next();
parameters[j++] = "-"+(String)parameter[0];
parameters[j++] = (String)parameter[1];
}
return parameters;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -