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

📄 example.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;

/** An example consists of attributes, one label, one predicted label, a weight and a cluser. 
 *  Tha data is backed by a DataRow. Hence, all values are actually doubles, symbolic values
 *  are mapped to integers stored in doubles.<br>
 *
 *  @author Ingo,Simon
 *  @version $Id: Example.java,v 2.22 2004/08/27 11:57:31 ingomierswa Exp $
 */
public class Example {

    /** Separator used in the getAttributesAsString() method (tab). */
    public static final String SEPARATOR = " ";

    /** Separates indices from values in sparse format (colon). */
    public static final String SPARSE_SEPARATOR = ":";

    /** The data for this example. */
    private DataRow data;
    /** The attributes used by this example. */
    private Attribute[] attributes;
    /** The label used by this example. */
    private Attribute label;
    /** The predicted label used by this example. */
    private Attribute predictedLabel;
    /** The weight attribute used by this example. */
    private Attribute weight;
    /** The cluster attribute used by this example. */
    private Attribute cluster;
    /** The id attribute used by this example. */
    private Attribute idAttribute;
    /** The weights of the attributes. */
    private AttributeWeights attributeWeights;
    /** The value of all attributes is calculated as a function f(original value, weight) by 
     *  an instance of {@link WeightApplier}. */
    private WeightApplier weightApplier;


    /** Creates a new Example that uses the Data stored in a DataRow. */
    public Example(DataRow data,
		   Attribute[] attributes,
		   AttributeWeights attributeWeights,
		   WeightApplier weightApplier,
		   Attribute label,
		   Attribute predictedLabel,
		   Attribute weight,
		   Attribute cluster,
		   Attribute idAttribute) {
	this.data             = data;
	this.attributes       = attributes;
	this.attributeWeights = attributeWeights;
	this.weightApplier    = weightApplier;
	this.label            = label;
	this.predictedLabel   = predictedLabel;
	this.weight           = weight;
	this.cluster          = cluster;
	this.idAttribute      = idAttribute;
    }

    /** Returns the i-th attribute. */
    public Attribute getAttribute(int i) {
	return attributes[i];
    }

    public Attribute getLabelAttribute() {
	return label;
    }

    public Attribute getPredictedLabelAttribute() {
	return predictedLabel;
    }

    public Attribute getIdAttribute() {
	return idAttribute;
    }

    public Attribute getWeightAttribute() {
	return weight;	
    }
	
    protected DataRow getDataRow() {
	return data;
    }

    // --------------------------------------------------------------------------------

    /** Returns the value of attribute a. The attribute a need not necessarily be 
     *  part of the example set the example is taken from, although this is no good style. */
    public double getValue(Attribute a) {
	double weight = attributeWeights.getWeight(a.getName());
	if (!a.isNumerical()) { // && !Double.isNaN(weight)) {
// 	    LogService.logMessage("Example: Cannot apply weight to non-numeric attribute! Using original value.", 
// 				  LogService.WARNING);
	    return getUnweightedValue(a);
	} else {
	    return weightApplier.applyWeight(getUnweightedValue(a), Double.isNaN(weight) ? 1.0d : weight);
	}
    }

    /** Returns the original, unweighted value of the given attribute. */
    public double getUnweightedValue(Attribute a) {
	return data.get(a);
    }

    /** Returns the value of the i-th attribute. */
    public double getValue(int i) {
	return getValue(attributes[i]);
    }

    /** Sets the value of the i-th attribute. */
    public void setValue(int i, double v) {
	if (i < 0) throw new IndexOutOfBoundsException("Illegal attribute index: "+i);
	setValue(attributes[i], v);
    }

    /** Sets the value of attribute a. The attribute a need not necessarily be 
     *  part of the example set the example is taken from, although this is no good style. */
    public void setValue(Attribute a, double value) {
	data.set(a, value);
    }

    /** Sets the value of the i-th attribute which must be a nominal attribute. */
    public void setValue(int i, String str) throws NumberFormatException {
	double v;
	if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attributes[i].getValueType(), Ontology.NOMINAL)) {   
	    v = attributes[i].mapString(str);  
	} else {   
	    v = Double.parseDouble(str);  
	}
	setValue(attributes[i], v);
    }
    
    /** Sets the value of attribute a which must be a nominal attribute. The attribute a need 
     *  not necessarily be part of the example set the example is taken from, although this is no good style. */
    public void setValue(Attribute a, String str) {
	if (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(a.getValueType(), Ontology.NOMINAL))
	    throw new IllegalArgumentException("setValue(Attribute, String) only supported for nominal values!");
	if (str != null)
	    setValue(a, a.mapString(str));
	else
	    setValue(a, Double.NaN);
    }

    public String getValueAsString(int i) {
	return getAsString(attributes[i], getValue(i));
    }

    public String getValueAsString(Attribute a) {
	return getAsString(a, getValue(a));
    }

    public double getLabel() {
	if (label == null) return Double.NaN;
	return getValue(label);
    }

    public void setLabel(double value) {
	setValue(label, value);
    }

    public void setLabel(String value) {
	setValue(label, value);
    }

    public String getLabelAsString() {
	if (label == null) return "n.a.";
	return getAsString(label, getLabel());
    }

    public double getPredictedLabel() {
	if (predictedLabel == null) return Double.NaN;
	else return getValue(predictedLabel);
    }

    public String getPredictedLabelAsString() {
	if (predictedLabel == null) return "n.a.";
	return getAsString(predictedLabel, getPredictedLabel());
    }

    public void setPredictedLabel(double value) {
	setValue(predictedLabel, value);
    }

    public void setPredictedLabel(String value) {
	setValue(predictedLabel, value);
    }

    public double getWeight() {
	if (weight == null) return 1.0;
	else return getValue(weight);
    }

    public void setWeight(double weight) {
	if (this.weight == null) throw new UnsupportedOperationException("Method 'setWeight' not supported: Weight attribute is not available");
	else setValue(this.weight, weight);
    }

    public int getCluster() {
	if (cluster == null) return -1;
	else return (int)getValue(cluster);
    }

    public int getId() {
	if (idAttribute == null) return -1;
	else return (int)getValue(idAttribute);
    }

    public String getIdAsString() {
	if (idAttribute == null) return "n.a.";
	return getAsString(idAttribute, getId());
    }


    public void setCluster(int cluster) {
	if (this.cluster == null) throw new UnsupportedOperationException("Method 'setCluster' not supported: Cluster attribute is not available");
	else setValue(this.cluster, "cluster"+cluster);
    }

    public String getAsString(Attribute attribute, double value) {
	return attribute.getAsString(value);
    }

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

    // --------------------------------------------------------------------------------

    /** Can be used for file output. */
    public String getAttributesAsString() {
	return getAttributesAsString(SEPARATOR);
    }

    /** Can be used for file output. */
    public String getAttributesAsString(String sep) {
	StringBuffer str = new StringBuffer();
	for (int i = 0; i < getNumberOfAttributes(); i++) {
	    if (i > 0) str.append(sep);
	    str.append(getValueAsString(i));
	}
	return str.toString();
    }

    /** Calls {@link Example#getAttributesAsSparseString(String,String)} using
     *  default separator characters. */
    public String getAttributesAsSparseString() {
	return getAttributesAsSparseString(SEPARATOR, SPARSE_SEPARATOR);
    }

    /** Returns the attribute values in the format <br>
     *  <center>index:value index:value</center><br>
     *  Index starts with 1. 
     *  @param separator separates attributes
     *  @param indexValueSeparator separates index and value. */
    public String getAttributesAsSparseString(String separator, String indexValueSeparator) {
	StringBuffer str = new StringBuffer();
	boolean first = true;
	for (int i = 0; i < getNumberOfAttributes(); i++) {
	    Attribute attr = getAttribute(i);
	    double value = getValue(i);
	    if (!attr.isDefault(value)) {
		if (!first) {
		    str.append(separator);
		}
		first = false;
		str.append((i+1)+indexValueSeparator+getAsString(attr, value));
	    }
//  	    if (attr.isNominal()) {
//  		if (value != Attribute.FIRST_CLASS_INDEX) {
//  		    str.append((i+1)+":"+attr.mapIndex((int)value)+" ");		    
//  		}
//  	    } else {
//  		if (value != 0d) {
//  		    str.append((i+1)+":"+value+" ");		    
//  		}
//  	    }
	}
	return str.toString();
    }

    // --------------------------------------------------------------------------------

    /** Returns all data in the following ordering:
     *  <ol>
     *    <li>id</id>
     *    <li>regular attributes</id>
     *    <li>label</id>
     *    <li>predicted label</id>
     *    <li>weight</id>
     *    <li>cluster</id>
     *  </ol> */ 
    public String toString() {
	return 
	    (idAttribute != null ? getIdAsString() + SEPARATOR : "") +
	    getAttributesAsString() + SEPARATOR +
	    (label != null ? getLabelAsString() + SEPARATOR : "") +
	    (predictedLabel != null ? getPredictedLabelAsString() + SEPARATOR : "") +
	    (weight != null ? getWeight() + SEPARATOR : "") +
	    (cluster != null ? getCluster() + SEPARATOR : "");
    }
    
    /** Returns regular and special attributes in sparse format.
     *  @param format one of the formats specified in {@link SparseFormatDataRowReader} */
    public String toSparseString(int format) {
	StringBuffer str = new StringBuffer();
	if ((format == SparseFormatDataRowReader.FORMAT_YX) && (label != null)) {
	    str.append(getLabelAsString()+" ");
	}
	if (idAttribute != null) { str.append("id:"+getId()+" "); }
	if (weight != null) { str.append(" w:"+getWeight()+" "); }

	str.append(getAttributesAsSparseString());

	if ((format == SparseFormatDataRowReader.FORMAT_PREFIX) && (label != null)) {
	    str.append(" l:"+getLabelAsString());
	}
	if ((format == SparseFormatDataRowReader.FORMAT_XY) && (label != null)) {
	    str.append(" " + getLabelAsString());
	}
	return str.toString();
    }
}



⌨️ 快捷键说明

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