Package weka.core

Class DenseInstance

All Implemented Interfaces:
Serializable, Copyable, Instance, RevisionHandler

public class DenseInstance extends AbstractInstance
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 DenseInstance(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's attribute values are safe, ie. a change of an instance's attribute values 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.

Version:
$Revision: 15069 $
Author:
Eibe Frank (eibe@cs.waikato.ac.nz)
See Also:
  • Constructor Details

    • DenseInstance

      public DenseInstance(Instance instance)
      Constructor that copies the attribute values and the weight from the given instance. It does NOT perform a deep copy of the attribute values if the instance provided is also of type DenseInstance (it simply copies the reference to the array of values), otherwise it does. Reference to the dataset is set to null. (ie. the instance doesn't have access to information about the attribute types)
      Parameters:
      instance - the instance from which the attribute values and the weight are to be copied
    • DenseInstance

      public DenseInstance(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)
      Parameters:
      weight - the instance's weight
      attValues - a vector of attribute values
    • DenseInstance

      public DenseInstance(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)
      Parameters:
      numAttributes - the size of the instance
  • Method Details

    • 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 DenseInstance(instance)
      Returns:
      the shallow copy
    • copy

      public Instance copy(double[] values)
      Copies the instance but fills up its values based on the given array of doubles. The copy has access to the same dataset.
      Parameters:
      values - the array with new values
      Returns:
      the new instance
    • index

      public int index(int position)
      Returns the index of the attribute stored at the given position. Just returns the given value.
      Parameters:
      position - the position
      Returns:
      the index of the attribute stored at the given position
    • mergeInstance

      public Instance mergeInstance(Instance inst)
      Merges this instance with the given instance and returns the result. Dataset is set to null. The returned instance is of the same type as this instance.
      Parameters:
      inst - the instance to be merged with this one
      Returns:
      the merged instances
    • numAttributes

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

      public int numValues()
      Returns the number of values present. Always the same as numAttributes().
      Returns:
      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.
      Parameters:
      array - containing the means and modes
      Throws:
      IllegalArgumentException - if numbers of attributes are unequal
    • 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.
      Parameters:
      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().
      Parameters:
      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).
    • toDoubleArray

      public double[] toDoubleArray()
      Returns the values of each attribute as an array of doubles. Creates a fresh array object for this.
      Returns:
      an array containing all the instance attribute values
    • toStringNoWeight

      public String toStringNoWeight()
      Returns the description of one instance (without weight appended). If the instance doesn't have access to a dataset, it returns the internal floating-point values. Quotes string values that contain whitespace characters. This method is used by getRandomNumberGenerator() in Instances.java in order to maintain backwards compatibility with weka 3.4.
      Returns:
      the instance's description as a string
    • toStringNoWeight

      public String toStringNoWeight(int afterDecimalPoint)
      Returns the description of one instance (without weight appended). If the instance doesn't have access to a dataset, it returns the internal floating-point values. Quotes string values that contain whitespace characters. This method is used by getRandomNumberGenerator() in Instances.java in order to maintain backwards compatibility with weka 3.4.
      Parameters:
      afterDecimalPoint - maximum number of digits after the decimal point for numeric values
      Returns:
      the instance's description as a string
    • value

      public double value(int attIndex)
      Returns an instance's attribute value in internal format.
      Parameters:
      attIndex - the attribute's index
      Returns:
      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).
    • main

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

      public String getRevision()
      Returns the revision string.
      Specified by:
      getRevision in interface RevisionHandler
      Overrides:
      getRevision in class AbstractInstance
      Returns:
      the revision