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

📄 instance.java

📁 Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *    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., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *    Instance.java *    Copyright (C) 1999 Eibe Frank * */package weka.core;import java.io.Serializable;import java.util.Enumeration;/** * Class for handling an instance. All values (numeric, date, nominal, string * or relational) are internally stored as floating-point numbers. If an * attribute is nominal (or a string or relational), the stored value is the * index of the corresponding nominal (or string or relational) value in the * attribute's definition. We have chosen this approach in favor of a more * elegant object-oriented approach because it is much faster. <p> * * Typical usage (code from the main() method of this class): <p> * * <code> * ... <br> *       * // Create empty instance with three attribute values <br> * Instance inst = new Instance(3); <br><br> *      * // Set instance's values for the attributes "length", "weight", and "position"<br> * inst.setValue(length, 5.3); <br> * inst.setValue(weight, 300); <br> * inst.setValue(position, "first"); <br><br> *    * // Set instance's dataset to be the dataset "race" <br> * inst.setDataset(race); <br><br> *    * // Print the instance <br> * System.out.println("The instance: " + inst); <br> * * ... <br> * </code><p> * * All methods that change an instance are safe, ie. a change of an * instance does not affect any other instances. All methods that * change an instance's attribute values clone the attribute value * vector before it is changed. If your application heavily modifies * instance values, it may be faster to create a new instance from scratch. * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.23 $  */public class Instance  implements Copyable, Serializable {    /** for serialization */  static final long serialVersionUID = 1482635194499365122L;    /** Constant representing a missing value. */  protected static final double MISSING_VALUE = Double.NaN;  /**    * The dataset the instance has access to.  Null if the instance   * doesn't have access to any dataset.  Only if an instance has   * access to a dataset, it knows about the actual attribute types.     */  protected /*@spec_public@*/ Instances m_Dataset;  /** The instance's attribute values. */  protected /*@spec_public non_null@*/ double[] m_AttValues;  /** The instance's weight. */  protected double m_Weight;  /**   * Constructor that copies the attribute values and the weight from   * the given instance. Reference to the dataset is set to null.   * (ie. the instance doesn't have access to information about the   * attribute types)   *   * @param instance the instance from which the attribute   * values and the weight are to be copied    */  //@ ensures m_Dataset == null;  protected Instance(/*@non_null@*/ Instance instance) {        m_AttValues = instance.m_AttValues;    m_Weight = instance.m_Weight;    m_Dataset = null;  }  /**   * Constructor that inititalizes instance variable with given   * values. Reference to the dataset is set to null. (ie. the instance   * doesn't have access to information about the attribute types)   *   * @param weight the instance's weight   * @param attValues a vector of attribute values    */  //@ ensures m_Dataset == null;  public Instance(double weight,  /*@non_null@*/ double[]attValues){        m_AttValues = attValues;    m_Weight = weight;    m_Dataset = null;  }  /**   * Constructor of an instance that sets weight to one, all values to   * be missing, and the reference to the dataset to null. (ie. the instance   * doesn't have access to information about the attribute types)   *   * @param numAttributes the size of the instance    */  //@ requires numAttributes > 0;    // Or maybe == 0 is okay too?  //@ ensures m_Dataset == null;  public Instance(int numAttributes) {        m_AttValues = new double[numAttributes];    for (int i = 0; i < m_AttValues.length; i++) {      m_AttValues[i] = MISSING_VALUE;    }    m_Weight = 1;    m_Dataset = null;  }  /**   * Returns the attribute with the given index.   *   * @param index the attribute's index   * @return the attribute at the given position   * @throws UnassignedDatasetException if instance doesn't have access to a   * dataset   */   //@ requires m_Dataset != null;  public /*@pure@*/ Attribute attribute(int index) {       if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.attribute(index);  }  /**   * Returns the attribute with the given index. Does the same   * thing as attribute().   *   * @param indexOfIndex the index of the attribute's index    * @return the attribute at the given position   * @throws UnassignedDatasetException if instance doesn't have access to a   * dataset   */   //@ requires m_Dataset != null;  public /*@pure@*/ Attribute attributeSparse(int indexOfIndex) {       if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.attribute(indexOfIndex);  }  /**   * Returns class attribute.   *   * @return the class attribute   * @throws UnassignedDatasetException if the class is not set or the   * instance doesn't have access to a dataset   */  //@ requires m_Dataset != null;  public /*@pure@*/ Attribute classAttribute() {    if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.classAttribute();  }  /**   * Returns the class attribute's index.   *   * @return the class index as an integer    * @throws UnassignedDatasetException if instance doesn't have access to a dataset    */  //@ requires m_Dataset != null;  //@ ensures  \result == m_Dataset.classIndex();  public /*@pure@*/ int classIndex() {        if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.classIndex();  }  /**   * Tests if an instance's class is missing.   *   * @return true if the instance's class is missing   * @throws UnassignedClassException if the class is not set or the instance doesn't   * have access to a dataset   */  //@ requires classIndex() >= 0;  public /*@pure@*/ boolean classIsMissing() {    if (classIndex() < 0) {      throw new UnassignedClassException("Class is not set!");    }    return isMissing(classIndex());  }  /**   * Returns an instance's class value in internal format. (ie. as a   * floating-point number)   *   * @return the corresponding value as a double (If the    * corresponding attribute is nominal (or a string) then it returns the    * value's index as a double).   * @throws UnassignedClassException if the class is not set or the instance doesn't   * have access to a dataset    */  //@ requires classIndex() >= 0;  public /*@pure@*/ double classValue() {        if (classIndex() < 0) {      throw new UnassignedClassException("Class is not set!");    }    return value(classIndex());  }  /**   * Produces a shallow copy of this instance. The copy has   * access to the same dataset. (if you want to make a copy   * that doesn't have access to the dataset, use    * <code>new Instance(instance)</code>   *   * @return the shallow copy   */  //@ also ensures \result != null;  //@ also ensures \result instanceof Instance;  //@ also ensures ((Instance)\result).m_Dataset == m_Dataset;  public /*@pure@*/ Object copy() {    Instance result = new Instance(this);    result.m_Dataset = m_Dataset;    return result;  }  /**   * Returns the dataset this instance has access to. (ie. obtains   * information about attribute types from) Null if the instance   * doesn't have access to a dataset.   *   * @return the dataset the instance has accesss to   */  //@ ensures \result == m_Dataset;  public /*@pure@*/ Instances dataset() {    return m_Dataset;  }  /**   * Deletes an attribute at the given position (0 to    * numAttributes() - 1). Only succeeds if the instance does not   * have access to any dataset because otherwise inconsistencies   * could be introduced.   *   * @param position the attribute's position   * @throws RuntimeException if the instance has access to a   * dataset    */  //@ requires m_Dataset != null;  public void deleteAttributeAt(int position) {    if (m_Dataset != null) {      throw new RuntimeException("Instance has access to a dataset!");    }    forceDeleteAttributeAt(position);  }  /**   * Returns an enumeration of all the attributes.   *   * @return enumeration of all the attributes   * @throws UnassignedDatasetException if the instance doesn't   * have access to a dataset    */  //@ requires m_Dataset != null;  public /*@pure@*/ Enumeration enumerateAttributes() {    if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.enumerateAttributes();  }  /**   * Tests if the headers of two instances are equivalent.   *   * @param inst another instance   * @return true if the header of the given instance is    * equivalent to this instance's header   * @throws UnassignedDatasetException if instance doesn't have access to any   * dataset   */  //@ requires m_Dataset != null;  public /*@pure@*/ boolean equalHeaders(Instance inst) {    if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    return m_Dataset.equalHeaders(inst.m_Dataset);  }  /**   * Tests whether an instance has a missing value. Skips the class attribute if set.   * @return true if instance has a missing value.   * @throws UnassignedDatasetException if instance doesn't have access to any   * dataset   */  //@ requires m_Dataset != null;  public /*@pure@*/ boolean hasMissingValue() {        if (m_Dataset == null) {      throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");    }    for (int i = 0; i < numAttributes(); i++) {      if (i != classIndex()) {	if (isMissing(i)) {	  return true;	}      }    }    return false;  }  /**   * Returns the index of the attribute stored at the given position.   * Just returns the given value.   *   * @param position the position    * @return the index of the attribute stored at the given position   */  public /*@pure@*/ int index(int position) {    return position;  }  /**   * Inserts an attribute at the given position (0 to    * numAttributes()). Only succeeds if the instance does not   * have access to any dataset because otherwise inconsistencies   * could be introduced.   *   * @param position the attribute's position   * @throws RuntimeException if the instance has accesss to a   * dataset   * @throws IllegalArgumentException if the position is out of range   */  //@ requires m_Dataset == null;  //@ requires 0 <= position && position <= numAttributes();  public void insertAttributeAt(int position) {    if (m_Dataset != null) {      throw new RuntimeException("Instance has accesss to a dataset!");    }    if ((position < 0) ||	(position > numAttributes())) {      throw new IllegalArgumentException("Can't insert attribute: index out "+                                         "of range");    }    forceInsertAttributeAt(position);  }  /**   * Tests if a specific value is "missing".   *   * @param attIndex the attribute's index   * @return true if the value is "missing"   */  public /*@pure@*/ boolean isMissing(int attIndex) {

⌨️ 快捷键说明

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