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

📄 exampleset.java.old

📁 著名的开源仿真软件yale
💻 OLD
📖 第 1 页 / 共 3 页
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002 *      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.example;import edu.udo.cs.yale.operator.FatalException;import edu.udo.cs.yale.operator.OperatorException;import edu.udo.cs.yale.MethodNotSupportedException;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.TempFileService;import edu.udo.cs.yale.tools.RandomGenerator;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.operator.ResultObjectAdapter;import edu.udo.cs.yale.operator.performance.PerformanceVector;import edu.udo.cs.yale.generator.FeatureGenerator;import edu.udo.cs.yale.gui.SwingTools;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.io.FileWriter;import java.io.IOException;import java.util.Comparator;import java.util.List;import java.util.LinkedList;import java.util.Iterator;import java.util.ArrayList;import java.util.ListIterator;import java.util.HashMap;import java.util.Map;import java.util.Enumeration;import java.awt.Component;import java.awt.event.ActionEvent;import javax.swing.JLabel;import javax.swing.AbstractAction;/** Instances of this class are a view on an ExampleTable. *  <ul> *    <li><tt>exampleTable</tt>: The ExampleTable containing the actual data. *    <li><tt>partition</tt>: ExampleSets can be partitioned so that the ExampleReaders returned by this ExampleSet *                            return only a subset of all examples. This is used e.g. for cross validation. Partitions *                            can be nested. *    <li><tt>attributeReferences</tt>: A list of <code>AttributeReference</code>s which reference a column in the *                                      ExampleTable. They can be switched on and off. *    <li><tt>performance</tt>: If an ExampleSet was evaluated in the course of a feature selection algorithm *                              the performance is cached so that it need not be recalculated *                              gesetzt werden. Dies verhindert, das nicht ver&auml;nderte ExampleSets erneut evaluiert werden. *  </ul> * *  <h4>TO DO</h4> *  <b>NOTE:</b> Right now there can be only one cluster attribute. Hence, cluster experiments are not *  entirely nestable, because new cluster attributes will overwrite an existing cluster attribute. *  We have to add a stack of cluster attributes. * *  @author simon, ingo *  @version $Id: ExampleSet.java,v 1.28 2003/02/28 11:49:18 fischer Exp $ */public class ExampleSet extends ResultObjectAdapter implements Cloneable {    /* History:     *     * Temporarily done:     *   2002/04/23, Ralf: avoid output of attribute sets with several thousands of attributes;     *                     remove temporary code marked with tag "RK/2002/04/23: TMP" later.     *     * Still to do:     *   -> methods 'createPredictedLabel', 'createWeightAttribute', 'createClusterAttribute':     *      make sure, that this attribute (or another attribute of the same name does not already exist);     *   -> method 'createPredictedLabel':  the case 'label == null' needs to be considered;     *      this case occurs for example, if the 'ExampleSet' constructor is called with 'label == null';     */    /** Maps attribute names to their index. */    private Map nameToIndexMap = new HashMap();    public static final Comparator PERFORMANCE_COMPARATOR =	new Comparator() {		public int compare(Object o1, Object o2) {		    return ((ExampleSet)o1).getPerformance().compareTo(((ExampleSet)o2).getPerformance());		}	    };    /** The partition used for multisplit. */    private Partition partition = null;    /** The ExampleReader provider. */    private ExampleTable exampleTable;    /** List of AttributeReference */    private List attributeReferences;    /** The true label of this example set. */    private Attribute label;    /** The predicted label of this example set. */    private Attribute predictedLabel;    /** A numerical attribute that can be used as a weight for examples. */    private Attribute weight;    /** An integer attribute that can be used for clustering and partitioning     *  the example set. */    private Attribute cluster;    /** The index of the selected cluster or -1. */    private int selectedCluster = -1;    /** the performance of this attribute set. */    private PerformanceVector performance;     /** Zeigt an, da&szlig; die InformationGain Werte der Attribute neu berechnet werden sollten.     */    private boolean shouldRecalculateInformationGain;    /** Der kleinste information gain wert der vorkommt.     */    private double smallestInformationGain;    private class SaveDataAction extends AbstractAction {	private SaveDataAction() {	    super("Save data...");	    putValue(SHORT_DESCRIPTION, "Writes the data to file.");	}	public void actionPerformed(ActionEvent e) {	    File file = SwingTools.chooseFile(null, null, false);	    if (file != null) {		try {		    PrintWriter out = new PrintWriter(new FileWriter(file));		    ExampleReader reader = getExampleReader();		    while (reader.hasNext()) {			out.println(reader.next().toString());		    }		    out.close();		} catch (IOException ex) {		    SwingTools.showErrorMessage("Could not write to example set file '"+file+"'", ex);		}	    }	}    }    {	addAction(new SaveDataAction());    }    /** Creates a new example set from a given ExampleTable and AttributeReferences. */    public ExampleSet(ExampleTable exampleTable, 		      List attributeReferences, 		      Attribute label,		      Attribute predictedLabel,		      Attribute weight,		      Attribute cluster) {	this.attributeReferences = attributeReferences;	this.exampleTable        = exampleTable;	this.label               = label;	this.predictedLabel      = predictedLabel;	this.weight              = weight;	this.cluster             = cluster;	this.nameToIndexMap      = new HashMap();	this.mapReferences();	shouldRecalculateInformationGain = true;    }    /** Clones the ExampleSet. */    protected ExampleSet(ExampleSet exampleSet) {	this.partition = null;	if (exampleSet.partition != null)	    this.partition = (Partition)exampleSet.partition.clone();	this.attributeReferences = new ArrayList();	ListIterator i = exampleSet.attributeReferences.listIterator();	while (i.hasNext()) {	    this.attributeReferences.add(((AttributeReference)i.next()).clone());    	}	this.label          = exampleSet.label;	this.predictedLabel = exampleSet.predictedLabel;	this.weight         = exampleSet.weight;	this.cluster        = exampleSet.cluster;	this.exampleTable   = exampleSet.exampleTable;	this.performance = exampleSet.performance;	this.nameToIndexMap = new HashMap();	this.mapReferences();	this.smallestInformationGain = exampleSet.smallestInformationGain;	this.shouldRecalculateInformationGain = exampleSet.shouldRecalculateInformationGain;    }    public Object clone() {	return new ExampleSet(this);    }        public ExampleTable getExampleTable() {	return exampleTable;    }    // --------------------------------------------------------------------------------    /** Maps all names of attribute references to their index in the list.     */    private void mapReferences() {	nameToIndexMap.clear();	ListIterator i = attributeReferences.listIterator();	int index = 0;	while (i.hasNext()) 	    mapReference((AttributeReference)i.next(), index++);    }    /** Maps the name of the attribute reference to its index in the list.     */    private void mapReference(AttributeReference reference, int index) {	String name = reference.getAttribute().getName();	nameToIndexMap.put(name, new Integer(index));    }    /** Returns the index of the attribute with the given name or -1 of no such attribute is present.     */    public int mapName(String name) {	//// RK/2002/04/23: old version:	// return ((Integer)nameToIndexMap.get(name)).intValue();	//	//// RK/2002/04/23: new version:	if (nameToIndexMap.containsKey(name)) {	    return ((Integer)nameToIndexMap.get(name)).intValue();	}	else {	    int     n = getNumberOfAttributes();	    String  availableNames = "{";	    if (n > 10) {		for (int i=0; i < 5; i++)    availableNames += " "+mapIndex(i);		availableNames += " ...";		for (int i=n-5; i < n; i++)  availableNames += " "+mapIndex(i);	    } else {		for (int i=0; i < n; i++)    availableNames += " "+mapIndex(i);	    }	    availableNames += " }";	    LogService.logMessage("ExampleSet.mapName(String attributeName): The given attribute name '" + 				  name + "' is not among the already stored attributes names!\n" +				  "Stored names = " + availableNames + "\n", LogService.WARNING);	    return -1;	}    }    /** Returns the name of the index-th Attribute.     */    public String mapIndex(int index) {	return getAttribute(index).getName();    }    // --------------------------------------------------------------------------------    /** Returns the number of selected examples. */    public int getSize() {	if (topPartition() != null) {	    return topPartition().selectionSize();	} else {	    return exampleTable.getSize();	}    }    /** Returns the i-th attribute reference. */    public AttributeReference getAttributeReference(int i) { 	return (AttributeReference)attributeReferences.get(i);     }    /** Sets the i-th attribute reference to ref. */    public void setAttributeReference(int i, AttributeReference ref) { 	performance = null;	shouldRecalculateInformationGain = true;	attributeReferences.set(i, ref); 	mapReferences();    }    /** Sets the attribute references of this example set to the AttribtueReferences of exampleSet. */    public void setAttributeReferences(ExampleSet exampleSet) { 	performance = null;	shouldRecalculateInformationGain = true;	attributeReferences = exampleSet.attributeReferences;	mapReferences();    }

⌨️ 快捷键说明

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