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

📄 simpleexampleset.java

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

import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;

import java.util.ArrayList;
import java.util.Set;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Collection;

/** A simple implementation of ExampleSet without any specials. */
public class SimpleExampleSet extends AbstractExampleSet {

    /** The list of attributes contained in this example set. */
    private ArrayList attributes = new ArrayList();

    /** Maps attribute names to names. */
    private Map attributeNameMap = new HashMap();

    /** A map to store user data in. */
    private Map userDataMap = new HashMap();

    /** Maps names of named special attributes to {@link Attribute}s */
    private Map specialAttributeMap = new HashMap();

    /** The table used for reading the examples from. */
    private ExampleTable exampleTable;

    /** Clone constructor. */
    public SimpleExampleSet(SimpleExampleSet exampleSet) {
	super(exampleSet);
	this.exampleTable   = exampleSet.exampleTable;
	this.specialAttributeMap.putAll(exampleSet.specialAttributeMap);
	this.attributeNameMap.putAll(exampleSet.attributeNameMap);
	this.attributes.addAll(exampleSet.attributes);
	this.userDataMap.putAll(exampleSet.userDataMap);
    }

    /** Private setter constructor. */
    private SimpleExampleSet(Attribute label,
  			     Attribute predictedLabel,
  			     Attribute weight,
  			     Attribute idAttribute) {
  	this.setLabel(label);
  	this.setPredictedLabel(predictedLabel);
  	this.setWeight(weight);
  	this.setIdAttribute(idAttribute);
    }
    
    /** Constructs a new SimpleExampleSet backed by the given example table.
     *  The example set initially does not have any special attributes. */
    public SimpleExampleSet(ExampleTable exampleTable, Collection regularAttributes) {
	this.exampleTable = exampleTable;
	addAllAttributes(regularAttributes);
    }

    /** Constructs a new SimpleExampleSet backed by the given example table.
     *  All attributes in the table apart from the special attributes become normal 
     *  attributes. */
    public SimpleExampleSet(ExampleTable exampleTable, 
			    Attribute label,
			    Attribute predictedLabel,
			    Attribute weight,
			    Attribute idAttribute) {
	this(label, predictedLabel, weight, idAttribute);
	this.exampleTable   = exampleTable;
	addRegularAttributesFromExampleTable(exampleTable);
    }

    /** Constructs a new SimpleExampleSet backed by the given example table.
     *  All attributes in the table apart from the special attributes become normal 
     *  attributes. The special attributes are specified by the given map. */
    public SimpleExampleSet(ExampleTable exampleTable, 
			    Map specialAttributes) {
	this.exampleTable   = exampleTable;

	Iterator i = specialAttributes.keySet().iterator();
	while (i.hasNext()) {
	    String name = (String)i.next();
	    setSpecialAttribute(name, (Attribute)specialAttributes.get(name));
	}

	addRegularAttributesFromExampleTable(exampleTable);
    }

    /** Adds all attributes from the given example table as regular attributes if they are not already 
     *  known as special attributes. */
    private void addRegularAttributesFromExampleTable(ExampleTable exampleTable) {
	for (int i = 0; i < exampleTable.getNumberOfAttributes(); i++) {
	    Attribute attribute = exampleTable.getAttribute(i);
	    if (!isSpecialAttribute(attribute))
		addAttribute(attribute);
	}
    }

    /** Returns true if one of the special attributes has the same index like the given one. */
    public boolean isSpecialAttribute(Attribute attribute) {
	Iterator i = specialAttributeMap.keySet().iterator();
	while (i.hasNext()) {
	    String name = (String)i.next();
	    if (((Attribute)specialAttributeMap.get(name)).getIndex() == attribute.getIndex())
		return true;
	}
	return false;
    }

    public void addAttribute(Attribute attribute) { 
	if (attributeNameMap.get(attribute.getName()) != null)
	    throw new RuntimeException("Attribute name already used: "+attribute.getName());
	attributeNameMap.put(attribute.getName(), attribute);
	attributes.add(attribute); 
	clearUserData();
    }

    /** Removes the attribute from the attribute list which has the same name like the given one. */
    public void removeAttribute(Attribute attribute) { 
	// since the equals method of attribute checks only for the construction description,
	// the last index of the given attribute must be calculated here.
	boolean removed = false;
	Iterator i = attributes.iterator();
	while (i.hasNext() && !removed) {
	    Attribute candidate = (Attribute)i.next();
	    if (candidate.getName().equals(attribute.getName())) {
		i.remove();
		removed = true;
	    }
	}
	if (!removed)
	    throw new java.util.NoSuchElementException("Attribute "+attribute.getName() + " is not in example set.");
	attributeNameMap.remove(attribute.getName());
	clearUserData();
    }

    public ExampleTable getExampleTable() { return exampleTable; }

    public int getNumberOfAttributes() { return attributes.size();  }

    public Attribute getAttribute(int index) { return (Attribute)attributes.get(index); }

    /** Returns the attribute with the given name. Returns null if no regular and no special attribute with this name exists. */
    public Attribute getAttribute(String name) { 
	Attribute attribute = (Attribute)attributeNameMap.get(name);
	if (attribute == null)
	    attribute = (Attribute)specialAttributeMap.get(name);
	return attribute;
    }
    
    public void setSpecialAttribute(String name, Attribute attribute) { 
	if (attribute != null) {
	    specialAttributeMap.put(name, attribute); 
	} else {
	    specialAttributeMap.remove(name);
	}
    }

    public Collection getSpecialAttributeNames() {
	return specialAttributeMap.keySet();
    }

    
    public int getSize() { return exampleTable.getSize(); }

    // -------------------- user data --------------------

    public void clearUserData() {
	userDataMap.clear();
    }

    public void setUserData(String key, Object data) {
	if (key == null) throw new IllegalArgumentException("Key must not be null!");
	userDataMap.put(key, data);
    }

    public Object getUserData(String key) {
	if (key == null) throw new IllegalArgumentException("Key must not be null!");
	Object data = userDataMap.get(key);
	return data;
    }

    public Set getUserDataKeys() {
	return userDataMap.keySet();
    }
}

⌨️ 快捷键说明

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