📄 instances.java
字号:
*/ public void delete() { m_Instances = new FastVector(); } /** * Removes an instance at the given position from the set. * * @param index the instance's position (index starts with 0) */ //@ requires 0 <= index && index < numInstances(); public void delete(int index) { m_Instances.removeElementAt(index); } /** * Deletes an attribute at the given position * (0 to numAttributes() - 1). A deep copy of the attribute * information is performed before the attribute is deleted. * * @param position the attribute's position (position starts with 0) * @throws IllegalArgumentException if the given index is out of range * or the class attribute is being deleted */ //@ requires 0 <= position && position < numAttributes(); //@ requires position != classIndex(); public void deleteAttributeAt(int position) { if ((position < 0) || (position >= m_Attributes.size())) { throw new IllegalArgumentException("Index out of range"); } if (position == m_ClassIndex) { throw new IllegalArgumentException("Can't delete class attribute"); } freshAttributeInfo(); if (m_ClassIndex > position) { m_ClassIndex--; } m_Attributes.removeElementAt(position); for (int i = position; i < m_Attributes.size(); i++) { Attribute current = (Attribute)m_Attributes.elementAt(i); current.setIndex(current.index() - 1); } for (int i = 0; i < numInstances(); i++) { instance(i).forceDeleteAttributeAt(position); } } /** * Deletes all attributes of the given type in the dataset. A deep copy of * the attribute information is performed before an attribute is deleted. * * @param attType the attribute type to delete * @throws IllegalArgumentException if attribute couldn't be * successfully deleted (probably because it is the class attribute). */ public void deleteAttributeType(int attType) { int i = 0; while (i < m_Attributes.size()) { if (attribute(i).type() == attType) { deleteAttributeAt(i); } else { i++; } } } /** * Deletes all string attributes in the dataset. A deep copy of the attribute * information is performed before an attribute is deleted. * * @throws IllegalArgumentException if string attribute couldn't be * successfully deleted (probably because it is the class attribute). * @see #deleteAttributeType(int) */ public void deleteStringAttributes() { deleteAttributeType(Attribute.STRING); } /** * Removes all instances with missing values for a particular * attribute from the dataset. * * @param attIndex the attribute's index (index starts with 0) */ //@ requires 0 <= attIndex && attIndex < numAttributes(); public void deleteWithMissing(int attIndex) { FastVector newInstances = new FastVector(numInstances()); for (int i = 0; i < numInstances(); i++) { if (!instance(i).isMissing(attIndex)) { newInstances.addElement(instance(i)); } } m_Instances = newInstances; } /** * Removes all instances with missing values for a particular * attribute from the dataset. * * @param att the attribute */ public void deleteWithMissing(/*@non_null@*/ Attribute att) { deleteWithMissing(att.index()); } /** * Removes all instances with a missing class value * from the dataset. * * @throws UnassignedClassException if class is not set */ public void deleteWithMissingClass() { if (m_ClassIndex < 0) { throw new UnassignedClassException("Class index is negative (not set)!"); } deleteWithMissing(m_ClassIndex); } /** * Returns an enumeration of all the attributes. * * @return enumeration of all the attributes. */ public /*@non_null pure@*/ Enumeration enumerateAttributes() { return m_Attributes.elements(m_ClassIndex); } /** * Returns an enumeration of all instances in the dataset. * * @return enumeration of all instances in the dataset */ public /*@non_null pure@*/ Enumeration enumerateInstances() { return m_Instances.elements(); } /** * Checks if two headers are equivalent. * * @param dataset another dataset * @return true if the header of the given dataset is equivalent * to this header */ public /*@pure@*/ boolean equalHeaders(Instances dataset){ // Check class and all attributes if (m_ClassIndex != dataset.m_ClassIndex) { return false; } if (m_Attributes.size() != dataset.m_Attributes.size()) { return false; } for (int i = 0; i < m_Attributes.size(); i++) { if (!(attribute(i).equals(dataset.attribute(i)))) { return false; } } return true; } /** * Returns the first instance in the set. * * @return the first instance in the set */ //@ requires numInstances() > 0; public /*@non_null pure@*/ Instance firstInstance() { return (Instance)m_Instances.firstElement(); } /** * Returns a random number generator. The initial seed of the random * number generator depends on the given seed and the hash code of * a string representation of a instances chosen based on the given * seed. * * @param seed the given seed * @return the random number generator */ public Random getRandomNumberGenerator(long seed) { Random r = new Random(seed); r.setSeed(instance(r.nextInt(numInstances())).toString().hashCode() + seed); return r; } /** * Inserts an attribute at the given position (0 to * numAttributes()) and sets all values to be missing. * Shallow copies the attribute before it is inserted, and performs * a deep copy of the existing attribute information. * * @param att the attribute to be inserted * @param position the attribute's position (position starts with 0) * @throws IllegalArgumentException if the given index is out of range */ //@ requires 0 <= position; //@ requires position <= numAttributes(); public void insertAttributeAt(/*@non_null@*/ Attribute att, int position) { if ((position < 0) || (position > m_Attributes.size())) { throw new IllegalArgumentException("Index out of range"); } att = (Attribute)att.copy(); freshAttributeInfo(); att.setIndex(position); m_Attributes.insertElementAt(att, position); for (int i = position + 1; i < m_Attributes.size(); i++) { Attribute current = (Attribute)m_Attributes.elementAt(i); current.setIndex(current.index() + 1); } for (int i = 0; i < numInstances(); i++) { instance(i).forceInsertAttributeAt(position); } if (m_ClassIndex >= position) { m_ClassIndex++; } } /** * Returns the instance at the given position. * * @param index the instance's index (index starts with 0) * @return the instance at the given position */ //@ requires 0 <= index; //@ requires index < numInstances(); public /*@non_null pure@*/ Instance instance(int index) { return (Instance)m_Instances.elementAt(index); } /** * Returns the kth-smallest attribute value of a numeric attribute. * Note that calling this method will change the order of the data! * * @param att the Attribute object * @param k the value of k * @return the kth-smallest value */ public double kthSmallestValue(Attribute att, int k) { return kthSmallestValue(att.index(), k); } /** * Returns the kth-smallest attribute value of a numeric attribute. * Note that calling this method will change the order of the data! * The number of non-missing values in the data must be as least * as last as k for this to work. * * @param attIndex the attribute's index * @param k the value of k * @return the kth-smallest value */ public double kthSmallestValue(int attIndex, int k) { if (!attribute(attIndex).isNumeric()) { throw new IllegalArgumentException("Instances: attribute must be numeric to compute kth-smallest value."); } int i,j; // move all instances with missing values to end j = numInstances() - 1; i = 0; while (i <= j) { if (instance(j).isMissing(attIndex)) { j--; } else { if (instance(i).isMissing(attIndex)) { swap(i,j); j--; } i++; } } if ((k < 1) || (k > j+1)) { throw new IllegalArgumentException("Instances: value for k for computing kth-smallest value too large."); } return instance(select(attIndex, 0, j, k)).value(attIndex); } /** * Returns the last instance in the set. * * @return the last instance in the set */ //@ requires numInstances() > 0; public /*@non_null pure@*/ Instance lastInstance() { return (Instance)m_Instances.lastElement(); } /** * Returns the mean (mode) for a numeric (nominal) attribute as * a floating-point value. Returns 0 if the attribute is neither nominal nor * numeric. If all values are missing it returns zero. * * @param attIndex the attribute's index (index starts with 0) * @return the mean or the mode */ public /*@pure@*/ double meanOrMode(int attIndex) { double result, found; int [] counts; if (attribute(attIndex).isNumeric()) { result = found = 0; for (int j = 0; j < numInstances(); j++) { if (!instance(j).isMissing(attIndex)) { found += instance(j).weight(); result += instance(j).weight()*instance(j).value(attIndex); } } if (found <= 0) { return 0; } else { return result / found; } } else if (attribute(attIndex).isNominal()) { counts = new int[attribute(attIndex).numValues()]; for (int j = 0; j < numInstances(); j++) { if (!instance(j).isMissing(attIndex)) { counts[(int) instance(j).value(attIndex)] += instance(j).weight(); } } return (double)Utils.maxIndex(counts); } else { return 0; } } /** * Returns the mean (mode) for a numeric (nominal) attribute as a * floating-point value. Returns 0 if the attribute is neither * nominal nor numeric. If all values are missing it returns zero. * * @param att the attribute * @return the mean or the mode */ public /*@pure@*/ double meanOrMode(Attribute att) { return meanOrMode(att.index()); } /** * Returns the number of attributes. * * @return the number of attributes as an integer */ //@ ensures \result == m_Attributes.size(); public /*@pure@*/ int numAttributes() { return m_Attributes.size(); } /** * Returns the number of class labels. * * @return the number of class labels as an integer if the class * attribute is nominal, 1 otherwise. * @throws UnassignedClassException if the class is not set */ //@ requires classIndex() >= 0; public /*@pure@*/ int numClasses() { if (m_ClassIndex < 0) { throw new UnassignedClassException("Class index is negative (not set)!"); } if (!classAttribute().isNominal()) { return 1; } else { return classAttribute().numValues(); } } /** * Returns the number of distinct values of a given attribute. * Returns the number of instances if the attribute is a * string attribute. The value 'missing' is not counted. * * @param attIndex the attribute (index starts with 0) * @return the number of distinct values of a given attribute */ //@ requires 0 <= attIndex; //@ requires attIndex < numAttributes(); public /*@pure@*/ int numDistinctValues(int attIndex) { if (attribute(attIndex).isNumeric()) { double [] attVals = attributeToDoubleArray(attIndex); int [] sorted = Utils.sort(attVals); double prev = 0; int counter = 0; for (int i = 0; i < sorted.length; i++) { Instance current = instance(sorted[i]); if (current.isMissing(attIndex)) { break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -