📄 sparseinstance.java
字号:
System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); tempIndices[index + 1] = attIndex; tempValues[index + 1] = value; System.arraycopy(m_AttValues, index + 1, tempValues, index + 2, m_AttValues.length - index - 1); System.arraycopy(m_Indices, index + 1, tempIndices, index + 2, m_Indices.length - index - 1); m_AttValues = tempValues; m_Indices = tempIndices; } } } /** * Sets a specific value in the instance to the given value * (internal floating-point format). Performs a deep copy * of the vector of attribute values before the value is set. * * @param indexOfIndex the index of the attribute's index * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). */ public void setValueSparse(int indexOfIndex, double value) { if (value != 0) { double[] tempValues = new double[m_AttValues.length]; System.arraycopy(m_AttValues, 0, tempValues, 0, m_AttValues.length); m_AttValues = tempValues; m_AttValues[indexOfIndex] = value; } else { double[] tempValues = new double[m_AttValues.length - 1]; int[] tempIndices = new int[m_Indices.length - 1]; System.arraycopy(m_AttValues, 0, tempValues, 0, indexOfIndex); System.arraycopy(m_Indices, 0, tempIndices, 0, indexOfIndex); System.arraycopy(m_AttValues, indexOfIndex + 1, tempValues, indexOfIndex, m_AttValues.length - indexOfIndex - 1); System.arraycopy(m_Indices, indexOfIndex + 1, tempIndices, indexOfIndex, m_Indices.length - indexOfIndex - 1); m_AttValues = tempValues; m_Indices = tempIndices; } } /** * Returns the values of each attribute as an array of doubles. * * @return an array containing all the instance attribute values */ public double[] toDoubleArray() { double[] newValues = new double[m_NumAttributes]; for (int i = 0; i < m_AttValues.length; i++) { newValues[m_Indices[i]] = m_AttValues[i]; } return newValues; } /** * Returns the description of one instance in sparse format. * If the instance doesn't have access to a dataset, it returns the * internal floating-point values. Quotes string values that contain * whitespace characters. * * @return the instance's description as a string */ public String toString() { StringBuffer text = new StringBuffer(); text.append('{'); for (int i = 0; i < m_Indices.length; i++) { if (i > 0) text.append(","); if (isMissingValue(m_AttValues[i])) { text.append(m_Indices[i] + " ?"); } else { if (m_Dataset == null) { text.append(m_Indices[i] + " " + Utils.doubleToString(m_AttValues[i],6)); } else { if (m_Dataset.attribute(m_Indices[i]).isNominal() || m_Dataset.attribute(m_Indices[i]).isString()) { try { text.append(m_Indices[i] + " " + Utils.quote(m_Dataset.attribute(m_Indices[i]). value((int)valueSparse(i)))); } catch (Exception e) { e.printStackTrace(); System.err.println(new Instances(m_Dataset, 0)); System.err.println("Att:" + m_Indices[i] + " Val:" + valueSparse(i)); throw new Error("This should never happen!"); } } else { text.append(m_Indices[i] + " " + Utils.doubleToString(m_AttValues[i],6)); } } } } text.append('}'); return text.toString(); } /** * Returns an instance's attribute value in internal format. * * @param attIndex the attribute's index * @return the specified value as a double (If the corresponding * attribute is nominal (or a string) then it returns the value's index as a * double). */ public double value(int attIndex) { int index = locateIndex(attIndex); if ((index >= 0) && (m_Indices[index] == attIndex)) { return m_AttValues[index]; } else { return 0.0; } } /** * Deletes an attribute at the given position (0 to * numAttributes() - 1). * * @param pos the attribute's position */ void forceDeleteAttributeAt(int position) { int index = locateIndex(position); m_NumAttributes--; if ((index >= 0) && (m_Indices[index] == position)) { int[] tempIndices = new int[m_Indices.length - 1]; double[] tempValues = new double[m_AttValues.length - 1]; System.arraycopy(m_Indices, 0, tempIndices, 0, index); System.arraycopy(m_AttValues, 0, tempValues, 0, index); for (int i = index; i < m_Indices.length - 1; i++) { tempIndices[i] = m_Indices[i + 1] - 1; tempValues[i] = m_AttValues[i + 1]; } m_Indices = tempIndices; m_AttValues = tempValues; } else { int[] tempIndices = new int[m_Indices.length]; double[] tempValues = new double[m_AttValues.length]; System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); for (int i = index + 1; i < m_Indices.length; i++) { tempIndices[i] = m_Indices[i] - 1; tempValues[i] = m_AttValues[i]; } m_Indices = tempIndices; m_AttValues = tempValues; } } /** * Inserts an attribute at the given position * (0 to numAttributes()) and sets its value to be missing. * * @param pos the attribute's position */ void forceInsertAttributeAt(int position) { int index = locateIndex(position); m_NumAttributes++; if ((index >= 0) && (m_Indices[index] == position)) { int[] tempIndices = new int[m_Indices.length + 1]; double[] tempValues = new double[m_AttValues.length + 1]; System.arraycopy(m_Indices, 0, tempIndices, 0, index); System.arraycopy(m_AttValues, 0, tempValues, 0, index); tempIndices[index] = position; tempValues[index] = MISSING_VALUE; for (int i = index; i < m_Indices.length; i++) { tempIndices[i + 1] = m_Indices[i] + 1; tempValues[i + 1] = m_AttValues[i]; } m_Indices = tempIndices; m_AttValues = tempValues; } else { int[] tempIndices = new int[m_Indices.length + 1]; double[] tempValues = new double[m_AttValues.length + 1]; System.arraycopy(m_Indices, 0, tempIndices, 0, index + 1); System.arraycopy(m_AttValues, 0, tempValues, 0, index + 1); tempIndices[index + 1] = position; tempValues[index + 1] = MISSING_VALUE; for (int i = index + 1; i < m_Indices.length; i++) { tempIndices[i + 1] = m_Indices[i] + 1; tempValues[i + 1] = m_AttValues[i]; } m_Indices = tempIndices; m_AttValues = tempValues; } } /** * Main method for testing this class. */ public static void main(String[] options) { try { // Create numeric attributes "length" and "weight" Attribute length = new Attribute("length"); Attribute weight = new Attribute("weight"); // Create vector to hold nominal values "first", "second", "third" FastVector my_nominal_values = new FastVector(3); my_nominal_values.addElement("first"); my_nominal_values.addElement("second"); my_nominal_values.addElement("third"); // Create nominal attribute "position" Attribute position = new Attribute("position", my_nominal_values); // Create vector of the above attributes FastVector attributes = new FastVector(3); attributes.addElement(length); attributes.addElement(weight); attributes.addElement(position); // Create the empty dataset "race" with above attributes Instances race = new Instances("race", attributes, 0); // Make position the class attribute race.setClassIndex(position.index()); // Create empty instance with three attribute values SparseInstance inst = new SparseInstance(3); // Set instance's values for the attributes "length", "weight", and "position" inst.setValue(length, 5.3); inst.setValue(weight, 300); inst.setValue(position, "first"); // Set instance's dataset to be the dataset "race" inst.setDataset(race); // Print the instance System.out.println("The instance: " + inst); // Print the first attribute System.out.println("First attribute: " + inst.attribute(0)); // Print the class attribute System.out.println("Class attribute: " + inst.classAttribute()); // Print the class index System.out.println("Class index: " + inst.classIndex()); // Say if class is missing System.out.println("Class is missing: " + inst.classIsMissing()); // Print the instance's class value in internal format System.out.println("Class value (internal format): " + inst.classValue()); // Print a shallow copy of this instance SparseInstance copy = (SparseInstance) inst.copy(); System.out.println("Shallow copy: " + copy); // Set dataset for shallow copy copy.setDataset(inst.dataset()); System.out.println("Shallow copy with dataset set: " + copy); // Print out all values in internal format System.out.print("All stored values in internal format: "); for (int i = 0; i < inst.numValues(); i++) { if (i > 0) { System.out.print(","); } System.out.print(inst.valueSparse(i)); } System.out.println(); // Set all values to zero System.out.print("All values set to zero: "); while (inst.numValues() > 0) { inst.setValueSparse(0, 0); } for (int i = 0; i < inst.numValues(); i++) { if (i > 0) { System.out.print(","); } System.out.print(inst.valueSparse(i)); } System.out.println(); // Set all values to one System.out.print("All values set to one: "); for (int i = 0; i < inst.numAttributes(); i++) { inst.setValue(i, 1); } for (int i = 0; i < inst.numValues(); i++) { if (i > 0) { System.out.print(","); } System.out.print(inst.valueSparse(i)); } System.out.println(); // Unset dataset for copy, delete first attribute, and insert it again copy.setDataset(null); copy.deleteAttributeAt(0); copy.insertAttributeAt(0); copy.setDataset(inst.dataset()); System.out.println("Copy with first attribute deleted and inserted: " + copy); // Same for second attribute copy.setDataset(null); copy.deleteAttributeAt(1); copy.insertAttributeAt(1); copy.setDataset(inst.dataset()); System.out.println("Copy with second attribute deleted and inserted: " + copy); // Same for last attribute copy.setDataset(null); copy.deleteAttributeAt(2); copy.insertAttributeAt(2); copy.setDataset(inst.dataset()); System.out.println("Copy with third attribute deleted and inserted: " + copy); // Enumerate attributes (leaving out the class attribute) System.out.println("Enumerating attributes (leaving out class):"); Enumeration enum = inst.enumerateAttributes(); while (enum.hasMoreElements()) { Attribute att = (Attribute) enum.nextElement(); System.out.println(att); } // Headers are equivalent? System.out.println("Header of original and copy equivalent: " + inst.equalHeaders(copy)); // Test for missing values System.out.println("Length of copy missing: " + copy.isMissing(length)); System.out.println("Weight of copy missing: " + copy.isMissing(weight.index())); System.out.println("Length of copy missing: " + Instance.isMissingValue(copy.value(length))); System.out.println("Missing value coded as: " + Instance.missingValue()); // Prints number of attributes and classes System.out.println("Number of attributes: " + copy.numAttributes()); System.out.println("Number of classes: " + copy.numClasses()); // Replace missing values double[] meansAndModes = {2, 3, 0}; copy.replaceMissingValues(meansAndModes); System.out.println("Copy with missing value replaced: " + copy); // Setting and getting values and weights copy.setClassMissing(); System.out.println("Copy with missing class: " + copy); copy.setClassValue(0); System.out.println("Copy with class value set to first value: " + copy); copy.setClassValue("third"); System.out.println("Copy with class value set to \"third\": " + copy); copy.setMissing(1); System.out.println("Copy with second attribute set to be missing: " + copy); copy.setMissing(length); System.out.println("Copy with length set to be missing: " + copy); copy.setValue(0, 0); System.out.println("Copy with first attribute set to 0: " + copy); copy.setValue(weight, 1); System.out.println("Copy with weight attribute set to 1: " + copy); copy.setValue(position, "second"); System.out.println("Copy with position set to \"second\": " + copy); copy.setValue(2, "first"); System.out.println("Copy with last attribute set to \"first\": " + copy); System.out.println("Current weight of instance copy: " + copy.weight()); copy.setWeight(2); System.out.println("Current weight of instance copy (set to 2): " + copy.weight()); System.out.println("Last value of copy: " + copy.toString(2)); System.out.println("Value of position for copy: " + copy.toString(position)); System.out.println("Last value of copy (internal format): " + copy.value(2)); System.out.println("Value of position for copy (internal format): " + copy.value(position)); } catch (Exception e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -