📄 wekatools.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.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. * @version $Id: WekaTools.java,v 2.5 2003/08/06 11:52:57 fischer Exp $ */public class WekaTools { /** Gets an example and creates a Weka instance. */ public static Instance toWekaInstance(Example example, Instances instances, boolean setLabels) { Instance instance = new Instance(example.getNumberOfAttributes()+(setLabels?1:0)); instance.setDataset(instances); 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 (setLabels) { double v = example.getLabel(); if (Double.isNaN(v)) { instance.setClassMissing(); } else { if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(example.getLabelAttribute().getValueType(), Ontology.NOMINAL)) { try { instance.setClassValue(example.getLabelAsString()); } 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. */ public static Instances toWekaInstances(ExampleSet exampleSet, String name) { FastVector attributeVector = new FastVector(exampleSet.getNumberOfAttributes()); for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) { attributeVector.addElement(toWekaAttribute((Attribute)exampleSet.getAttribute(i))); } Attribute label = exampleSet.getLabel(); if (label != null) attributeVector.addElement(toWekaAttribute(label)); Instances instances = new Instances(name, attributeVector, exampleSet.getSize()); try { if (label != null) instances.setClassIndex(attributeVector.size()-1); } catch (Exception e) { throw new RuntimeException("Cannot set index of class: " + e); } ExampleReader r = exampleSet.getExampleReader(); while (r.hasNext()) { instances.add(toWekaInstance(r.next(), instances, label != null)); } 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.getNumberOfClasses()); for (int c = 0; c < attribute.getNumberOfClasses(); c++) nominalValues.addElement(attribute.mapIndex(c+Attribute.FIRST_CLASS_INDEX)); 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, String packageName) { JarFile jar = null; try { jar = getWekaJar(); } catch (IOException e) { return new String[0]; } List classes = new LinkedList(); Enumeration e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = (JarEntry)e.nextElement(); String name = entry.getName(); int dotClass = name.lastIndexOf(".class"); if (dotClass < 0) continue; name = name.substring(0, dotClass); name = name.replaceAll("/", "\\."); if (!name.startsWith(packageName)) continue; try { Class c = Class.forName(name); if (superclass.isAssignableFrom(c)) { if (!java.lang.reflect.Modifier.isAbstract(c.getModifiers())) { classes.add(name); } } } catch (Throwable t) { } } 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 + -