📄 example.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.example;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.MethodNotSupportedException;/** 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.13 2003/07/24 09:52:52 fischer 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[] attribute; /** 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; /** Creates a new Example that uses the Data stored in a DataRow. */ public Example(DataRow data, Attribute[] attribute, Attribute label, Attribute predictedLabel, Attribute weight, Attribute cluster, Attribute idAttribute) { this.data = data; this.attribute = attribute; this.label = label; this.predictedLabel = predictedLabel; this.weight = weight; this.cluster = cluster; this.idAttribute = idAttribute; } /** Returns the i-th attribue. */ public Attribute getAttribute(int i) { return attribute[i]; } public Attribute getLabelAttribute() { return label; } public Attribute getIdAttribute() { return idAttribute; } 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) { return data.get(a); } /** Returns the value of the i-th attribute. */ public double getValue(int i) { return data.get(attribute[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); data.set(attribute[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(attribute[i].getValueType(), Ontology.NOMINAL)) { v = attribute[i].mapString(str); } else { v = Double.parseDouble(str); } data.set(attribute[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) data.set(a, a.mapString(str)); else data.set(a, Double.NaN); } public String getValueAsString(int i) { return getAsString(attribute[i], getValue(i)); } public String getValueAsString(Attribute a) { return getAsString(a, getValue(a)); } public double getLabel() { if (label == null) return Double.NaN; return data.get(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 data.get(predictedLabel); } public String getPredictedLabelAsString() { if (predictedLabel == null) return "n.a."; return getAsString(predictedLabel, getPredictedLabel()); } public void setPredictedLabel(double value) { data.set(predictedLabel, value); } public void setPredictedLabel(String value) { setValue(predictedLabel, value); } public double getWeight() { if (weight == null) return 1.0; else return data.get(weight); } public void setWeight(double weight) throws MethodNotSupportedException { if (this.weight == null) throw new MethodNotSupportedException("Method 'setWeight' not supported: Weight attribute is not available"); else data.set(this.weight, weight); } public int getCluster() { if (cluster == null) return -1; else return (int)data.get(cluster); } public int getId() { if (idAttribute == null) return -1; else return (int)data.get(idAttribute); } public String getIdAsString() { if (idAttribute == null) return "n.a."; return getAsString(idAttribute, getId()); } public void setCluster(int cluster) throws MethodNotSupportedException { if (this.cluster == null) throw new MethodNotSupportedException("Method 'setCluster' not supported: Cluster attribute is not available"); else setValue(this.cluster, "cluster"+cluster); } private String getAsString(Attribute attribute, double value) { return attribute.getAsString(value); } public int getNumberOfAttributes() { return attribute.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 + -