Instance.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 33k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    Instance.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.core;
  22. import java.util.*;
  23. import java.io.*;
  24. /**
  25.  * Class for handling an instance. All values (numeric, nominal, or
  26.  * string) are internally stored as floating-point numbers. If an
  27.  * attribute is nominal (or a string), the stored value is the index
  28.  * of the corresponding nominal (or string) value in the attribute's
  29.  * definition. We have chosen this approach in favor of a more elegant
  30.  * object-oriented approach because it is much faster. <p>
  31.  *
  32.  * Typical usage (code from the main() method of this class): <p>
  33.  *
  34.  * <code>
  35.  * ... <br>
  36.  *      
  37.  * // Create empty instance with three attribute values <br>
  38.  * Instance inst = new Instance(3); <br><br>
  39.  *     
  40.  * // Set instance's values for the attributes "length", "weight", and "position"<br>
  41.  * inst.setValue(length, 5.3); <br>
  42.  * inst.setValue(weight, 300); <br>
  43.  * inst.setValue(position, "first"); <br><br>
  44.  *   
  45.  * // Set instance's dataset to be the dataset "race" <br>
  46.  * inst.setDataset(race); <br><br>
  47.  *   
  48.  * // Print the instance <br>
  49.  * System.out.println("The instance: " + inst); <br>
  50.  *
  51.  * ... <br>
  52.  * </code><p>
  53.  *
  54.  * All methods that change an instance are safe, ie. a change of an
  55.  * instance does not affect any other instances. All methods that
  56.  * change an instance's attribute values clone the attribute value
  57.  * vector before it is changed. If your application heavily modifies
  58.  * instance values, it may be faster to create a new instance from scratch.
  59.  *
  60.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  61.  * @version $Revision: 1.14 $ 
  62.  */
  63. public class Instance implements Copyable, Serializable {
  64.   
  65.   /** Constant representing a missing value. */
  66.   protected final static double MISSING_VALUE = Double.NaN;
  67.   /** 
  68.    * The dataset the instance has access to.  Null if the instance
  69.    * doesn't have access to any dataset.  Only if an instance has
  70.    * access to a dataset, it knows about the actual attribute types.  
  71.    */
  72.   protected Instances m_Dataset;
  73.   /** The instance's attribute values. */
  74.   protected double[] m_AttValues;
  75.   /** The instance's weight. */
  76.   protected double m_Weight;
  77.   /**
  78.    * Constructor that copies the attribute values and the weight from
  79.    * the given instance. Reference to the dataset is set to null.
  80.    * (ie. the instance doesn't have access to information about the
  81.    * attribute types)
  82.    *
  83.    * @param instance the instance from which the attribute
  84.    * values and the weight are to be copied 
  85.    */
  86.   public Instance(Instance instance) {
  87.     
  88.     m_AttValues = instance.m_AttValues;
  89.     m_Weight = instance.m_Weight;
  90.     m_Dataset = null;
  91.   }
  92.   /**
  93.    * Constructor that inititalizes instance variable with given
  94.    * values. Reference to the dataset is set to null. (ie. the instance
  95.    * doesn't have access to information about the attribute types)
  96.    *
  97.    * @param weight the instance's weight
  98.    * @param attValues a vector of attribute values 
  99.    */
  100.   public Instance(double weight, double[] attValues){
  101.     
  102.     m_AttValues = attValues;
  103.     m_Weight = weight;
  104.     m_Dataset = null;
  105.   }
  106.   /**
  107.    * Constructor of an instance that sets weight to one, all values to
  108.    * be missing, and the reference to the dataset to null. (ie. the instance
  109.    * doesn't have access to information about the attribute types)
  110.    *
  111.    * @param numAttributes the size of the instance 
  112.    */
  113.   public Instance(int numAttributes) {
  114.     
  115.     m_AttValues = new double[numAttributes];
  116.     for (int i = 0; i < m_AttValues.length; i++) {
  117.       m_AttValues[i] = MISSING_VALUE;
  118.     }
  119.     m_Weight = 1;
  120.     m_Dataset = null;
  121.   }
  122.   /**
  123.    * Returns the attribute with the given index.
  124.    *
  125.    * @param index the attribute's index
  126.    * @return the attribute at the given position
  127.    * @exception UnassignedDatasetException if instance doesn't have access to a
  128.    * dataset
  129.    */ 
  130.   public Attribute attribute(int index) {
  131.    
  132.     if (m_Dataset == null) {
  133.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  134.     }
  135.     return m_Dataset.attribute(index);
  136.   }
  137.   /**
  138.    * Returns the attribute with the given index. Does the same
  139.    * thing as attribute().
  140.    *
  141.    * @param indexOfIndex the index of the attribute's index 
  142.    * @return the attribute at the given position
  143.    * @exception UnassignedDatasetException if instance doesn't have access to a
  144.    * dataset
  145.    */ 
  146.   public Attribute attributeSparse(int indexOfIndex) {
  147.    
  148.     if (m_Dataset == null) {
  149.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  150.     }
  151.     return m_Dataset.attribute(indexOfIndex);
  152.   }
  153.   /**
  154.    * Returns class attribute.
  155.    *
  156.    * @return the class attribute
  157.    * @exception UnassignedDatasetException if the class is not set or the
  158.    * instance doesn't have access to a dataset
  159.    */
  160.   public Attribute classAttribute() {
  161.     if (m_Dataset == null) {
  162.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  163.     }
  164.     return m_Dataset.classAttribute();
  165.   }
  166.   /**
  167.    * Returns the class attribute's index.
  168.    *
  169.    * @return the class index as an integer 
  170.    * @exception UnassignedDatasetException if instance doesn't have access to a dataset 
  171.    */
  172.   public int classIndex() {
  173.     
  174.     if (m_Dataset == null) {
  175.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  176.     }
  177.     return m_Dataset.classIndex();
  178.   }
  179.   /**
  180.    * Tests if an instance's class is missing.
  181.    *
  182.    * @return true if the instance's class is missing
  183.    * @exception UnassignedClassException if the class is not set or the instance doesn't
  184.    * have access to a dataset
  185.    */
  186.   public boolean classIsMissing() {
  187.     if (classIndex() < 0) {
  188.       throw new UnassignedClassException("Class is not set!");
  189.     }
  190.     return isMissing(classIndex());
  191.   }
  192.   /**
  193.    * Returns an instance's class value in internal format. (ie. as a
  194.    * floating-point number)
  195.    *
  196.    * @return the corresponding value as a double (If the 
  197.    * corresponding attribute is nominal (or a string) then it returns the 
  198.    * value's index as a double).
  199.    * @exception UnassignedClassException if the class is not set or the instance doesn't
  200.    * have access to a dataset 
  201.    */
  202.   public double classValue() {
  203.     
  204.     if (classIndex() < 0) {
  205.       throw new UnassignedClassException("Class is not set!");
  206.     }
  207.     return value(classIndex());
  208.   }
  209.   /**
  210.    * Produces a shallow copy of this instance. The copy has
  211.    * access to the same dataset. (if you want to make a copy
  212.    * that doesn't have access to the dataset, use 
  213.    * <code>new Instance(instance)</code>
  214.    *
  215.    * @return the shallow copy
  216.    */
  217.   public Object copy() {
  218.     Instance result = new Instance(this);
  219.     result.m_Dataset = m_Dataset;
  220.     return result;
  221.   }
  222.   /**
  223.    * Returns the dataset this instance has access to. (ie. obtains
  224.    * information about attribute types from) Null if the instance
  225.    * doesn't have access to a dataset.
  226.    *
  227.    * @return the dataset the instance has accesss to
  228.    */
  229.   public Instances dataset() {
  230.     return m_Dataset;
  231.   }
  232.   /**
  233.    * Deletes an attribute at the given position (0 to 
  234.    * numAttributes() - 1). Only succeeds if the instance does not
  235.    * have access to any dataset because otherwise inconsistencies
  236.    * could be introduced.
  237.    *
  238.    * @param pos the attribute's position
  239.    * @exception RuntimeException if the instance has access to a
  240.    * dataset 
  241.    */
  242.   public void deleteAttributeAt(int position) {
  243.     if (m_Dataset != null) {
  244.       throw new RuntimeException("Instance has access to a dataset!");
  245.     }
  246.     forceDeleteAttributeAt(position);
  247.   }
  248.   /**
  249.    * Returns an enumeration of all the attributes.
  250.    *
  251.    * @return enumeration of all the attributes
  252.    * @exception UnassignedDatasetException if the instance doesn't
  253.    * have access to a dataset 
  254.    */
  255.   public Enumeration enumerateAttributes() {
  256.     if (m_Dataset == null) {
  257.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  258.     }
  259.     return m_Dataset.enumerateAttributes();
  260.   }
  261.   /**
  262.    * Tests if the headers of two instances are equivalent.
  263.    *
  264.    * @param instance another instance
  265.    * @return true if the header of the given instance is 
  266.    * equivalent to this instance's header
  267.    * @exception UnassignedDatasetException if instance doesn't have access to any
  268.    * dataset
  269.    */
  270.   public boolean equalHeaders(Instance inst) {
  271.     if (m_Dataset == null) {
  272.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  273.     }
  274.     return m_Dataset.equalHeaders(inst.m_Dataset);
  275.   }
  276.   /**
  277.    * Returns the index of the attribute stored at the given position.
  278.    * Just returns the given value.
  279.    *
  280.    * @param position the position 
  281.    * @return the index of the attribute stored at the given position
  282.    */
  283.   public int index(int position) {
  284.     return position;
  285.   }
  286.   /**
  287.    * Inserts an attribute at the given position (0 to 
  288.    * numAttributes()). Only succeeds if the instance does not
  289.    * have access to any dataset because otherwise inconsistencies
  290.    * could be introduced.
  291.    *
  292.    * @param pos the attribute's position
  293.    * @exception RuntimeException if the instance has accesss to a
  294.    * dataset
  295.    * @exception IllegalArgumentException if the position is out of range
  296.    */
  297.   public void insertAttributeAt(int position) {
  298.     if (m_Dataset != null) {
  299.       throw new RuntimeException("Instance has accesss to a dataset!");
  300.     }
  301.     if ((position < 0) ||
  302. (position > numAttributes())) {
  303.       throw new IllegalArgumentException("Can't insert attribute: index out "+
  304.                                          "of range");
  305.     }
  306.     forceInsertAttributeAt(position);
  307.   }
  308.   /**
  309.    * Tests if a specific value is "missing".
  310.    *
  311.    * @param attIndex the attribute's index
  312.    */
  313.   public boolean isMissing(int attIndex) {
  314.     if (Double.isNaN(m_AttValues[attIndex])) {
  315.       return true;
  316.     }
  317.     return false;
  318.   }
  319.   /**
  320.    * Tests if a specific value is "missing". Does
  321.    * the same thing as isMissing() if applied to an Instance.
  322.    *
  323.    * @param indexOfIndex the index of the attribute's index 
  324.    */
  325.   public boolean isMissingSparse(int indexOfIndex) {
  326.     if (Double.isNaN(m_AttValues[indexOfIndex])) {
  327.       return true;
  328.     }
  329.     return false;
  330.   }
  331.   /**
  332.    * Tests if a specific value is "missing".
  333.    * The given attribute has to belong to a dataset.
  334.    *
  335.    * @param att the attribute
  336.    */
  337.   public boolean isMissing(Attribute att) {
  338.     return isMissing(att.index());
  339.   }
  340.   /**
  341.    * Tests if the given value codes "missing".
  342.    *
  343.    * @param val the value to be tested
  344.    * @return true if val codes "missing"
  345.    */
  346.   public static boolean isMissingValue(double val) {
  347.     return Double.isNaN(val);
  348.   }
  349.   /**
  350.    * Merges this instance with the given instance and returns
  351.    * the result. Dataset is set to null.
  352.    *
  353.    * @param inst the instance to be merged with this one
  354.    * @return the merged instances
  355.    */
  356.   public Instance mergeInstance(Instance inst) {
  357.     int m = 0;
  358.     double [] newVals = new double[numAttributes() + inst.numAttributes()];
  359.     for (int j = 0; j < numAttributes(); j++, m++) {
  360.       newVals[m] = value(j);
  361.     }
  362.     for (int j = 0; j < inst.numAttributes(); j++, m++) {
  363.       newVals[m] = inst.value(j);
  364.     }
  365.     return new Instance(1.0, newVals);
  366.   }
  367.   /**
  368.    * Returns the double that codes "missing".
  369.    *
  370.    * @return the double that codes "missing"
  371.    */
  372.   public static double missingValue() {
  373.     return MISSING_VALUE;
  374.   }
  375.   /**
  376.    * Returns the number of attributes.
  377.    *
  378.    * @return the number of attributes as an integer
  379.    */
  380.   public int numAttributes() {
  381.     return m_AttValues.length;
  382.   }
  383.   /**
  384.    * Returns the number of class labels.
  385.    *
  386.    * @return the number of class labels as an integer if the 
  387.    * class attribute is nominal, 1 otherwise.
  388.    * @exception UnassignedDatasetException if instance doesn't have access to any
  389.    * dataset
  390.    */
  391.   public int numClasses() {
  392.     
  393.     if (m_Dataset == null) {
  394.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  395.     }
  396.     return m_Dataset.numClasses();
  397.   }
  398.   /**
  399.    * Returns the number of values present. Always the same as numAttributes().
  400.    *
  401.    * @return the number of values
  402.    */
  403.   public int numValues() {
  404.     return m_AttValues.length;
  405.   }
  406.   /** 
  407.    * Replaces all missing values in the instance with the
  408.    * values contained in the given array. A deep copy of
  409.    * the vector of attribute values is performed before the
  410.    * values are replaced.
  411.    *
  412.    * @param array containing the means and modes
  413.    * @exception IllegalArgumentException if numbers of attributes are unequal
  414.    */
  415.   public void replaceMissingValues(double[] array) {
  416.  
  417.     if ((array == null) || 
  418. (array.length != m_AttValues.length)) {
  419.       throw new IllegalArgumentException("Unequal number of attributes!");
  420.     }
  421.     freshAttributeVector();
  422.     for (int i = 0; i < m_AttValues.length; i++) {
  423.       if (isMissing(i)) {
  424. m_AttValues[i] = array[i];
  425.       }
  426.     }
  427.   }
  428.   /**
  429.    * Sets the class value of an instance to be "missing". A deep copy of
  430.    * the vector of attribute values is performed before the
  431.    * value is set to be missing.
  432.    *
  433.    * @exception UnassignedClassException if the class is not set
  434.    * @exception UnassignedDatasetException if the instance doesn't
  435.    * have access to a dataset
  436.    */
  437.   public void setClassMissing() {
  438.     if (classIndex() < 0) {
  439.       throw new UnassignedClassException("Class is not set!");
  440.     }
  441.     setMissing(classIndex());
  442.   }
  443.   /**
  444.    * Sets the class value of an instance to the given value (internal
  445.    * floating-point format).  A deep copy of the vector of attribute
  446.    * values is performed before the value is set.
  447.    *
  448.    * @param value the new attribute value (If the corresponding
  449.    * attribute is nominal (or a string) then this is the new value's
  450.    * index as a double).  
  451.    * @exception UnassignedClassException if the class is not set
  452.    * @exception UnaddignedDatasetException if the instance doesn't
  453.    * have access to a dataset 
  454.    */
  455.   public void setClassValue(double value) {
  456.     if (classIndex() < 0) {
  457.       throw new UnassignedClassException("Class is not set!");
  458.     }
  459.     setValue(classIndex(), value);
  460.   }
  461.   /**
  462.    * Sets the class value of an instance to the given value. A deep
  463.    * copy of the vector of attribute values is performed before the
  464.    * value is set.
  465.    *
  466.    * @param value the new class value (If the class
  467.    * is a string attribute and the value can't be found,
  468.    * the value is added to the attribute).
  469.    * @exception UnassignedClassException if the class is not set
  470.    * @exception UnassignedDatasetException if the dataset is not set
  471.    * @exception IllegalArgumentException if the attribute is not
  472.    * nominal or a string, or the value couldn't be found for a nominal
  473.    * attribute 
  474.    */
  475.   public final void setClassValue(String value) {
  476.     if (classIndex() < 0) {
  477.       throw new UnassignedClassException("Class is not set!");
  478.     }
  479.     setValue(classIndex(), value);
  480.   }
  481.   /**
  482.    * Sets the reference to the dataset. Does not check if the instance
  483.    * is compatible with the dataset. Note: the dataset does not know
  484.    * about this instance. If the structure of the dataset's header
  485.    * gets changed, this instance will not be adjusted automatically.
  486.    *
  487.    * @param instances the reference to the dataset 
  488.    */
  489.   public final void setDataset(Instances instances) {
  490.     
  491.     m_Dataset = instances;
  492.   }
  493.   /**
  494.    * Sets a specific value to be "missing". Performs a deep copy
  495.    * of the vector of attribute values before the value is set to
  496.    * be missing.
  497.    *
  498.    * @param attIndex the attribute's index
  499.    */
  500.   public final void setMissing(int attIndex) {
  501.     setValue(attIndex, MISSING_VALUE);
  502.   }
  503.   /**
  504.    * Sets a specific value to be "missing". Performs a deep copy
  505.    * of the vector of attribute values before the value is set to
  506.    * be missing. The given attribute has to belong to a dataset.
  507.    *
  508.    * @param att the attribute
  509.    */
  510.   public final void setMissing(Attribute att) {
  511.     setMissing(att.index());
  512.   }
  513.   /**
  514.    * Sets a specific value in the instance to the given value 
  515.    * (internal floating-point format). Performs a deep copy
  516.    * of the vector of attribute values before the value is set.
  517.    *
  518.    * @param attIndex the attribute's index 
  519.    * @param value the new attribute value (If the corresponding
  520.    * attribute is nominal (or a string) then this is the new value's
  521.    * index as a double).  
  522.    */
  523.   public void setValue(int attIndex, double value) {
  524.     
  525.     freshAttributeVector();
  526.     m_AttValues[attIndex] = value;
  527.   }
  528.   /**
  529.    * Sets a specific value in the instance to the given value 
  530.    * (internal floating-point format). Performs a deep copy
  531.    * of the vector of attribute values before the value is set.
  532.    * Does exactly the same thing as setValue().
  533.    *
  534.    * @param indexOfIndex the index of the attribute's index 
  535.    * @param value the new attribute value (If the corresponding
  536.    * attribute is nominal (or a string) then this is the new value's
  537.    * index as a double).  
  538.    */
  539.   public void setValueSparse(int indexOfIndex, double value) {
  540.     
  541.     freshAttributeVector();
  542.     m_AttValues[indexOfIndex] = value;
  543.   }
  544.   /**
  545.    * Sets a value of a nominal or string attribute to the given
  546.    * value. Performs a deep copy of the vector of attribute values
  547.    * before the value is set.
  548.    *
  549.    * @param attIndex the attribute's index
  550.    * @param value the new attribute value (If the attribute
  551.    * is a string attribute and the value can't be found,
  552.    * the value is added to the attribute).
  553.    * @exception UnassignedDatasetException if the dataset is not set
  554.    * @exception IllegalArgumentException if the selected
  555.    * attribute is not nominal or a string, or the supplied value couldn't 
  556.    * be found for a nominal attribute 
  557.    */
  558.   public final void setValue(int attIndex, String value) {
  559.     
  560.     int valIndex;
  561.     if (m_Dataset == null) {
  562.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  563.     }
  564.     if (!attribute(attIndex).isNominal() &&
  565. !attribute(attIndex).isString()) {
  566.       throw new IllegalArgumentException("Attribute neither nominal nor string!");
  567.     }
  568.     valIndex = attribute(attIndex).indexOfValue(value);
  569.     if (valIndex == -1) {
  570.       if (attribute(attIndex).isNominal()) {
  571. throw new IllegalArgumentException("Value not defined for given nominal attribute!");
  572.       } else {
  573. attribute(attIndex).forceAddValue(value);
  574. valIndex = attribute(attIndex).indexOfValue(value);
  575.       }
  576.     }
  577.     setValue(attIndex, (double)valIndex); 
  578.   }
  579.   /**
  580.    * Sets a specific value in the instance to the given value
  581.    * (internal floating-point format). Performs a deep copy of the
  582.    * vector of attribute values before the value is set, so if you are
  583.    * planning on calling setValue many times it may be faster to
  584.    * create a new instance using toDoubleArray.  The given attribute
  585.    * has to belong to a dataset.
  586.    *
  587.    * @param att the attribute 
  588.    * @param value the new attribute value (If the corresponding
  589.    * attribute is nominal (or a string) then this is the new value's
  590.    * index as a double).  
  591.    */
  592.   public final void setValue(Attribute att, double value) {
  593.     setValue(att.index(), value);
  594.   }
  595.   /**
  596.    * Sets a value of an nominal or string attribute to the given
  597.    * value. Performs a deep copy of the vector of attribute values
  598.    * before the value is set, so if you are planning on calling setValue many
  599.    * times it may be faster to create a new instance using toDoubleArray.
  600.    * The given attribute has to belong to a dataset.
  601.    *
  602.    * @param att the attribute
  603.    * @param value the new attribute value (If the attribute
  604.    * is a string attribute and the value can't be found,
  605.    * the value is added to the attribute).
  606.    * @exception IllegalArgumentException if the the attribute is not
  607.    * nominal or a string, or the value couldn't be found for a nominal
  608.    * attribute 
  609.    */
  610.   public final void setValue(Attribute att, String value) {
  611.     if (!att.isNominal() &&
  612. !att.isString()) {
  613.       throw new IllegalArgumentException("Attribute neither nominal nor string!");
  614.     }
  615.     int valIndex = att.indexOfValue(value);
  616.     if (valIndex == -1) {
  617.       if (att.isNominal()) {
  618. throw new IllegalArgumentException("Value not defined for given nominal attribute!");
  619.       } else {
  620. att.forceAddValue(value);
  621. valIndex = att.indexOfValue(value);
  622.       }
  623.     }
  624.     setValue(att.index(), (double)valIndex);
  625.   }
  626.   /**
  627.    * Sets the weight of an instance.
  628.    *
  629.    * @param weight the weight
  630.    */
  631.   public final void setWeight(double weight) {
  632.     m_Weight = weight;
  633.   }
  634.   /** 
  635.    * Returns the string value of a nominal, string, or date attribute
  636.    * for the instance.
  637.    *
  638.    * @param attIndex the attribute's index
  639.    * @return the value as a string
  640.    * @exception IllegalArgumentException if the attribute is not a nominal,
  641.    * string, or date attribute.
  642.    * @exception UnassignedDatasetException if the instance doesn't belong
  643.    * to a dataset.
  644.    */
  645.   public final String stringValue(int attIndex) {
  646.     if (m_Dataset == null) {
  647.       throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
  648.     } 
  649.     return stringValue(m_Dataset.attribute(attIndex));
  650.   }
  651.   /** 
  652.    * Returns the string value of a nominal, string, or date attribute
  653.    * for the instance.
  654.    *
  655.    * @param att the attribute
  656.    * @return the value as a string
  657.    * @exception IllegalArgumentException if the attribute is not a nominal,
  658.    * string, or date attribute.
  659.    * @exception UnassignedDatasetException if the instance doesn't belong
  660.    * to a dataset.
  661.    */
  662.   public final String stringValue(Attribute att) {
  663.     int attIndex = att.index();
  664.     switch (att.type()) {
  665.     case Attribute.NOMINAL:
  666.     case Attribute.STRING:
  667.       return att.value((int) value(attIndex));
  668.     case Attribute.DATE:
  669.       return att.formatDate(value(attIndex));
  670.     default:
  671.       throw new IllegalArgumentException("Attribute isn't nominal, string or date!");
  672.     }
  673.   }
  674.   /**
  675.    * Returns the values of each attribute as an array of doubles.
  676.    *
  677.    * @return an array containing all the instance attribute values
  678.    */
  679.   public double[] toDoubleArray() {
  680.     double[] newValues = new double[m_AttValues.length];
  681.     System.arraycopy(m_AttValues, 0, newValues, 0, 
  682.      m_AttValues.length);
  683.     return newValues;
  684.   }
  685.   /**
  686.    * Returns the description of one instance. If the instance
  687.    * doesn't have access to a dataset, it returns the internal
  688.    * floating-point values. Quotes string
  689.    * values that contain whitespace characters.
  690.    *
  691.    * @return the instance's description as a string
  692.    */
  693.   public String toString() {
  694.     StringBuffer text = new StringBuffer();
  695.     
  696.     for (int i = 0; i < m_AttValues.length; i++) {
  697.       if (i > 0) text.append(",");
  698.       text.append(toString(i));
  699.     }
  700.     return text.toString();
  701.   }
  702.   /**
  703.    * Returns the description of one value of the instance as a 
  704.    * string. If the instance doesn't have access to a dataset, it 
  705.    * returns the internal floating-point value. Quotes string
  706.    * values that contain whitespace characters, or if they
  707.    * are a question mark.
  708.    *
  709.    * @param attIndex the attribute's index
  710.    * @return the value's description as a string
  711.    */
  712.   public final String toString(int attIndex) {
  713.    StringBuffer text = new StringBuffer();
  714.    
  715.    if (isMissing(attIndex)) {
  716.      text.append("?");
  717.    } else {
  718.      if (m_Dataset == null) {
  719.        text.append(Utils.doubleToString(m_AttValues[attIndex],6));
  720.      } else {
  721.        switch (m_Dataset.attribute(attIndex).type()) {
  722.        case Attribute.NOMINAL:
  723.        case Attribute.STRING:
  724.        case Attribute.DATE:
  725.          text.append(Utils.quote(stringValue(attIndex)));
  726.          break;
  727.        case Attribute.NUMERIC:
  728.  text.append(Utils.doubleToString(value(attIndex),6));
  729.          break;
  730.        default:
  731.          throw new IllegalStateException("Unknown attribute type");
  732.        }
  733.      }
  734.    }
  735.    return text.toString();
  736.   }
  737.   /**
  738.    * Returns the description of one value of the instance as a 
  739.    * string. If the instance doesn't have access to a dataset it 
  740.    * returns the internal floating-point value. Quotes string
  741.    * values that contain whitespace characters, or if they
  742.    * are a question mark.
  743.    * The given attribute has to belong to a dataset.
  744.    *
  745.    * @param att the attribute
  746.    * @return the value's description as a string
  747.    */
  748.   public final String toString(Attribute att) {
  749.    
  750.    return toString(att.index());
  751.   }
  752.   /**
  753.    * Returns an instance's attribute value in internal format.
  754.    *
  755.    * @param attIndex the attribute's index
  756.    * @return the specified value as a double (If the corresponding
  757.    * attribute is nominal (or a string) then it returns the value's index as a 
  758.    * double).
  759.    */
  760.   public double value(int attIndex) {
  761.     return m_AttValues[attIndex];
  762.   }
  763.   /**
  764.    * Returns an instance's attribute value in internal format.
  765.    * Does exactly the same thing as value() if applied to an Instance.
  766.    *
  767.    * @param indexOfIndex the index of the attribute's index
  768.    * @return the specified value as a double (If the corresponding
  769.    * attribute is nominal (or a string) then it returns the value's index as a 
  770.    * double).
  771.    */
  772.   public double valueSparse(int indexOfIndex) {
  773.     return m_AttValues[indexOfIndex];
  774.   }  
  775.   /**
  776.    * Returns an instance's attribute value in internal format.
  777.    * The given attribute has to belong to a dataset.
  778.    *
  779.    * @param att the attribute
  780.    * @return the specified value as a double (If the corresponding
  781.    * attribute is nominal (or a string) then it returns the value's index as a
  782.    * double).
  783.    */
  784.   public double value(Attribute att) {
  785.     return value(att.index());
  786.   }
  787.   /**
  788.    * Returns the instance's weight.
  789.    *
  790.    * @return the instance's weight as a double
  791.    */
  792.   public final double weight() {
  793.     return m_Weight;
  794.   }
  795.   /**
  796.    * Deletes an attribute at the given position (0 to 
  797.    * numAttributes() - 1).
  798.    *
  799.    * @param pos the attribute's position
  800.    */
  801.   void forceDeleteAttributeAt(int position) {
  802.     double[] newValues = new double[m_AttValues.length - 1];
  803.     System.arraycopy(m_AttValues, 0, newValues, 0, position);
  804.     if (position < m_AttValues.length - 1) {
  805.       System.arraycopy(m_AttValues, position + 1, 
  806.        newValues, position, 
  807.        m_AttValues.length - (position + 1));
  808.     }
  809.     m_AttValues = newValues;
  810.   }
  811.   /**
  812.    * Inserts an attribute at the given position
  813.    * (0 to numAttributes()) and sets its value to be missing. 
  814.    *
  815.    * @param pos the attribute's position
  816.    */
  817.   void forceInsertAttributeAt(int position)  {
  818.     double[] newValues = new double[m_AttValues.length + 1];
  819.     System.arraycopy(m_AttValues, 0, newValues, 0, position);
  820.     newValues[position] = MISSING_VALUE;
  821.     System.arraycopy(m_AttValues, position, newValues, 
  822.      position + 1, m_AttValues.length - position);
  823.     m_AttValues = newValues;
  824.   }
  825.   /**
  826.    * Private constructor for subclasses. Does nothing.
  827.    */
  828.   protected Instance() {
  829.   }
  830.   /**
  831.    * Clones the attribute vector of the instance and
  832.    * overwrites it with the clone.
  833.    */
  834.   private void freshAttributeVector() {
  835.     m_AttValues = toDoubleArray();
  836.   }
  837.   /**
  838.    * Main method for testing this class.
  839.    */
  840.   public static void main(String[] options) {
  841.     try {
  842.       // Create numeric attributes "length" and "weight"
  843.       Attribute length = new Attribute("length");
  844.       Attribute weight = new Attribute("weight");
  845.       
  846.       // Create vector to hold nominal values "first", "second", "third" 
  847.       FastVector my_nominal_values = new FastVector(3); 
  848.       my_nominal_values.addElement("first"); 
  849.       my_nominal_values.addElement("second"); 
  850.       my_nominal_values.addElement("third"); 
  851.       
  852.       // Create nominal attribute "position" 
  853.       Attribute position = new Attribute("position", my_nominal_values);
  854.       
  855.       // Create vector of the above attributes 
  856.       FastVector attributes = new FastVector(3);
  857.       attributes.addElement(length);
  858.       attributes.addElement(weight);
  859.       attributes.addElement(position);
  860.       
  861.       // Create the empty dataset "race" with above attributes
  862.       Instances race = new Instances("race", attributes, 0);
  863.       
  864.       // Make position the class attribute
  865.       race.setClassIndex(position.index());
  866.       
  867.       // Create empty instance with three attribute values
  868.       Instance inst = new Instance(3);
  869.       
  870.       // Set instance's values for the attributes "length", "weight", and "position"
  871.       inst.setValue(length, 5.3);
  872.       inst.setValue(weight, 300);
  873.       inst.setValue(position, "first");
  874.       
  875.       // Set instance's dataset to be the dataset "race"
  876.       inst.setDataset(race);
  877.       
  878.       // Print the instance
  879.       System.out.println("The instance: " + inst);
  880.       
  881.       // Print the first attribute
  882.       System.out.println("First attribute: " + inst.attribute(0));
  883.       
  884.       // Print the class attribute
  885.       System.out.println("Class attribute: " + inst.classAttribute());
  886.       
  887.       // Print the class index
  888.       System.out.println("Class index: " + inst.classIndex());
  889.       
  890.       // Say if class is missing
  891.       System.out.println("Class is missing: " + inst.classIsMissing());
  892.       
  893.       // Print the instance's class value in internal format
  894.       System.out.println("Class value (internal format): " + inst.classValue());
  895.       
  896.       // Print a shallow copy of this instance
  897.       Instance copy = (Instance) inst.copy();
  898.       System.out.println("Shallow copy: " + copy);
  899.       
  900.       // Set dataset for shallow copy
  901.       copy.setDataset(inst.dataset());
  902.       System.out.println("Shallow copy with dataset set: " + copy);
  903.       
  904.       // Unset dataset for copy, delete first attribute, and insert it again
  905.       copy.setDataset(null);
  906.       copy.deleteAttributeAt(0);
  907.       copy.insertAttributeAt(0);
  908.       copy.setDataset(inst.dataset());
  909.       System.out.println("Copy with first attribute deleted and inserted: " + copy); 
  910.       
  911.       // Enumerate attributes (leaving out the class attribute)
  912.       System.out.println("Enumerating attributes (leaving out class):");
  913.       Enumeration enum = inst.enumerateAttributes();
  914.       while (enum.hasMoreElements()) {
  915. Attribute att = (Attribute) enum.nextElement();
  916. System.out.println(att);
  917.       }
  918.       
  919.       // Headers are equivalent?
  920.       System.out.println("Header of original and copy equivalent: " +
  921.  inst.equalHeaders(copy));
  922.       // Test for missing values
  923.       System.out.println("Length of copy missing: " + copy.isMissing(length));
  924.       System.out.println("Weight of copy missing: " + copy.isMissing(weight.index()));
  925.       System.out.println("Length of copy missing: " + 
  926.  Instance.isMissingValue(copy.value(length)));
  927.       System.out.println("Missing value coded as: " + Instance.missingValue());
  928.       // Prints number of attributes and classes
  929.       System.out.println("Number of attributes: " + copy.numAttributes());
  930.       System.out.println("Number of classes: " + copy.numClasses());
  931.       // Replace missing values
  932.       double[] meansAndModes = {2, 3, 0};
  933.       copy.replaceMissingValues(meansAndModes);
  934.       System.out.println("Copy with missing value replaced: " + copy);
  935.       // Setting and getting values and weights
  936.       copy.setClassMissing();
  937.       System.out.println("Copy with missing class: " + copy);
  938.       copy.setClassValue(0);
  939.       System.out.println("Copy with class value set to first value: " + copy);
  940.       copy.setClassValue("third");
  941.       System.out.println("Copy with class value set to "third": " + copy);
  942.       copy.setMissing(1);
  943.       System.out.println("Copy with second attribute set to be missing: " + copy);
  944.       copy.setMissing(length);
  945.       System.out.println("Copy with length set to be missing: " + copy);
  946.       copy.setValue(0, 0);
  947.       System.out.println("Copy with first attribute set to 0: " + copy);
  948.       copy.setValue(weight, 1);
  949.       System.out.println("Copy with weight attribute set to 1: " + copy);
  950.       copy.setValue(position, "second");
  951.       System.out.println("Copy with position set to "second": " + copy);
  952.       copy.setValue(2, "first");
  953.       System.out.println("Copy with last attribute set to "first": " + copy);
  954.       System.out.println("Current weight of instance copy: " + copy.weight());
  955.       copy.setWeight(2);
  956.       System.out.println("Current weight of instance copy (set to 2): " + copy.weight());
  957.       System.out.println("Last value of copy: " + copy.toString(2));
  958.       System.out.println("Value of position for copy: " + copy.toString(position));
  959.       System.out.println("Last value of copy (internal format): " + copy.value(2));
  960.       System.out.println("Value of position for copy (internal format): " + 
  961.  copy.value(position));
  962.     } catch (Exception e) {
  963.       e.printStackTrace();
  964.     }
  965.   }
  966. }