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

📄 abstractexampleset.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  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.example;

import edu.udo.cs.yale.Statistics;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.ResultObjectAdapter;
import edu.udo.cs.yale.operator.Saveable;
import edu.udo.cs.yale.gui.SwingTools;
import edu.udo.cs.yale.gui.PlotterPanel;

import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.*;
import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;

/** Implements wrapper methods of abstract example set. 
 *  Implements all ResultObject methods.<br> 
 *
 *  Apart from the interface methods the implementing classes must have a public single 
 *  argument clone constructor. This constructor is invoked by reflection from the clone method.
 *  Do not forget to call the superclass method. 
 *
 *  @version $Id: AbstractExampleSet.java,v 2.43 2004/09/17 12:57:37 ingomierswa Exp $
 */
public abstract class AbstractExampleSet extends ResultObjectAdapter implements ExampleSet, Saveable {

    /** Zero argument constructor for subclasses. */
    public AbstractExampleSet() {
    }

    /** Clone constructor. Does nothing. */
    public AbstractExampleSet(AbstractExampleSet exampleSet) {
    }

    public ExampleReader getExampleReader() {
	return new WeightingExampleReader(getExampleTable().getDataReader(), this);
    }

    /** Adds all given attributes to this example set. Ignores all attributes which had been already added. */
    public void addAllAttributes(Collection c) {
	Iterator i = c.iterator();
	while (i.hasNext()) {
	    Attribute attribute = (Attribute)i.next();
	    // TODO: check if this check before adding is ok!!!
	    if (!contains(attribute))
		addAttribute(attribute);
	    // old version:
            //addAttribute((Attribute)i.next());
	}
    }

    public void removeAllAttributes() {
	while (getNumberOfAttributes() > 0)
	    removeAttribute(getAttribute(0));
    }

    public Attribute removeAttribute(int index) {
	Attribute attr = getAttribute(index);
	removeAttribute(attr);
	return attr;
    }

    public void setAttributes(ExampleSet exampleSet) {
	removeAllAttributes();
	for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
	    addAttribute(exampleSet.getAttribute(i));
	}
    }

    public Attribute getLabel() { return getAttribute(LABEL_NAME); }
    public void setLabel(Attribute label) { setSpecialAttribute(LABEL_NAME, label); }
    public Attribute getPredictedLabel() { return getAttribute(PREDICTION_NAME); }
    public void setPredictedLabel(Attribute predictedLabel) { setSpecialAttribute(PREDICTION_NAME, 
										  predictedLabel); }
    public void clearPredictedLabel() { setSpecialAttribute(PREDICTION_NAME, null); }
    public Attribute getWeight() { return getAttribute(WEIGHT_NAME); }
    public void setWeight(Attribute weight) { setSpecialAttribute(WEIGHT_NAME, weight); }
    public Attribute getCluster() { return getAttribute(CLUSTER_NAME); }
    public void setCluster(Attribute cluster) { setSpecialAttribute(CLUSTER_NAME, cluster); }
    public Attribute getIdAttribute() { return getAttribute(ID_NAME); }
    public void setIdAttribute(Attribute idAttribute) { setSpecialAttribute(ID_NAME, idAttribute); }

    public Attribute createWeightAttribute() {
	Attribute weight = getWeight();
	if (weight != null) {
	    LogService.logMessage("ExampleSet.createWeightAttribute(): Overwriting old weight attribute!",
				  LogService.WARNING);
	}
	weight = createSpecialAttribute(WEIGHT_NAME, Ontology.REAL);

	// TODO: check if this initializing is ok!
	DataRowReader reader = getExampleTable().getDataReader();
	while (reader.hasNext()) {
	    DataRow data = reader.next();
	    data.set(weight, 1.0d);
	}
	return weight;
    }

    public Attribute createClusterAttribute() {
	Attribute cluster = getCluster();
	if (cluster != null) {
	    LogService.logMessage("ExampleSet.createClusterAttribute(): Overwriting old cluster attribute!",
				  LogService.WARNING);
	}
	return createSpecialAttribute(CLUSTER_NAME, Ontology.CLUSTER);
    }
    
    public Attribute createSpecialAttribute(String name, int valueType) {
	Attribute attribute = new Attribute(Attribute.createName(name),
					    valueType,
					    Ontology.SINGLE_VALUE,
					    Attribute.UNDEFINED_BLOCK_NR,
					    null);
	getExampleTable().addAttribute(attribute);
	setSpecialAttribute(name, attribute);
	return attribute;
    }

    public boolean contains(Attribute attribute) {
	return getAttribute(attribute.getName()) != null;
    }

    // -------------------- Visualisation and toString() methods --------------------

    public String toString() {
        StringBuffer str = new StringBuffer(Tools.classNameWOPackage(this.getClass())+": ");
	str.append(getSize() + " examples\n");
	str.append("       attributes = {\n");

	if (getNumberOfAttributes() >= getAttributeLimit()) {
	    str.append("..."+getNumberOfAttributes()+" attributes...");
	} else {
	    for (int i = 0; i < getNumberOfAttributes(); i++) {
		Attribute att = getAttribute(i);
		str.append((i == 0 ? "" : ",\n") + "         " + att.toString());
		if (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(att.getBlockType(), Ontology.VALUE_SERIES_START)) {
		    int end = getBlockEndIndex(i);
		    str.append(",..., "+(end-i-1)+" attributes,...");
		    i = end-1;
		}
	    }
	}
	str.append("\n       }");

	Iterator i = getSpecialAttributeNames().iterator();
	while (i.hasNext()) {
	    String name = (String)i.next();
	    str.append("\n       "+name+" = " +getAttribute(name));
	}
        return str.toString();
    }

    private int getAttributeLimit() {
	int attributeLimit = Integer.MAX_VALUE;
  	String max = System.getProperty("yale.gui.resultviewer.attributelimit");
  	if (max != null) {
  	    try {
  		attributeLimit = Integer.parseInt(max);
  	    } catch (NumberFormatException e) {
  		System.err.println("Value of yale.gui.resultviewer.attributelimit must be an integer!");
  	    }
  	}
	return attributeLimit;
    }

    private int getExampleLimit() {
	int exampleLimit = Integer.MAX_VALUE;
  	String max = System.getProperty("yale.gui.resultviewer.examplelimit");
  	if (max != null) {
  	    try {
  		exampleLimit = Integer.parseInt(max);
  	    } catch (NumberFormatException e) {
  		System.err.println("Value of yale.gui.resultviewer.examplelimit must be an integer!");
  	    }
  	}
	return exampleLimit;
    }

    /** Returns a html description of the example set. */
    protected String toHTML() {
	StringBuffer buffer = new StringBuffer("");
	buffer.append("<h1>"+edu.udo.cs.yale.tools.Tools.classNameWOPackage(this.getClass())+"</h1>");
	buffer.append("<b>Number of examples:</b> "+getSize()+"<br>");
	buffer.append("<b>Number of attributes:</b> "+getNumberOfAttributes()+"<br>");
	buffer.append("<table bgcolor=\"#E3D8C3\" border=\"1\">");
	buffer.append("<tr bgcolor=\"#ccccff\"><th>Index</th><th>Name</th><th>Generated from</th><th>Unit</th><th>Type</th><th>Blocktype</th><th>Blocknr.</th><th>Values</th></tr>");
	buffer.append("<tr bgcolor=\"#C3B8A3\"><td colspan=\"8\" border=\"0\"><b>Regular attributes</b></td></tr>");
	for (int i = 0; i < getNumberOfAttributes(); i++) {
	    buffer.append(getAttribute(i).toHTML());
	    if ((i >= getAttributeLimit() - 1) && (getAttributeLimit() != -1))  {
 		buffer.append("<tr><td colspan=\"8\">... "+(getNumberOfAttributes() - getAttributeLimit() -1)+" attributes ... </td></tr>");
		buffer.append(getAttribute(getNumberOfAttributes()-1).toHTML());
		break;
	    }	    
	}

	Iterator i = getSpecialAttributeNames().iterator();
	while (i.hasNext()) {
	    String name = (String)i.next();
	    specialAttributeToHTML(getAttribute(name), name, buffer);
	}
	buffer.append("</table>");
	return buffer.toString();
    }

    private static void specialAttributeToHTML(Attribute attribute, String name, StringBuffer buffer) {
	if (attribute != null) {
	    buffer.append("<tr bgcolor=\"#C3B8A3\"><td colspan=\"8\" border=\"0\"><b>"+name+"</b></td></tr>");
	    buffer.append(attribute.toHTML());
	}
    }

    /** Returns a html label with a table view or a plotter for statistic view. */
    public Component getVisualisationComponent() {
	final JPanel mainPanel = new JPanel();
	mainPanel.setLayout(new BorderLayout());

	// html table view
  	final JLabel label = new JLabel("<html>" + toHTML() + "</html>");
  	label.setBorder(javax.swing.BorderFactory.createEmptyBorder(11,11,11,11));
  	label.setFont(label.getFont().deriveFont(java.awt.Font.PLAIN));

	mainPanel.add(label, BorderLayout.CENTER);

	if ((getSize() <= getExampleLimit()) || (getExampleLimit() == -1))  {
	    // statistics plotter view
	    Statistics stats = new Statistics("Example Set");
	    String[] columnNames = new String[getNumberOfAttributes() + getSpecialAttributeNames().size()];
	    for (int i = 0; i < getNumberOfAttributes(); i++)
		columnNames[i] = getAttribute(i).getName();
	    Iterator s = getSpecialAttributeNames().iterator();
	    int k = 0;
	    while (s.hasNext()) {
		columnNames[getNumberOfAttributes() + k] = (String)s.next();

⌨️ 快捷键说明

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