source: src/main/java/weka/core/DenseInstance.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 16.6 KB
Line 
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/*
18 *    DenseInstance.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core;
24
25import java.io.Serializable;
26import java.util.Enumeration;
27import java.util.ArrayList;
28
29/**
30 * Class for handling an instance. All values (numeric, date, nominal, string
31 * or relational) are internally stored as floating-point numbers. If an
32 * attribute is nominal (or a string or relational), the stored value is the
33 * index of the corresponding nominal (or string or relational) value in the
34 * attribute's definition. We have chosen this approach in favor of a more
35 * elegant object-oriented approach because it is much faster. <p>
36 *
37 * Typical usage (code from the main() method of this class): <p>
38 *
39 * <code>
40 * ... <br>
41 *     
42 * // Create empty instance with three attribute values <br>
43 * Instance inst = new DenseInstance(3); <br><br>
44 *     
45 * // Set instance's values for the attributes "length", "weight", and "position"<br>
46 * inst.setValue(length, 5.3); <br>
47 * inst.setValue(weight, 300); <br>
48 * inst.setValue(position, "first"); <br><br>
49 *   
50 * // Set instance's dataset to be the dataset "race" <br>
51 * inst.setDataset(race); <br><br>
52 *   
53 * // Print the instance <br>
54 * System.out.println("The instance: " + inst); <br>
55 *
56 * ... <br>
57 * </code><p>
58 *
59 * All methods that change an instance's attribute values are safe,
60 * ie. a change of an instance's attribute values does not affect any
61 * other instances. All methods that change an instance's attribute
62 * values clone the attribute value vector before it is changed. If
63 * your application heavily modifies instance values, it may be faster
64 * to create a new instance from scratch.
65 *
66 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
67 * @version $Revision: 5987 $
68 */
69public class DenseInstance extends AbstractInstance {
70 
71  /** for serialization */
72  static final long serialVersionUID = 1482635194499365122L;
73
74  /**
75   * Constructor that copies the attribute values and the weight from
76   * the given instance. It does NOT perform a deep copy of the
77   * attribute values if the instance provided is also of type
78   * DenseInstance (it simply copies the reference to the array of
79   * values), otherwise it does. Reference to the dataset is set to
80   * null.  (ie. the instance doesn't have access to information about
81   * the attribute types)
82   *
83   * @param instance the instance from which the attribute
84   * values and the weight are to be copied
85   */
86  //@ ensures m_Dataset == null;
87  public DenseInstance(/*@non_null@*/ Instance instance) {
88     
89    if (instance instanceof DenseInstance) {
90      m_AttValues = ((DenseInstance)instance).m_AttValues;
91    } else {
92      m_AttValues = instance.toDoubleArray();
93    }
94    m_Weight = instance.weight();
95    m_Dataset = null;
96  }
97
98  /**
99   * Constructor that inititalizes instance variable with given
100   * values. Reference to the dataset is set to null. (ie. the instance
101   * doesn't have access to information about the attribute types)
102   *
103   * @param weight the instance's weight
104   * @param attValues a vector of attribute values
105   */
106  //@ ensures m_Dataset == null;
107  public DenseInstance(double weight,  /*@non_null@*/ double[]attValues){
108   
109    m_AttValues = attValues;
110    m_Weight = weight;
111    m_Dataset = null;
112  }
113
114  /**
115   * Constructor of an instance that sets weight to one, all values to
116   * be missing, and the reference to the dataset to null. (ie. the instance
117   * doesn't have access to information about the attribute types)
118   *
119   * @param numAttributes the size of the instance
120   */
121  //@ requires numAttributes > 0;    // Or maybe == 0 is okay too?
122  //@ ensures m_Dataset == null;
123  public DenseInstance(int numAttributes) {
124   
125    m_AttValues = new double[numAttributes];
126    for (int i = 0; i < m_AttValues.length; i++) {
127      m_AttValues[i] = Utils.missingValue();
128    }
129    m_Weight = 1;
130    m_Dataset = null;
131  }
132
133  /**
134   * Produces a shallow copy of this instance. The copy has
135   * access to the same dataset. (if you want to make a copy
136   * that doesn't have access to the dataset, use
137   * <code>new DenseInstance(instance)</code>
138   *
139   * @return the shallow copy
140   */
141  //@ also ensures \result != null;
142  //@ also ensures \result instanceof DenseInstance;
143  //@ also ensures ((DenseInstance)\result).m_Dataset == m_Dataset;
144  public /*@pure@*/ Object copy() {
145
146    DenseInstance result = new DenseInstance(this);
147    result.m_Dataset = m_Dataset;
148    return result;
149  }
150
151  /**
152   * Returns the index of the attribute stored at the given position.
153   * Just returns the given value.
154   *
155   * @param position the position
156   * @return the index of the attribute stored at the given position
157   */
158  public /*@pure@*/ int index(int position) {
159
160    return position;
161  }
162
163  /**
164   * Merges this instance with the given instance and returns
165   * the result. Dataset is set to null. The returned instance
166   * is of the same type as this instance.
167   *
168   * @param inst the instance to be merged with this one
169   * @return the merged instances
170   */
171  public Instance mergeInstance(Instance inst) {
172
173    int m = 0;
174    double [] newVals = new double[numAttributes() + inst.numAttributes()];
175    for (int j = 0; j < numAttributes(); j++, m++) {
176      newVals[m] = value(j);
177    }
178    for (int j = 0; j < inst.numAttributes(); j++, m++) {
179      newVals[m] = inst.value(j);
180    }
181    return new DenseInstance(1.0, newVals);
182  }
183
184  /**
185   * Returns the number of attributes.
186   *
187   * @return the number of attributes as an integer
188   */
189  //@ ensures \result == m_AttValues.length;
190  public /*@pure@*/ int numAttributes() {
191
192    return m_AttValues.length;
193  }
194
195  /**
196   * Returns the number of values present. Always the same as numAttributes().
197   *
198   * @return the number of values
199   */
200  //@ ensures \result == m_AttValues.length;
201  public /*@pure@*/ int numValues() {
202
203    return m_AttValues.length;
204  }
205
206  /**
207   * Replaces all missing values in the instance with the
208   * values contained in the given array. A deep copy of
209   * the vector of attribute values is performed before the
210   * values are replaced.
211   *
212   * @param array containing the means and modes
213   * @throws IllegalArgumentException if numbers of attributes are unequal
214   */
215  public void replaceMissingValues(double[] array) {
216         
217    if ((array == null) || 
218        (array.length != m_AttValues.length)) {
219      throw new IllegalArgumentException("Unequal number of attributes!");
220    }
221    freshAttributeVector();
222    for (int i = 0; i < m_AttValues.length; i++) {
223      if (isMissing(i)) {
224        m_AttValues[i] = array[i];
225      }
226    }
227  }
228
229  /**
230   * Sets a specific value in the instance to the given value
231   * (internal floating-point format). Performs a deep copy
232   * of the vector of attribute values before the value is set.
233   *
234   * @param attIndex the attribute's index
235   * @param value the new attribute value (If the corresponding
236   * attribute is nominal (or a string) then this is the new value's
237   * index as a double). 
238   */
239  public void setValue(int attIndex, double value) {
240   
241    freshAttributeVector();
242    m_AttValues[attIndex] = value;
243  }
244
245  /**
246   * Sets a specific value in the instance to the given value
247   * (internal floating-point format). Performs a deep copy
248   * of the vector of attribute values before the value is set.
249   * Does exactly the same thing as setValue().
250   *
251   * @param indexOfIndex the index of the attribute's index
252   * @param value the new attribute value (If the corresponding
253   * attribute is nominal (or a string) then this is the new value's
254   * index as a double). 
255   */
256  public void setValueSparse(int indexOfIndex, double value) {
257   
258    freshAttributeVector();
259    m_AttValues[indexOfIndex] = value;
260  }
261
262  /**
263   * Returns the values of each attribute as an array of doubles.
264   *
265   * @return an array containing all the instance attribute values
266   */
267  public double[] toDoubleArray() {
268
269    double[] newValues = new double[m_AttValues.length];
270    System.arraycopy(m_AttValues, 0, newValues, 0, 
271                     m_AttValues.length);
272    return newValues;
273  }
274
275  /**
276   * Returns the description of one instance (without weight
277   * appended). If the instance
278   * doesn't have access to a dataset, it returns the internal
279   * floating-point values. Quotes string
280   * values that contain whitespace characters.
281   *
282   * This method is used by getRandomNumberGenerator() in
283   * Instances.java in order to maintain backwards compatibility
284   * with weka 3.4.
285   *
286   * @return the instance's description as a string
287   */
288  public String toStringNoWeight() {
289    StringBuffer text = new StringBuffer();
290   
291    for (int i = 0; i < m_AttValues.length; i++) {
292      if (i > 0) text.append(",");
293      text.append(toString(i));
294    }
295
296    return text.toString();
297  }
298
299  /**
300   * Returns an instance's attribute value in internal format.
301   *
302   * @param attIndex the attribute's index
303   * @return the specified value as a double (If the corresponding
304   * attribute is nominal (or a string) then it returns the value's index as a
305   * double).
306   */
307  public /*@pure@*/ double value(int attIndex) {
308
309    return m_AttValues[attIndex];
310  }
311
312  /**
313   * Deletes an attribute at the given position (0 to
314   * numAttributes() - 1).
315   *
316   * @param position the attribute's position
317   */
318  protected void forceDeleteAttributeAt(int position) {
319
320    double[] newValues = new double[m_AttValues.length - 1];
321
322    System.arraycopy(m_AttValues, 0, newValues, 0, position);
323    if (position < m_AttValues.length - 1) {
324      System.arraycopy(m_AttValues, position + 1, 
325                       newValues, position, 
326                       m_AttValues.length - (position + 1));
327    }
328    m_AttValues = newValues;
329  }
330
331  /**
332   * Inserts an attribute at the given position
333   * (0 to numAttributes()) and sets its value to be missing.
334   *
335   * @param position the attribute's position
336   */
337  protected void forceInsertAttributeAt(int position)  {
338
339    double[] newValues = new double[m_AttValues.length + 1];
340
341    System.arraycopy(m_AttValues, 0, newValues, 0, position);
342    newValues[position] = Utils.missingValue();
343    System.arraycopy(m_AttValues, position, newValues, 
344                     position + 1, m_AttValues.length - position);
345    m_AttValues = newValues;
346  }
347
348  /**
349   * Clones the attribute vector of the instance and
350   * overwrites it with the clone.
351   */
352  private void freshAttributeVector() {
353
354    m_AttValues = toDoubleArray();
355  }
356
357  /**
358   * Main method for testing this class.
359   *
360   * @param options the commandline options - ignored
361   */
362  //@ requires options != null;
363  public static void main(String[] options) {
364
365    try {
366
367      // Create numeric attributes "length" and "weight"
368      Attribute length = new Attribute("length");
369      Attribute weight = new Attribute("weight");
370     
371      // Create vector to hold nominal values "first", "second", "third"
372      ArrayList<String> my_nominal_values = new ArrayList<String>(3); 
373      my_nominal_values.add("first"); 
374      my_nominal_values.add("second"); 
375      my_nominal_values.add("third"); 
376     
377      // Create nominal attribute "position"
378      Attribute position = new Attribute("position", my_nominal_values);
379     
380      // Create vector of the above attributes
381      ArrayList<Attribute> attributes = new ArrayList<Attribute>(3);
382      attributes.add(length);
383      attributes.add(weight);
384      attributes.add(position);
385     
386      // Create the empty dataset "race" with above attributes
387      Instances race = new Instances("race", attributes, 0);
388     
389      // Make position the class attribute
390      race.setClassIndex(position.index());
391     
392      // Create empty instance with three attribute values
393      Instance inst = new DenseInstance(3);
394     
395      // Set instance's values for the attributes "length", "weight", and "position"
396      inst.setValue(length, 5.3);
397      inst.setValue(weight, 300);
398      inst.setValue(position, "first");
399     
400      // Set instance's dataset to be the dataset "race"
401      inst.setDataset(race);
402     
403      // Print the instance
404      System.out.println("The instance: " + inst);
405     
406      // Print the first attribute
407      System.out.println("First attribute: " + inst.attribute(0));
408     
409      // Print the class attribute
410      System.out.println("Class attribute: " + inst.classAttribute());
411     
412      // Print the class index
413      System.out.println("Class index: " + inst.classIndex());
414     
415      // Say if class is missing
416      System.out.println("Class is missing: " + inst.classIsMissing());
417     
418      // Print the instance's class value in internal format
419      System.out.println("Class value (internal format): " + inst.classValue());
420     
421      // Print a shallow copy of this instance
422      Instance copy = (Instance) inst.copy();
423      System.out.println("Shallow copy: " + copy);
424     
425      // Set dataset for shallow copy
426      copy.setDataset(inst.dataset());
427      System.out.println("Shallow copy with dataset set: " + copy);
428     
429      // Unset dataset for copy, delete first attribute, and insert it again
430      copy.setDataset(null);
431      copy.deleteAttributeAt(0);
432      copy.insertAttributeAt(0);
433      copy.setDataset(inst.dataset());
434      System.out.println("Copy with first attribute deleted and inserted: " + copy); 
435     
436      // Enumerate attributes (leaving out the class attribute)
437      System.out.println("Enumerating attributes (leaving out class):");
438      Enumeration enu = inst.enumerateAttributes();
439      while (enu.hasMoreElements()) {
440        Attribute att = (Attribute) enu.nextElement();
441        System.out.println(att);
442      }
443     
444      // Headers are equivalent?
445      System.out.println("Header of original and copy equivalent: " +
446                         inst.equalHeaders(copy));
447
448      // Test for missing values
449      System.out.println("Length of copy missing: " + copy.isMissing(length));
450      System.out.println("Weight of copy missing: " + copy.isMissing(weight.index()));
451      System.out.println("Length of copy missing: " + 
452                         Utils.isMissingValue(copy.value(length)));
453
454      // Prints number of attributes and classes
455      System.out.println("Number of attributes: " + copy.numAttributes());
456      System.out.println("Number of classes: " + copy.numClasses());
457
458      // Replace missing values
459      double[] meansAndModes = {2, 3, 0};
460      copy.replaceMissingValues(meansAndModes);
461      System.out.println("Copy with missing value replaced: " + copy);
462
463      // Setting and getting values and weights
464      copy.setClassMissing();
465      System.out.println("Copy with missing class: " + copy);
466      copy.setClassValue(0);
467      System.out.println("Copy with class value set to first value: " + copy);
468      copy.setClassValue("third");
469      System.out.println("Copy with class value set to \"third\": " + copy);
470      copy.setMissing(1);
471      System.out.println("Copy with second attribute set to be missing: " + copy);
472      copy.setMissing(length);
473      System.out.println("Copy with length set to be missing: " + copy);
474      copy.setValue(0, 0);
475      System.out.println("Copy with first attribute set to 0: " + copy);
476      copy.setValue(weight, 1);
477      System.out.println("Copy with weight attribute set to 1: " + copy);
478      copy.setValue(position, "second");
479      System.out.println("Copy with position set to \"second\": " + copy);
480      copy.setValue(2, "first");
481      System.out.println("Copy with last attribute set to \"first\": " + copy);
482      System.out.println("Current weight of instance copy: " + copy.weight());
483      copy.setWeight(2);
484      System.out.println("Current weight of instance copy (set to 2): " + copy.weight());
485      System.out.println("Last value of copy: " + copy.toString(2));
486      System.out.println("Value of position for copy: " + copy.toString(position));
487      System.out.println("Last value of copy (internal format): " + copy.value(2));
488      System.out.println("Value of position for copy (internal format): " + 
489                         copy.value(position));
490    } catch (Exception e) {
491      e.printStackTrace();
492    }
493  }
494 
495  /**
496   * Returns the revision string.
497   *
498   * @return            the revision
499   */
500  public String getRevision() {
501    return RevisionUtils.extract("$Revision: 5987 $");
502  }
503}
Note: See TracBrowser for help on using the repository browser.