程序包 weka.core

类 Instance

java.lang.Object
weka.core.Instance
所有已实现的接口:
Serializable, Copyable, RevisionHandler
直接已知子类:
IndividualInstance, SparseInstance

public class Instance extends Object implements Copyable, Serializable, RevisionHandler
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.

Typical usage (code from the main() method of this class):

...
// Create empty instance with three attribute values
Instance inst = new Instance(3);

// Set instance's values for the attributes "length", "weight", and "position"
inst.setValue(length, 5.3);
inst.setValue(weight, 300);
inst.setValue(position, "first");

// Set instance's dataset to be the dataset "race"
inst.setDataset(race);

// Print the instance
System.out.println("The instance: " + inst);
...

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.

版本:
$Revision: 9140 $
作者:
Eibe Frank (eibe@cs.waikato.ac.nz)
另请参阅:
  • 构造器概要

    构造器
    构造器
    说明
    Instance(double weight, double[] attValues)
    Constructor that inititalizes instance variable with given values.
    Instance(int numAttributes)
    Constructor of an instance that sets weight to one, all values to be missing, and the reference to the dataset to null.
    Instance(Instance instance)
    Constructor that copies the attribute values and the weight from the given instance.
  • 方法概要

    修饰符和类型
    方法
    说明
    attribute(int index)
    Returns the attribute with the given index.
    attributeSparse(int indexOfIndex)
    Returns the attribute with the given index.
    Returns class attribute.
    int
    Returns the class attribute's index.
    boolean
    Tests if an instance's class is missing.
    double
    Returns an instance's class value in internal format.
    Produces a shallow copy of this instance.
    Returns the dataset this instance has access to.
    void
    deleteAttributeAt(int position)
    Deletes an attribute at the given position (0 to numAttributes() - 1).
    Returns an enumeration of all the attributes.
    boolean
    Tests if the headers of two instances are equivalent.
    Returns the revision string.
    boolean
    Tests whether an instance has a missing value.
    int
    index(int position)
    Returns the index of the attribute stored at the given position.
    void
    insertAttributeAt(int position)
    Inserts an attribute at the given position (0 to numAttributes()).
    boolean
    isMissing(int attIndex)
    Tests if a specific value is "missing".
    boolean
    Tests if a specific value is "missing".
    boolean
    isMissingSparse(int indexOfIndex)
    Tests if a specific value is "missing".
    static boolean
    isMissingValue(double val)
    Tests if the given value codes "missing".
    static void
    main(String[] options)
    Main method for testing this class.
    Merges this instance with the given instance and returns the result.
    static double
    Returns the double that codes "missing".
    int
    Returns the number of attributes.
    int
    Returns the number of class labels.
    int
    Returns the number of values present.
    final Instances
    relationalValue(int attIndex)
    Returns the relational value of a relational attribute.
    final Instances
    Returns the relational value of a relational attribute.
    void
    replaceMissingValues(double[] array)
    Replaces all missing values in the instance with the values contained in the given array.
    void
    Sets the class value of an instance to be "missing".
    void
    setClassValue(double value)
    Sets the class value of an instance to the given value (internal floating-point format).
    final void
    Sets the class value of an instance to the given value.
    final void
    setDataset(Instances instances)
    Sets the reference to the dataset.
    final void
    setMissing(int attIndex)
    Sets a specific value to be "missing".
    final void
    Sets a specific value to be "missing".
    void
    setValue(int attIndex, double value)
    Sets a specific value in the instance to the given value (internal floating-point format).
    final void
    setValue(int attIndex, String value)
    Sets a value of a nominal or string attribute to the given value.
    final void
    setValue(Attribute att, double value)
    Sets a specific value in the instance to the given value (internal floating-point format).
    final void
    setValue(Attribute att, String value)
    Sets a value of an nominal or string attribute to the given value.
    void
    setValueSparse(int indexOfIndex, double value)
    Sets a specific value in the instance to the given value (internal floating-point format).
    final void
    setWeight(double weight)
    Sets the weight of an instance.
    final String
    stringValue(int attIndex)
    Returns the value of a nominal, string, date, or relational attribute for the instance as a string.
    final String
    Returns the value of a nominal, string, date, or relational attribute for the instance as a string.
    double[]
    Returns the values of each attribute as an array of doubles.
    Returns the description of one instance.
    final String
    toString(int attIndex)
    Returns the description of one value of the instance as a string.
    final String
    Returns the description of one value of the instance as a string.
    double
    value(int attIndex)
    Returns an instance's attribute value in internal format.
    double
    Returns an instance's attribute value in internal format.
    double
    valueSparse(int indexOfIndex)
    Returns an instance's attribute value in internal format.
    final double
    Returns the instance's weight.

    从类继承的方法 java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 构造器详细资料

    • Instance

      public Instance(Instance instance)
      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)
      参数:
      instance - the instance from which the attribute values and the weight are to be copied
    • Instance

      public Instance(double weight, double[] attValues)
      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)
      参数:
      weight - the instance's weight
      attValues - a vector of attribute values
    • Instance

      public Instance(int numAttributes)
      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)
      参数:
      numAttributes - the size of the instance
  • 方法详细资料

    • attribute

      public Attribute attribute(int index)
      Returns the attribute with the given index.
      参数:
      index - the attribute's index
      返回:
      the attribute at the given position
      抛出:
      UnassignedDatasetException - if instance doesn't have access to a dataset
    • attributeSparse

      public Attribute attributeSparse(int indexOfIndex)
      Returns the attribute with the given index. Does the same thing as attribute().
      参数:
      indexOfIndex - the index of the attribute's index
      返回:
      the attribute at the given position
      抛出:
      UnassignedDatasetException - if instance doesn't have access to a dataset
    • classAttribute

      public Attribute classAttribute()
      Returns class attribute.
      返回:
      the class attribute
      抛出:
      UnassignedDatasetException - if the class is not set or the instance doesn't have access to a dataset
    • classIndex

      public int classIndex()
      Returns the class attribute's index.
      返回:
      the class index as an integer
      抛出:
      UnassignedDatasetException - if instance doesn't have access to a dataset
    • classIsMissing

      public boolean classIsMissing()
      Tests if an instance's class is missing.
      返回:
      true if the instance's class is missing
      抛出:
      UnassignedClassException - if the class is not set or the instance doesn't have access to a dataset
    • classValue

      public double classValue()
      Returns an instance's class value in internal format. (ie. as a floating-point number)
      返回:
      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).
      抛出:
      UnassignedClassException - if the class is not set or the instance doesn't have access to a dataset
    • copy

      public Object copy()
      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 new Instance(instance)
      指定者:
      copy 在接口中 Copyable
      返回:
      the shallow copy
    • dataset

      public Instances dataset()
      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.
      返回:
      the dataset the instance has accesss to
    • deleteAttributeAt

      public void deleteAttributeAt(int position)
      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.
      参数:
      position - the attribute's position
      抛出:
      RuntimeException - if the instance has access to a dataset
    • enumerateAttributes

      public Enumeration enumerateAttributes()
      Returns an enumeration of all the attributes.
      返回:
      enumeration of all the attributes
      抛出:
      UnassignedDatasetException - if the instance doesn't have access to a dataset
    • equalHeaders

      public boolean equalHeaders(Instance inst)
      Tests if the headers of two instances are equivalent.
      参数:
      inst - another instance
      返回:
      true if the header of the given instance is equivalent to this instance's header
      抛出:
      UnassignedDatasetException - if instance doesn't have access to any dataset
    • hasMissingValue

      public boolean hasMissingValue()
      Tests whether an instance has a missing value. Skips the class attribute if set.
      返回:
      true if instance has a missing value.
      抛出:
      UnassignedDatasetException - if instance doesn't have access to any dataset
    • index

      public int index(int position)
      Returns the index of the attribute stored at the given position. Just returns the given value.
      参数:
      position - the position
      返回:
      the index of the attribute stored at the given position
    • insertAttributeAt

      public void insertAttributeAt(int 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.
      参数:
      position - the attribute's position
      抛出:
      RuntimeException - if the instance has accesss to a dataset
      IllegalArgumentException - if the position is out of range
    • isMissing

      public boolean isMissing(int attIndex)
      Tests if a specific value is "missing".
      参数:
      attIndex - the attribute's index
      返回:
      true if the value is "missing"
    • isMissingSparse

      public boolean isMissingSparse(int indexOfIndex)
      Tests if a specific value is "missing". Does the same thing as isMissing() if applied to an Instance.
      参数:
      indexOfIndex - the index of the attribute's index
      返回:
      true if the value is "missing"
    • isMissing

      public boolean isMissing(Attribute att)
      Tests if a specific value is "missing". The given attribute has to belong to a dataset.
      参数:
      att - the attribute
      返回:
      true if the value is "missing"
    • isMissingValue

      public static boolean isMissingValue(double val)
      Tests if the given value codes "missing".
      参数:
      val - the value to be tested
      返回:
      true if val codes "missing"
    • mergeInstance

      public Instance mergeInstance(Instance inst)
      Merges this instance with the given instance and returns the result. Dataset is set to null.
      参数:
      inst - the instance to be merged with this one
      返回:
      the merged instances
    • missingValue

      public static double missingValue()
      Returns the double that codes "missing".
      返回:
      the double that codes "missing"
    • numAttributes

      public int numAttributes()
      Returns the number of attributes.
      返回:
      the number of attributes as an integer
    • numClasses

      public int numClasses()
      Returns the number of class labels.
      返回:
      the number of class labels as an integer if the class attribute is nominal, 1 otherwise.
      抛出:
      UnassignedDatasetException - if instance doesn't have access to any dataset
    • numValues

      public int numValues()
      Returns the number of values present. Always the same as numAttributes().
      返回:
      the number of values
    • replaceMissingValues

      public void replaceMissingValues(double[] array)
      Replaces all missing values in the instance with the values contained in the given array. A deep copy of the vector of attribute values is performed before the values are replaced.
      参数:
      array - containing the means and modes
      抛出:
      IllegalArgumentException - if numbers of attributes are unequal
    • setClassMissing

      public void setClassMissing()
      Sets the class value of an instance to be "missing". A deep copy of the vector of attribute values is performed before the value is set to be missing.
      抛出:
      UnassignedClassException - if the class is not set
      UnassignedDatasetException - if the instance doesn't have access to a dataset
    • setClassValue

      public void setClassValue(double value)
      Sets the class value of an instance to the given value (internal floating-point format). A deep copy of the vector of attribute values is performed before the value is set.
      参数:
      value - the new attribute value (If the corresponding attribute is nominal (or a string) then this is the new value's index as a double).
      抛出:
      UnassignedClassException - if the class is not set
      UnaddignedDatasetException - if the instance doesn't have access to a dataset
    • setClassValue

      public final void setClassValue(String value)
      Sets the class value of an instance to the given value. A deep copy of the vector of attribute values is performed before the value is set.
      参数:
      value - the new class value (If the class is a string attribute and the value can't be found, the value is added to the attribute).
      抛出:
      UnassignedClassException - if the class is not set
      UnassignedDatasetException - if the dataset is not set
      IllegalArgumentException - if the attribute is not nominal or a string, or the value couldn't be found for a nominal attribute
    • setDataset

      public final void setDataset(Instances instances)
      Sets the reference to the dataset. Does not check if the instance is compatible with the dataset. Note: the dataset does not know about this instance. If the structure of the dataset's header gets changed, this instance will not be adjusted automatically.
      参数:
      instances - the reference to the dataset
    • setMissing

      public final void setMissing(int attIndex)
      Sets a specific value to be "missing". Performs a deep copy of the vector of attribute values before the value is set to be missing.
      参数:
      attIndex - the attribute's index
    • setMissing

      public final void setMissing(Attribute att)
      Sets a specific value to be "missing". Performs a deep copy of the vector of attribute values before the value is set to be missing. The given attribute has to belong to a dataset.
      参数:
      att - the attribute
    • setValue

      public void setValue(int attIndex, double value)
      Sets a specific value in the instance to the given value (internal floating-point format). Performs a deep copy of the vector of attribute values before the value is set.
      参数:
      attIndex - the attribute's index
      value - the new attribute value (If the corresponding attribute is nominal (or a string) then this is the new value's index as a double).
    • setValueSparse

      public void setValueSparse(int indexOfIndex, double value)
      Sets a specific value in the instance to the given value (internal floating-point format). Performs a deep copy of the vector of attribute values before the value is set. Does exactly the same thing as setValue().
      参数:
      indexOfIndex - the index of the attribute's index
      value - the new attribute value (If the corresponding attribute is nominal (or a string) then this is the new value's index as a double).
    • setValue

      public final void setValue(int attIndex, String value)
      Sets a value of a nominal or string attribute to the given value. Performs a deep copy of the vector of attribute values before the value is set.
      参数:
      attIndex - the attribute's index
      value - the new attribute value (If the attribute is a string attribute and the value can't be found, the value is added to the attribute).
      抛出:
      UnassignedDatasetException - if the dataset is not set
      IllegalArgumentException - if the selected attribute is not nominal or a string, or the supplied value couldn't be found for a nominal attribute
    • setValue

      public final void setValue(Attribute att, double value)
      Sets a specific value in the instance to the given value (internal floating-point format). Performs a deep copy of the vector of attribute values before the value is set, so if you are planning on calling setValue many times it may be faster to create a new instance using toDoubleArray. The given attribute has to belong to a dataset.
      参数:
      att - the attribute
      value - the new attribute value (If the corresponding attribute is nominal (or a string) then this is the new value's index as a double).
    • setValue

      public final void setValue(Attribute att, String value)
      Sets a value of an nominal or string attribute to the given value. Performs a deep copy of the vector of attribute values before the value is set, so if you are planning on calling setValue many times it may be faster to create a new instance using toDoubleArray. The given attribute has to belong to a dataset.
      参数:
      att - the attribute
      value - the new attribute value (If the attribute is a string attribute and the value can't be found, the value is added to the attribute).
      抛出:
      IllegalArgumentException - if the the attribute is not nominal or a string, or the value couldn't be found for a nominal attribute
    • setWeight

      public final void setWeight(double weight)
      Sets the weight of an instance.
      参数:
      weight - the weight
    • relationalValue

      public final Instances relationalValue(int attIndex)
      Returns the relational value of a relational attribute.
      参数:
      attIndex - the attribute's index
      返回:
      the corresponding relation as an Instances object
      抛出:
      IllegalArgumentException - if the attribute is not a relation-valued attribute
      UnassignedDatasetException - if the instance doesn't belong to a dataset.
    • relationalValue

      public final Instances relationalValue(Attribute att)
      Returns the relational value of a relational attribute.
      参数:
      att - the attribute
      返回:
      the corresponding relation as an Instances object
      抛出:
      IllegalArgumentException - if the attribute is not a relation-valued attribute
      UnassignedDatasetException - if the instance doesn't belong to a dataset.
    • stringValue

      public final String stringValue(int attIndex)
      Returns the value of a nominal, string, date, or relational attribute for the instance as a string.
      参数:
      attIndex - the attribute's index
      返回:
      the value as a string
      抛出:
      IllegalArgumentException - if the attribute is not a nominal, string, date, or relation-valued attribute.
      UnassignedDatasetException - if the instance doesn't belong to a dataset.
    • stringValue

      public final String stringValue(Attribute att)
      Returns the value of a nominal, string, date, or relational attribute for the instance as a string.
      参数:
      att - the attribute
      返回:
      the value as a string
      抛出:
      IllegalArgumentException - if the attribute is not a nominal, string, date, or relation-valued attribute.
      UnassignedDatasetException - if the instance doesn't belong to a dataset.
    • toDoubleArray

      public double[] toDoubleArray()
      Returns the values of each attribute as an array of doubles.
      返回:
      an array containing all the instance attribute values
    • toString

      public String toString()
      Returns the description of one instance. If the instance doesn't have access to a dataset, it returns the internal floating-point values. Quotes string values that contain whitespace characters.
      覆盖:
      toString 在类中 Object
      返回:
      the instance's description as a string
    • toString

      public final String toString(int attIndex)
      Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset, it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark.
      参数:
      attIndex - the attribute's index
      返回:
      the value's description as a string
    • toString

      public final String toString(Attribute att)
      Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark. The given attribute has to belong to a dataset.
      参数:
      att - the attribute
      返回:
      the value's description as a string
    • value

      public double value(int attIndex)
      Returns an instance's attribute value in internal format.
      参数:
      attIndex - the attribute's index
      返回:
      the specified value as a double (If the corresponding attribute is nominal (or a string) then it returns the value's index as a double).
    • valueSparse

      public double valueSparse(int indexOfIndex)
      Returns an instance's attribute value in internal format. Does exactly the same thing as value() if applied to an Instance.
      参数:
      indexOfIndex - the index of the attribute's index
      返回:
      the specified value as a double (If the corresponding attribute is nominal (or a string) then it returns the value's index as a double).
    • value

      public double value(Attribute att)
      Returns an instance's attribute value in internal format. The given attribute has to belong to a dataset.
      参数:
      att - the attribute
      返回:
      the specified value as a double (If the corresponding attribute is nominal (or a string) then it returns the value's index as a double).
    • weight

      public final double weight()
      Returns the instance's weight.
      返回:
      the instance's weight as a double
    • main

      public static void main(String[] options)
      Main method for testing this class.
      参数:
      options - the commandline options - ignored
    • getRevision

      public String getRevision()
      Returns the revision string.
      指定者:
      getRevision 在接口中 RevisionHandler
      返回:
      the revision