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 | * SparseInstance.java |
---|
19 | * Copyright (C) 2000 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.core; |
---|
24 | |
---|
25 | import java.io.Serializable; |
---|
26 | import java.util.Enumeration; |
---|
27 | import java.util.ArrayList; |
---|
28 | |
---|
29 | /** |
---|
30 | * Class for storing an instance as a sparse vector. A sparse instance |
---|
31 | * only requires storage for those attribute values that are non-zero. |
---|
32 | * Since the objective is to reduce storage requirements for datasets |
---|
33 | * with large numbers of default values, this also includes nominal |
---|
34 | * attributes -- the first nominal value (i.e. that which has index 0) |
---|
35 | * will not require explicit storage, so rearrange your nominal attribute |
---|
36 | * value orderings if necessary. Missing values will be stored |
---|
37 | * explicitly. |
---|
38 | * |
---|
39 | * @author Eibe Frank |
---|
40 | * @version $Revision: 5987 $ |
---|
41 | */ |
---|
42 | public class SparseInstance extends AbstractInstance { |
---|
43 | |
---|
44 | /** for serialization */ |
---|
45 | private static final long serialVersionUID = -3579051291332630149L; |
---|
46 | |
---|
47 | /** The index of the attribute associated with each stored value. */ |
---|
48 | protected int[] m_Indices; |
---|
49 | |
---|
50 | /** The maximum number of values that can be stored. */ |
---|
51 | protected int m_NumAttributes; |
---|
52 | |
---|
53 | /** |
---|
54 | * Constructor that generates a sparse instance from the given |
---|
55 | * instance. Reference to the dataset is set to null. |
---|
56 | * (ie. the instance doesn't have access to information about the |
---|
57 | * attribute types) |
---|
58 | * |
---|
59 | * @param instance the instance from which the attribute values |
---|
60 | * and the weight are to be copied |
---|
61 | */ |
---|
62 | public SparseInstance(Instance instance) { |
---|
63 | |
---|
64 | m_Weight = instance.weight(); |
---|
65 | m_Dataset = null; |
---|
66 | m_NumAttributes = instance.numAttributes(); |
---|
67 | if (instance instanceof SparseInstance) { |
---|
68 | m_AttValues = ((SparseInstance)instance).m_AttValues; |
---|
69 | m_Indices = ((SparseInstance)instance).m_Indices; |
---|
70 | } else { |
---|
71 | double[] tempValues = new double[instance.numAttributes()]; |
---|
72 | int[] tempIndices = new int[instance.numAttributes()]; |
---|
73 | int vals = 0; |
---|
74 | for (int i = 0; i < instance.numAttributes(); i++) { |
---|
75 | if (instance.value(i) != 0) { |
---|
76 | tempValues[vals] = instance.value(i); |
---|
77 | tempIndices[vals] = i; |
---|
78 | vals++; |
---|
79 | } |
---|
80 | } |
---|
81 | m_AttValues = new double[vals]; |
---|
82 | m_Indices = new int[vals]; |
---|
83 | System.arraycopy(tempValues, 0, m_AttValues, 0, vals); |
---|
84 | System.arraycopy(tempIndices, 0, m_Indices, 0, vals); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Constructor that copies the info from the given instance. |
---|
90 | * Reference to the dataset is set to null. |
---|
91 | * (ie. the instance doesn't have access to information about the |
---|
92 | * attribute types) |
---|
93 | * |
---|
94 | * @param instance the instance from which the attribute |
---|
95 | * info is to be copied |
---|
96 | */ |
---|
97 | public SparseInstance(SparseInstance instance) { |
---|
98 | |
---|
99 | m_AttValues = instance.m_AttValues; |
---|
100 | m_Indices = instance.m_Indices; |
---|
101 | m_Weight = instance.m_Weight; |
---|
102 | m_NumAttributes = instance.m_NumAttributes; |
---|
103 | m_Dataset = null; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Constructor that generates a sparse instance from the given |
---|
108 | * parameters. Reference to the dataset is set to null. |
---|
109 | * (ie. the instance doesn't have access to information about the |
---|
110 | * attribute types) |
---|
111 | * |
---|
112 | * @param weight the instance's weight |
---|
113 | * @param attValues a vector of attribute values |
---|
114 | */ |
---|
115 | public SparseInstance(double weight, double[] attValues) { |
---|
116 | |
---|
117 | m_Weight = weight; |
---|
118 | m_Dataset = null; |
---|
119 | m_NumAttributes = attValues.length; |
---|
120 | double[] tempValues = new double[m_NumAttributes]; |
---|
121 | int[] tempIndices = new int[m_NumAttributes]; |
---|
122 | int vals = 0; |
---|
123 | for (int i = 0; i < m_NumAttributes; i++) { |
---|
124 | if (attValues[i] != 0) { |
---|
125 | tempValues[vals] = attValues[i]; |
---|
126 | tempIndices[vals] = i; |
---|
127 | vals++; |
---|
128 | } |
---|
129 | } |
---|
130 | m_AttValues = new double[vals]; |
---|
131 | m_Indices = new int[vals]; |
---|
132 | System.arraycopy(tempValues, 0, m_AttValues, 0, vals); |
---|
133 | System.arraycopy(tempIndices, 0, m_Indices, 0, vals); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Constructor that inititalizes instance variable with given |
---|
138 | * values. Reference to the dataset is set to null. (ie. the instance |
---|
139 | * doesn't have access to information about the attribute types) |
---|
140 | * Note that the indices need to be sorted in ascending order. Otherwise |
---|
141 | * things won't work properly. |
---|
142 | * |
---|
143 | * @param weight the instance's weight |
---|
144 | * @param attValues a vector of attribute values (just the ones to be stored) |
---|
145 | * @param indices the indices of the given values in the full vector (need to |
---|
146 | * be sorted in ascending order) |
---|
147 | * @param maxNumValues the maximium number of values that can be stored |
---|
148 | */ |
---|
149 | public SparseInstance(double weight, double[] attValues, |
---|
150 | int[] indices, int maxNumValues){ |
---|
151 | |
---|
152 | int vals = 0; |
---|
153 | m_AttValues = new double [attValues.length]; |
---|
154 | m_Indices = new int [indices.length]; |
---|
155 | for (int i = 0; i < attValues.length; i++) { |
---|
156 | if (attValues[i] != 0) { |
---|
157 | m_AttValues[vals] = attValues[i]; |
---|
158 | m_Indices[vals] = indices[i]; |
---|
159 | vals++; |
---|
160 | } |
---|
161 | } |
---|
162 | if (vals != attValues.length) { |
---|
163 | // Need to truncate. |
---|
164 | double [] newVals = new double[vals]; |
---|
165 | System.arraycopy(m_AttValues, 0, newVals, 0, vals); |
---|
166 | m_AttValues = newVals; |
---|
167 | int [] newIndices = new int[vals]; |
---|
168 | System.arraycopy(m_Indices, 0, newIndices, 0, vals); |
---|
169 | m_Indices = newIndices; |
---|
170 | } |
---|
171 | m_Weight = weight; |
---|
172 | m_NumAttributes = maxNumValues; |
---|
173 | m_Dataset = null; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Constructor of an instance that sets weight to one, all values to |
---|
178 | * be missing, and the reference to the dataset to null. (ie. the instance |
---|
179 | * doesn't have access to information about the attribute types) |
---|
180 | * |
---|
181 | * @param numAttributes the size of the instance |
---|
182 | */ |
---|
183 | public SparseInstance(int numAttributes) { |
---|
184 | |
---|
185 | m_AttValues = new double[numAttributes]; |
---|
186 | m_NumAttributes = numAttributes; |
---|
187 | m_Indices = new int[numAttributes]; |
---|
188 | for (int i = 0; i < m_AttValues.length; i++) { |
---|
189 | m_AttValues[i] = Utils.missingValue(); |
---|
190 | m_Indices[i] = i; |
---|
191 | } |
---|
192 | m_Weight = 1; |
---|
193 | m_Dataset = null; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Produces a shallow copy of this instance. The copy has |
---|
198 | * access to the same dataset. (if you want to make a copy |
---|
199 | * that doesn't have access to the dataset, use |
---|
200 | * <code>new SparseInstance(instance)</code> |
---|
201 | * |
---|
202 | * @return the shallow copy |
---|
203 | */ |
---|
204 | public Object copy() { |
---|
205 | |
---|
206 | SparseInstance result = new SparseInstance(this); |
---|
207 | result.m_Dataset = m_Dataset; |
---|
208 | return result; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Returns the index of the attribute stored at the given position. |
---|
213 | * |
---|
214 | * @param position the position |
---|
215 | * @return the index of the attribute stored at the given position |
---|
216 | */ |
---|
217 | public int index(int position) { |
---|
218 | |
---|
219 | return m_Indices[position]; |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Locates the greatest index that is not greater than the |
---|
224 | * given index. |
---|
225 | * |
---|
226 | * @return the internal index of the attribute index. Returns |
---|
227 | * -1 if no index with this property could be found |
---|
228 | */ |
---|
229 | public int locateIndex(int index) { |
---|
230 | |
---|
231 | int min = 0, max = m_Indices.length - 1; |
---|
232 | |
---|
233 | if (max == -1) { |
---|
234 | return -1; |
---|
235 | } |
---|
236 | |
---|
237 | // Binary search |
---|
238 | while ((m_Indices[min] <= index) && (m_Indices[max] >= index)) { |
---|
239 | int current = (max + min) / 2; |
---|
240 | if (m_Indices[current] > index) { |
---|
241 | max = current - 1; |
---|
242 | } else if (m_Indices[current] < index) { |
---|
243 | min = current + 1; |
---|
244 | } else { |
---|
245 | return current; |
---|
246 | } |
---|
247 | } |
---|
248 | if (m_Indices[max] < index) { |
---|
249 | return max; |
---|
250 | } else { |
---|
251 | return min - 1; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Merges this instance with the given instance and returns |
---|
257 | * the result. Dataset is set to null. |
---|
258 | * |
---|
259 | * @param inst the instance to be merged with this one |
---|
260 | * @return the merged instances |
---|
261 | */ |
---|
262 | public Instance mergeInstance(Instance inst) { |
---|
263 | |
---|
264 | double[] values = new double[numValues() + inst.numValues()]; |
---|
265 | int[] indices = new int[numValues() + inst.numValues()]; |
---|
266 | |
---|
267 | int m = 0; |
---|
268 | for (int j = 0; j < numValues(); j++, m++) { |
---|
269 | values[m] = valueSparse(j); |
---|
270 | indices[m] = index(j); |
---|
271 | } |
---|
272 | for (int j = 0; j < inst.numValues(); j++, m++) { |
---|
273 | values[m] = inst.valueSparse(j); |
---|
274 | indices[m] = numAttributes() + inst.index(j); |
---|
275 | } |
---|
276 | |
---|
277 | return new SparseInstance(1.0, values, indices, numAttributes() + |
---|
278 | inst.numAttributes()); |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Returns the number of attributes. |
---|
283 | * |
---|
284 | * @return the number of attributes as an integer |
---|
285 | */ |
---|
286 | public int numAttributes() { |
---|
287 | |
---|
288 | return m_NumAttributes; |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Returns the number of values in the sparse vector. |
---|
293 | * |
---|
294 | * @return the number of values |
---|
295 | */ |
---|
296 | public int numValues() { |
---|
297 | |
---|
298 | return m_Indices.length; |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Replaces all missing values in the instance with the |
---|
303 | * values contained in the given array. A deep copy of |
---|
304 | * the vector of attribute values is performed before the |
---|
305 | * values are replaced. |
---|
306 | * |
---|
307 | * @param array containing the means and modes |
---|
308 | * @exception IllegalArgumentException if numbers of attributes are unequal |
---|
309 | */ |
---|
310 | public void replaceMissingValues(double[] array) { |
---|
311 | |
---|
312 | if ((array == null) || (array.length != m_NumAttributes)) { |
---|
313 | throw new IllegalArgumentException("Unequal number of attributes!"); |
---|
314 | } |
---|
315 | double[] tempValues = new double[m_AttValues.length]; |
---|
316 | int[] tempIndices = new int[m_AttValues.length]; |
---|
317 | int vals = 0; |
---|
318 | for (int i = 0; i < m_AttValues.length; i++) { |
---|
319 | if (isMissingSparse(i)) { |
---|
320 | if (array[m_Indices[i]] != 0) { |
---|
321 | tempValues[vals] = array[m_Indices[i]]; |
---|
322 | tempIndices[vals] = m_Indices[i]; |
---|
323 | vals++; |
---|
324 | } |
---|
325 | } else { |
---|
326 | tempValues[vals] = m_AttValues[i]; |
---|
327 | tempIndices[vals] = m_Indices[i]; |
---|
328 | vals++; |
---|
329 | } |
---|
330 | } |
---|
331 | m_AttValues = new double[vals]; |
---|
332 | m_Indices = new int[vals]; |
---|
333 | System.arraycopy(tempValues, 0, m_AttValues, 0, vals); |
---|
334 | System.arraycopy(tempIndices, 0, m_Indices, 0, vals); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * Sets a specific value in the instance to the given value |
---|
339 | * (internal floating-point format). Performs a deep copy |
---|
340 | * of the vector of attribute values before the value is set. |
---|
341 | * |
---|
342 | * @param attIndex the attribute's index |
---|
343 | * @param value the new attribute value (If the corresponding |
---|
344 | * attribute is nominal (or a string) then this is the new value's |
---|
345 | * index as a double). |
---|
346 | */ |
---|
347 | public void setValue(int attIndex, double value) { |
---|
348 | |
---|
349 | int index = locateIndex(attIndex); |
---|
350 | |
---|
351 | if ((index >= 0) && (m_Indices[index] == attIndex)) { |
---|
352 | if (value != 0) { |
---|
353 | double[] tempValues = new double[m_AttValues.length]; |
---|
354 | System.arraycopy(m_AttValues, 0, tempValues, 0, m_AttValues.length); |
---|
355 | tempValues[index] = value; |
---|
356 | m_AttValues = tempValues; |
---|
357 | } else { |
---|
358 | double[] tempValues = new double[m_AttValues.length - 1]; |
---|
359 | int[] tempIndices = new int[m_Indices.length - 1]; |
---|
360 | System.arraycopy(m_AttValues, 0, tempValues, 0, index); |
---|
361 | System.arraycopy(m_Indices, 0, tempIndices, 0, index); |
---|
362 | System.arraycopy(m_AttValues, index + 1, tempValues, index, |
---|
363 | m_AttValues.length - index - 1); |
---|
364 | System.arraycopy(m_Indices, index + 1, tempIndices, index, |
---|
365 | m_Indices.length - index - 1); |
---|
366 | m_AttValues = tempValues; |
---|
367 | m_Indices = tempIndices; |
---|
368 | } |
---|
369 | } else { |
---|
370 | if (value != 0) { |
---|
371 | double[] tempValues = new double[m_AttValues.length + 1]; |
---|
372 | int[] tempIndices = new int[m_Indices.length + 1]; |
---|
373 | System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); |
---|
374 | System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); |
---|
375 | tempIndices[index + 1] = attIndex; |
---|
376 | tempValues[index + 1] = value; |
---|
377 | System.arraycopy(m_AttValues, index + 1, tempValues, index + 2, |
---|
378 | m_AttValues.length - index - 1); |
---|
379 | System.arraycopy(m_Indices, index + 1, tempIndices, index + 2, |
---|
380 | m_Indices.length - index - 1); |
---|
381 | m_AttValues = tempValues; |
---|
382 | m_Indices = tempIndices; |
---|
383 | } |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * Sets a specific value in the instance to the given value |
---|
389 | * (internal floating-point format). Performs a deep copy |
---|
390 | * of the vector of attribute values before the value is set. |
---|
391 | * |
---|
392 | * @param indexOfIndex the index of the attribute's index |
---|
393 | * @param value the new attribute value (If the corresponding |
---|
394 | * attribute is nominal (or a string) then this is the new value's |
---|
395 | * index as a double). |
---|
396 | */ |
---|
397 | public void setValueSparse(int indexOfIndex, double value) { |
---|
398 | |
---|
399 | if (value != 0) { |
---|
400 | double[] tempValues = new double[m_AttValues.length]; |
---|
401 | System.arraycopy(m_AttValues, 0, tempValues, 0, m_AttValues.length); |
---|
402 | m_AttValues = tempValues; |
---|
403 | m_AttValues[indexOfIndex] = value; |
---|
404 | } else { |
---|
405 | double[] tempValues = new double[m_AttValues.length - 1]; |
---|
406 | int[] tempIndices = new int[m_Indices.length - 1]; |
---|
407 | System.arraycopy(m_AttValues, 0, tempValues, 0, indexOfIndex); |
---|
408 | System.arraycopy(m_Indices, 0, tempIndices, 0, indexOfIndex); |
---|
409 | System.arraycopy(m_AttValues, indexOfIndex + 1, tempValues, indexOfIndex, |
---|
410 | m_AttValues.length - indexOfIndex - 1); |
---|
411 | System.arraycopy(m_Indices, indexOfIndex + 1, tempIndices, indexOfIndex, |
---|
412 | m_Indices.length - indexOfIndex - 1); |
---|
413 | m_AttValues = tempValues; |
---|
414 | m_Indices = tempIndices; |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * Returns the values of each attribute as an array of doubles. |
---|
420 | * |
---|
421 | * @return an array containing all the instance attribute values |
---|
422 | */ |
---|
423 | public double[] toDoubleArray() { |
---|
424 | |
---|
425 | double[] newValues = new double[m_NumAttributes]; |
---|
426 | for (int i = 0; i < m_AttValues.length; i++) { |
---|
427 | newValues[m_Indices[i]] = m_AttValues[i]; |
---|
428 | } |
---|
429 | return newValues; |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Returns the description of one instance in sparse format. |
---|
434 | * If the instance doesn't have access to a dataset, it returns the |
---|
435 | * internal floating-point values. Quotes string values that contain |
---|
436 | * whitespace characters. |
---|
437 | * |
---|
438 | * @return the instance's description as a string |
---|
439 | */ |
---|
440 | public String toStringNoWeight() { |
---|
441 | |
---|
442 | StringBuffer text = new StringBuffer(); |
---|
443 | |
---|
444 | text.append('{'); |
---|
445 | for (int i = 0; i < m_Indices.length; i++) { |
---|
446 | if (i > 0) text.append(","); |
---|
447 | if (isMissingSparse(i)) { |
---|
448 | text.append(m_Indices[i] + " ?"); |
---|
449 | } else { |
---|
450 | if (m_Dataset == null) { |
---|
451 | text.append(m_Indices[i] + " " + |
---|
452 | Utils.doubleToString(m_AttValues[i],6)); |
---|
453 | } else { |
---|
454 | if (m_Dataset.attribute(m_Indices[i]).isNominal() || |
---|
455 | m_Dataset.attribute(m_Indices[i]).isString() || |
---|
456 | m_Dataset.attribute(m_Indices[i]).isDate()) { |
---|
457 | try { |
---|
458 | text.append(m_Indices[i] + " " + |
---|
459 | Utils.quote(stringValue(m_Indices[i]))); |
---|
460 | } catch (Exception e) { |
---|
461 | e.printStackTrace(); |
---|
462 | System.err.println(new Instances(m_Dataset, 0)); |
---|
463 | System.err.println("Att:" + m_Indices[i] + " Val:" + valueSparse(i)); |
---|
464 | throw new Error("This should never happen!"); |
---|
465 | } |
---|
466 | } else if (m_Dataset.attribute(m_Indices[i]).isRelationValued()) { |
---|
467 | try { |
---|
468 | text.append(m_Indices[i] + " " + |
---|
469 | Utils.quote(m_Dataset.attribute(m_Indices[i]). |
---|
470 | relation((int)valueSparse(i)). |
---|
471 | stringWithoutHeader())); |
---|
472 | } catch (Exception e) { |
---|
473 | e.printStackTrace(); |
---|
474 | System.err.println(new Instances(m_Dataset, 0)); |
---|
475 | System.err.println("Att:" + m_Indices[i] + " Val:" + valueSparse(i)); |
---|
476 | throw new Error("This should never happen!"); |
---|
477 | } |
---|
478 | } else { |
---|
479 | text.append(m_Indices[i] + " " + |
---|
480 | Utils.doubleToString(m_AttValues[i],6)); |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | } |
---|
485 | text.append('}'); |
---|
486 | |
---|
487 | return text.toString(); |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * Returns an instance's attribute value in internal format. |
---|
492 | * |
---|
493 | * @param attIndex the attribute's index |
---|
494 | * @return the specified value as a double (If the corresponding |
---|
495 | * attribute is nominal (or a string) then it returns the value's index as a |
---|
496 | * double). |
---|
497 | */ |
---|
498 | public double value(int attIndex) { |
---|
499 | |
---|
500 | int index = locateIndex(attIndex); |
---|
501 | if ((index >= 0) && (m_Indices[index] == attIndex)) { |
---|
502 | return m_AttValues[index]; |
---|
503 | } else { |
---|
504 | return 0.0; |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | /** |
---|
509 | * Deletes an attribute at the given position (0 to |
---|
510 | * numAttributes() - 1). |
---|
511 | * |
---|
512 | * @param pos the attribute's position |
---|
513 | */ |
---|
514 | protected void forceDeleteAttributeAt(int position) { |
---|
515 | |
---|
516 | int index = locateIndex(position); |
---|
517 | |
---|
518 | m_NumAttributes--; |
---|
519 | if ((index >= 0) && (m_Indices[index] == position)) { |
---|
520 | int[] tempIndices = new int[m_Indices.length - 1]; |
---|
521 | double[] tempValues = new double[m_AttValues.length - 1]; |
---|
522 | System.arraycopy(m_Indices, 0, tempIndices, 0, index); |
---|
523 | System.arraycopy(m_AttValues, 0, tempValues, 0, index); |
---|
524 | for (int i = index; i < m_Indices.length - 1; i++) { |
---|
525 | tempIndices[i] = m_Indices[i + 1] - 1; |
---|
526 | tempValues[i] = m_AttValues[i + 1]; |
---|
527 | } |
---|
528 | m_Indices = tempIndices; |
---|
529 | m_AttValues = tempValues; |
---|
530 | } else { |
---|
531 | int[] tempIndices = new int[m_Indices.length]; |
---|
532 | double[] tempValues = new double[m_AttValues.length]; |
---|
533 | System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); |
---|
534 | System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); |
---|
535 | for (int i = index + 1; i < m_Indices.length; i++) { |
---|
536 | tempIndices[i] = m_Indices[i] - 1; |
---|
537 | tempValues[i] = m_AttValues[i]; |
---|
538 | } |
---|
539 | m_Indices = tempIndices; |
---|
540 | m_AttValues = tempValues; |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | /** |
---|
545 | * Inserts an attribute at the given position |
---|
546 | * (0 to numAttributes()) and sets its value to be missing. |
---|
547 | * |
---|
548 | * @param pos the attribute's position |
---|
549 | */ |
---|
550 | protected void forceInsertAttributeAt(int position) { |
---|
551 | |
---|
552 | int index = locateIndex(position); |
---|
553 | |
---|
554 | m_NumAttributes++; |
---|
555 | if ((index >= 0) && (m_Indices[index] == position)) { |
---|
556 | int[] tempIndices = new int[m_Indices.length + 1]; |
---|
557 | double[] tempValues = new double[m_AttValues.length + 1]; |
---|
558 | System.arraycopy(m_Indices, 0, tempIndices, 0, index); |
---|
559 | System.arraycopy(m_AttValues, 0, tempValues, 0, index); |
---|
560 | tempIndices[index] = position; |
---|
561 | tempValues[index] = Utils.missingValue(); |
---|
562 | for (int i = index; i < m_Indices.length; i++) { |
---|
563 | tempIndices[i + 1] = m_Indices[i] + 1; |
---|
564 | tempValues[i + 1] = m_AttValues[i]; |
---|
565 | } |
---|
566 | m_Indices = tempIndices; |
---|
567 | m_AttValues = tempValues; |
---|
568 | } else { |
---|
569 | int[] tempIndices = new int[m_Indices.length + 1]; |
---|
570 | double[] tempValues = new double[m_AttValues.length + 1]; |
---|
571 | System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); |
---|
572 | System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); |
---|
573 | tempIndices[index + 1] = position; |
---|
574 | tempValues[index + 1] = Utils.missingValue(); |
---|
575 | for (int i = index + 1; i < m_Indices.length; i++) { |
---|
576 | tempIndices[i + 1] = m_Indices[i] + 1; |
---|
577 | tempValues[i + 1] = m_AttValues[i]; |
---|
578 | } |
---|
579 | m_Indices = tempIndices; |
---|
580 | m_AttValues = tempValues; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | /** |
---|
585 | * Constructor for sub classes. |
---|
586 | */ |
---|
587 | protected SparseInstance() {}; |
---|
588 | |
---|
589 | /** |
---|
590 | * Main method for testing this class. |
---|
591 | */ |
---|
592 | public static void main(String[] options) { |
---|
593 | |
---|
594 | try { |
---|
595 | |
---|
596 | // Create numeric attributes "length" and "weight" |
---|
597 | Attribute length = new Attribute("length"); |
---|
598 | Attribute weight = new Attribute("weight"); |
---|
599 | |
---|
600 | // Create vector to hold nominal values "first", "second", "third" |
---|
601 | ArrayList<String> my_nominal_values = new ArrayList<String>(3); |
---|
602 | my_nominal_values.add("first"); |
---|
603 | my_nominal_values.add("second"); |
---|
604 | my_nominal_values.add("third"); |
---|
605 | |
---|
606 | // Create nominal attribute "position" |
---|
607 | Attribute position = new Attribute("position", my_nominal_values); |
---|
608 | |
---|
609 | // Create vector of the above attributes |
---|
610 | ArrayList<Attribute> attributes = new ArrayList<Attribute>(3); |
---|
611 | attributes.add(length); |
---|
612 | attributes.add(weight); |
---|
613 | attributes.add(position); |
---|
614 | |
---|
615 | // Create the empty dataset "race" with above attributes |
---|
616 | Instances race = new Instances("race", attributes, 0); |
---|
617 | |
---|
618 | // Make position the class attribute |
---|
619 | race.setClassIndex(position.index()); |
---|
620 | |
---|
621 | // Create empty instance with three attribute values |
---|
622 | SparseInstance inst = new SparseInstance(3); |
---|
623 | |
---|
624 | // Set instance's values for the attributes "length", "weight", and "position" |
---|
625 | inst.setValue(length, 5.3); |
---|
626 | inst.setValue(weight, 300); |
---|
627 | inst.setValue(position, "first"); |
---|
628 | |
---|
629 | // Set instance's dataset to be the dataset "race" |
---|
630 | inst.setDataset(race); |
---|
631 | |
---|
632 | // Print the instance |
---|
633 | System.out.println("The instance: " + inst); |
---|
634 | |
---|
635 | // Print the first attribute |
---|
636 | System.out.println("First attribute: " + inst.attribute(0)); |
---|
637 | |
---|
638 | // Print the class attribute |
---|
639 | System.out.println("Class attribute: " + inst.classAttribute()); |
---|
640 | |
---|
641 | // Print the class index |
---|
642 | System.out.println("Class index: " + inst.classIndex()); |
---|
643 | |
---|
644 | // Say if class is missing |
---|
645 | System.out.println("Class is missing: " + inst.classIsMissing()); |
---|
646 | |
---|
647 | // Print the instance's class value in internal format |
---|
648 | System.out.println("Class value (internal format): " + inst.classValue()); |
---|
649 | |
---|
650 | // Print a shallow copy of this instance |
---|
651 | SparseInstance copy = (SparseInstance) inst.copy(); |
---|
652 | System.out.println("Shallow copy: " + copy); |
---|
653 | |
---|
654 | // Set dataset for shallow copy |
---|
655 | copy.setDataset(inst.dataset()); |
---|
656 | System.out.println("Shallow copy with dataset set: " + copy); |
---|
657 | |
---|
658 | // Print out all values in internal format |
---|
659 | System.out.print("All stored values in internal format: "); |
---|
660 | for (int i = 0; i < inst.numValues(); i++) { |
---|
661 | if (i > 0) { |
---|
662 | System.out.print(","); |
---|
663 | } |
---|
664 | System.out.print(inst.valueSparse(i)); |
---|
665 | } |
---|
666 | System.out.println(); |
---|
667 | |
---|
668 | // Set all values to zero |
---|
669 | System.out.print("All values set to zero: "); |
---|
670 | while (inst.numValues() > 0) { |
---|
671 | inst.setValueSparse(0, 0); |
---|
672 | } |
---|
673 | for (int i = 0; i < inst.numValues(); i++) { |
---|
674 | if (i > 0) { |
---|
675 | System.out.print(","); |
---|
676 | } |
---|
677 | System.out.print(inst.valueSparse(i)); |
---|
678 | } |
---|
679 | System.out.println(); |
---|
680 | |
---|
681 | // Set all values to one |
---|
682 | System.out.print("All values set to one: "); |
---|
683 | for (int i = 0; i < inst.numAttributes(); i++) { |
---|
684 | inst.setValue(i, 1); |
---|
685 | } |
---|
686 | for (int i = 0; i < inst.numValues(); i++) { |
---|
687 | if (i > 0) { |
---|
688 | System.out.print(","); |
---|
689 | } |
---|
690 | System.out.print(inst.valueSparse(i)); |
---|
691 | } |
---|
692 | System.out.println(); |
---|
693 | |
---|
694 | // Unset dataset for copy, delete first attribute, and insert it again |
---|
695 | copy.setDataset(null); |
---|
696 | copy.deleteAttributeAt(0); |
---|
697 | copy.insertAttributeAt(0); |
---|
698 | copy.setDataset(inst.dataset()); |
---|
699 | System.out.println("Copy with first attribute deleted and inserted: " + copy); |
---|
700 | |
---|
701 | // Same for second attribute |
---|
702 | copy.setDataset(null); |
---|
703 | copy.deleteAttributeAt(1); |
---|
704 | copy.insertAttributeAt(1); |
---|
705 | copy.setDataset(inst.dataset()); |
---|
706 | System.out.println("Copy with second attribute deleted and inserted: " + copy); |
---|
707 | |
---|
708 | // Same for last attribute |
---|
709 | copy.setDataset(null); |
---|
710 | copy.deleteAttributeAt(2); |
---|
711 | copy.insertAttributeAt(2); |
---|
712 | copy.setDataset(inst.dataset()); |
---|
713 | System.out.println("Copy with third attribute deleted and inserted: " + copy); |
---|
714 | |
---|
715 | // Enumerate attributes (leaving out the class attribute) |
---|
716 | System.out.println("Enumerating attributes (leaving out class):"); |
---|
717 | Enumeration enu = inst.enumerateAttributes(); |
---|
718 | while (enu.hasMoreElements()) { |
---|
719 | Attribute att = (Attribute) enu.nextElement(); |
---|
720 | System.out.println(att); |
---|
721 | } |
---|
722 | |
---|
723 | // Headers are equivalent? |
---|
724 | System.out.println("Header of original and copy equivalent: " + |
---|
725 | inst.equalHeaders(copy)); |
---|
726 | |
---|
727 | // Test for missing values |
---|
728 | System.out.println("Length of copy missing: " + copy.isMissing(length)); |
---|
729 | System.out.println("Weight of copy missing: " + copy.isMissing(weight.index())); |
---|
730 | System.out.println("Length of copy missing: " + |
---|
731 | Utils.isMissingValue(copy.value(length))); |
---|
732 | |
---|
733 | // Prints number of attributes and classes |
---|
734 | System.out.println("Number of attributes: " + copy.numAttributes()); |
---|
735 | System.out.println("Number of classes: " + copy.numClasses()); |
---|
736 | |
---|
737 | // Replace missing values |
---|
738 | double[] meansAndModes = {2, 3, 0}; |
---|
739 | copy.replaceMissingValues(meansAndModes); |
---|
740 | System.out.println("Copy with missing value replaced: " + copy); |
---|
741 | |
---|
742 | // Setting and getting values and weights |
---|
743 | copy.setClassMissing(); |
---|
744 | System.out.println("Copy with missing class: " + copy); |
---|
745 | copy.setClassValue(0); |
---|
746 | System.out.println("Copy with class value set to first value: " + copy); |
---|
747 | copy.setClassValue("third"); |
---|
748 | System.out.println("Copy with class value set to \"third\": " + copy); |
---|
749 | copy.setMissing(1); |
---|
750 | System.out.println("Copy with second attribute set to be missing: " + copy); |
---|
751 | copy.setMissing(length); |
---|
752 | System.out.println("Copy with length set to be missing: " + copy); |
---|
753 | copy.setValue(0, 0); |
---|
754 | System.out.println("Copy with first attribute set to 0: " + copy); |
---|
755 | copy.setValue(weight, 1); |
---|
756 | System.out.println("Copy with weight attribute set to 1: " + copy); |
---|
757 | copy.setValue(position, "second"); |
---|
758 | System.out.println("Copy with position set to \"second\": " + copy); |
---|
759 | copy.setValue(2, "first"); |
---|
760 | System.out.println("Copy with last attribute set to \"first\": " + copy); |
---|
761 | System.out.println("Current weight of instance copy: " + copy.weight()); |
---|
762 | copy.setWeight(2); |
---|
763 | System.out.println("Current weight of instance copy (set to 2): " + copy.weight()); |
---|
764 | System.out.println("Last value of copy: " + copy.toString(2)); |
---|
765 | System.out.println("Value of position for copy: " + copy.toString(position)); |
---|
766 | System.out.println("Last value of copy (internal format): " + copy.value(2)); |
---|
767 | System.out.println("Value of position for copy (internal format): " + |
---|
768 | copy.value(position)); |
---|
769 | } catch (Exception e) { |
---|
770 | e.printStackTrace(); |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | /** |
---|
775 | * Returns the revision string. |
---|
776 | * |
---|
777 | * @return the revision |
---|
778 | */ |
---|
779 | public String getRevision() { |
---|
780 | return RevisionUtils.extract("$Revision: 5987 $"); |
---|
781 | } |
---|
782 | } |
---|