Package weka.core

Class InstancesView

  • All Implemented Interfaces:
    Serializable, Iterable<weka.core.Instance>, Collection<weka.core.Instance>, List<weka.core.Instance>, weka.core.RevisionHandler

    public class InstancesView
    extends weka.core.Instances
    Presents a view of an Instances object. Rows can be limited.
    Version:
    $Revision$
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected weka.core.Instances m_Dataset
      the underlying dataset.
      protected gnu.trove.list.TIntList m_Rows
      the rows to use.
      • Fields inherited from class weka.core.Instances

        ARFF_DATA, ARFF_RELATION, FILE_EXTENSION, m_Attributes, m_ClassIndex, m_Instances, m_Lines, m_NamesToAttributeIndices, m_RelationName, SERIALIZED_OBJ_FILE_EXTENSION
    • Constructor Summary

      Constructors 
      Constructor Description
      InstancesView​(weka.core.Instances dataset, int[] rows)
      Initializes the dataset.
      InstancesView​(weka.core.Instances dataset, int fromRow, int toRow)
      Initializes the dataset.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, weka.core.Instance instance)
      Adds one instance at the given position in the list.
      boolean add​(weka.core.Instance instance)
      Adds one instance to the end of the set.
      weka.core.AttributeStats attributeStats​(int index)
      Calculates summary statistics on the values that appear in this set of instances for a specified attribute.
      void compactify()
      Does nothing.
      void delete()
      Removes all instances from the set.
      void delete​(int index)
      Removes an instance at the given position from the set.
      void deleteAttributeAt​(int position)
      Deletes an attribute at the given position (0 to numAttributes() - 1).
      void deleteAttributeType​(int attType)
      Deletes all attributes of the given type in the dataset.
      void deleteWithMissing​(int attIndex)
      Removes all instances with missing values for a particular attribute from the dataset.
      Enumeration<weka.core.Instance> enumerateInstances()
      Returns an enumeration of all instances in the dataset.
      weka.core.Instance firstInstance()
      Returns the first instance in the set.
      weka.core.Instance get​(int index)
      Returns the instance at the given position.
      void insertAttributeAt​(weka.core.Attribute att, int position)
      Inserts an attribute at the given position (0 to numAttributes()) and sets all values to be missing.
      weka.core.Instance instance​(int index)
      Returns the instance at the given position.
      weka.core.Instance lastInstance()
      Returns the last instance in the set.
      int numClasses()
      Returns the number of class labels.
      int numInstances()
      Returns the number of instances in the dataset.
      weka.core.Instance remove​(int index)
      Removes the instance at the given position.
      void renameAttribute​(int att, String name)
      Renames an attribute.
      void renameAttributeValue​(int att, int val, String name)
      Renames the value of a nominal (or string) attribute value.
      void replaceAttributeAt​(weka.core.Attribute att, int position)
      Replaces the attribute at the given position (0 to numAttributes()) with the given attribute and sets all its values to be missing.
      weka.core.Instance set​(int index, weka.core.Instance instance)
      Replaces the instance at the given position.
      void setClassIndex​(int classIndex)
      Sets the class index of the set.
      int size()
      Returns the number of instances in the dataset.
      void sort​(int attIndex)
      Sorts the instances based on an attribute.
      protected void sortBasedOnNominalAttribute​(int attIndex)
      Sorts a nominal attribute (stable, linear-time sort).
      void stableSort​(int attIndex)
      Sorts the instances based on an attribute, using a stable sort.
      protected void stratStep​(int numFolds)
      Help function needed for stratification of set.
      weka.core.Instances stringFreeStructure()
      Create a copy of the structure.
      void swap​(int i, int j)
      Swaps two instances in the set.
      • Methods inherited from class weka.core.Instances

        attribute, attribute, attributeToDoubleArray, checkForAttributeType, checkForStringAttributes, checkInstance, classAttribute, classIndex, copyInstances, deleteStringAttributes, deleteWithMissing, deleteWithMissingClass, enumerateAttributes, equalHeaders, equalHeadersMsg, getRandomNumberGenerator, getRevision, initialize, instancesAndWeights, kthSmallestValue, kthSmallestValue, main, meanOrMode, meanOrMode, mergeInstances, numAttributes, numDistinctValues, numDistinctValues, randomize, readInstance, relationName, renameAttribute, renameAttributeValue, resample, resampleWithWeights, resampleWithWeights, resampleWithWeights, resampleWithWeights, resampleWithWeights, resampleWithWeights, resampleWithWeights, setClass, setRelationName, sort, stableSort, stratify, stringWithoutHeader, sumOfWeights, test, testCV, toString, toSummaryString, trainCV, trainCV, variance, variance, variances
    • Field Detail

      • m_Dataset

        protected weka.core.Instances m_Dataset
        the underlying dataset.
      • m_Rows

        protected gnu.trove.list.TIntList m_Rows
        the rows to use.
    • Constructor Detail

      • InstancesView

        public InstancesView​(weka.core.Instances dataset,
                             int[] rows)
        Initializes the dataset.
        Parameters:
        dataset - the underlying dataset
        rows - the rows to use, null for all
      • InstancesView

        public InstancesView​(weka.core.Instances dataset,
                             int fromRow,
                             int toRow)
        Initializes the dataset.
        Parameters:
        dataset - the underlying dataset
        fromRow - the first row
        toRow - the last row (excluded)
    • Method Detail

      • stringFreeStructure

        public weka.core.Instances stringFreeStructure()
        Create a copy of the structure. If the data has string or relational attributes, theses are replaced by empty copies. Other attributes are left unmodified, but the underlying list structure holding references to the attributes is shallow-copied, so that other Instances objects with a reference to this list are not affected.
        Overrides:
        stringFreeStructure in class weka.core.Instances
        Returns:
        a copy of the instance structure.
      • add

        public boolean add​(weka.core.Instance instance)
        Adds one instance to the end of the set. Shallow copies instance before it is added. Increases the size of the dataset if it is not large enough. Does not check if the instance is compatible with the dataset. Note: String or relational values are not transferred.
        Specified by:
        add in interface Collection<weka.core.Instance>
        Specified by:
        add in interface List<weka.core.Instance>
        Overrides:
        add in class weka.core.Instances
        Parameters:
        instance - the instance to be added
      • add

        public void add​(int index,
                        weka.core.Instance instance)
        Adds one instance at the given position in the list. Shallow copies instance before it is added. Increases the size of the dataset if it is not large enough. Does not check if the instance is compatible with the dataset. Note: String or relational values are not transferred.
        Specified by:
        add in interface List<weka.core.Instance>
        Overrides:
        add in class weka.core.Instances
        Parameters:
        index - position where instance is to be inserted
        instance - the instance to be added
      • compactify

        public void compactify()
        Does nothing.
        Overrides:
        compactify in class weka.core.Instances
      • delete

        public void delete()
        Removes all instances from the set.
        Overrides:
        delete in class weka.core.Instances
      • delete

        public void delete​(int index)
        Removes an instance at the given position from the set.
        Overrides:
        delete in class weka.core.Instances
        Parameters:
        index - the instance's position (index starts with 0)
      • deleteAttributeAt

        public void deleteAttributeAt​(int position)
        Deletes an attribute at the given position (0 to numAttributes() - 1). Attribute objects after the deletion point are copied so that their indices can be decremented. Creates a fresh list to hold the old and new attribute objects.
        Overrides:
        deleteAttributeAt in class weka.core.Instances
        Parameters:
        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
      • deleteAttributeType

        public void deleteAttributeType​(int attType)
        Deletes all attributes of the given type in the dataset. A deep copy of the attribute information is performed before an attribute is deleted.
        Overrides:
        deleteAttributeType in class weka.core.Instances
        Parameters:
        attType - the attribute type to delete
        Throws:
        IllegalArgumentException - if attribute couldn't be successfully deleted (probably because it is the class attribute).
      • deleteWithMissing

        public void deleteWithMissing​(int attIndex)
        Removes all instances with missing values for a particular attribute from the dataset.
        Overrides:
        deleteWithMissing in class weka.core.Instances
        Parameters:
        attIndex - the attribute's index (index starts with 0)
      • enumerateInstances

        public Enumeration<weka.core.Instance> enumerateInstances()
        Returns an enumeration of all instances in the dataset.
        Overrides:
        enumerateInstances in class weka.core.Instances
        Returns:
        enumeration of all instances in the dataset
      • firstInstance

        public weka.core.Instance firstInstance()
        Returns the first instance in the set.
        Overrides:
        firstInstance in class weka.core.Instances
        Returns:
        the first instance in the set
      • insertAttributeAt

        public void insertAttributeAt​(weka.core.Attribute att,
                                      int position)
        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. Existing attribute objects at and after the insertion point are also copied so that their indices can be incremented. Creates a fresh list to hold the old and new attribute objects.
        Overrides:
        insertAttributeAt in class weka.core.Instances
        Parameters:
        att - the attribute to be inserted
        position - the attribute's position (position starts with 0)
        Throws:
        IllegalArgumentException - if the given index is out of range
      • instance

        public weka.core.Instance instance​(int index)
        Returns the instance at the given position.
        Overrides:
        instance in class weka.core.Instances
        Parameters:
        index - the instance's index (index starts with 0)
        Returns:
        the instance at the given position
      • get

        public weka.core.Instance get​(int index)
        Returns the instance at the given position.
        Specified by:
        get in interface List<weka.core.Instance>
        Overrides:
        get in class weka.core.Instances
        Parameters:
        index - the instance's index (index starts with 0)
        Returns:
        the instance at the given position
      • lastInstance

        public weka.core.Instance lastInstance()
        Returns the last instance in the set.
        Overrides:
        lastInstance in class weka.core.Instances
        Returns:
        the last instance in the set
      • numClasses

        public int numClasses()
        Returns the number of class labels.
        Overrides:
        numClasses in class weka.core.Instances
        Returns:
        the number of class labels as an integer if the class attribute is nominal, 1 otherwise.
        Throws:
        weka.core.UnassignedClassException - if the class is not set
      • size

        public int size()
        Returns the number of instances in the dataset.
        Specified by:
        size in interface Collection<weka.core.Instance>
        Specified by:
        size in interface List<weka.core.Instance>
        Overrides:
        size in class weka.core.Instances
        Returns:
        the number of instances in the dataset as an integer
      • numInstances

        public int numInstances()
        Returns the number of instances in the dataset.
        Overrides:
        numInstances in class weka.core.Instances
        Returns:
        the number of instances in the dataset as an integer
      • swap

        public void swap​(int i,
                         int j)
        Swaps two instances in the set.
        Overrides:
        swap in class weka.core.Instances
        Parameters:
        i - the first instance's index (index starts with 0)
        j - the second instance's index (index starts with 0)
      • replaceAttributeAt

        public void replaceAttributeAt​(weka.core.Attribute att,
                                       int position)
        Replaces the attribute at the given position (0 to numAttributes()) with the given attribute and sets all its values to be missing. Shallow copies the given attribute before it is inserted. Creates a fresh list to hold the old and new attribute objects.
        Overrides:
        replaceAttributeAt in class weka.core.Instances
        Parameters:
        att - the attribute to be inserted
        position - the attribute's position (position starts with 0)
        Throws:
        IllegalArgumentException - if the given index is out of range
      • remove

        public weka.core.Instance remove​(int index)
        Removes the instance at the given position.
        Specified by:
        remove in interface List<weka.core.Instance>
        Overrides:
        remove in class weka.core.Instances
        Parameters:
        index - the instance's index (index starts with 0)
        Returns:
        the instance at the given position
      • renameAttribute

        public void renameAttribute​(int att,
                                    String name)
        Renames an attribute. This change only affects this dataset.
        Overrides:
        renameAttribute in class weka.core.Instances
        Parameters:
        att - the attribute's index (index starts with 0)
        name - the new name
      • renameAttributeValue

        public void renameAttributeValue​(int att,
                                         int val,
                                         String name)
        Renames the value of a nominal (or string) attribute value. This change only affects this dataset.
        Overrides:
        renameAttributeValue in class weka.core.Instances
        Parameters:
        att - the attribute's index (index starts with 0)
        val - the value's index (index starts with 0)
        name - the new name
      • set

        public weka.core.Instance set​(int index,
                                      weka.core.Instance instance)
        Replaces the instance at the given position. Shallow copies instance before it is added. Does not check if the instance is compatible with the dataset. Note: String or relational values are not transferred.
        Specified by:
        set in interface List<weka.core.Instance>
        Overrides:
        set in class weka.core.Instances
        Parameters:
        index - position where instance is to be inserted
        instance - the instance to be inserted
        Returns:
        the instance previously at that position
      • sortBasedOnNominalAttribute

        protected void sortBasedOnNominalAttribute​(int attIndex)
        Sorts a nominal attribute (stable, linear-time sort). Instances are sorted based on the attribute label ordering specified in the header.
        Overrides:
        sortBasedOnNominalAttribute in class weka.core.Instances
        Parameters:
        attIndex - the attribute's index (index starts with 0)
        Throws:
        adams.core.exception.NotImplementedException
      • sort

        public void sort​(int attIndex)
        Sorts the instances based on an attribute. For numeric attributes, instances are sorted in ascending order. For nominal attributes, instances are sorted based on the attribute label ordering specified in the header. Instances with missing values for the attribute are placed at the end of the dataset.
        Overrides:
        sort in class weka.core.Instances
        Parameters:
        attIndex - the attribute's index (index starts with 0)
        Throws:
        adams.core.exception.NotImplementedException
      • stableSort

        public void stableSort​(int attIndex)
        Sorts the instances based on an attribute, using a stable sort. For numeric attributes, instances are sorted in ascending order. For nominal attributes, instances are sorted based on the attribute label ordering specified in the header. Instances with missing values for the attribute are placed at the end of the dataset.
        Overrides:
        stableSort in class weka.core.Instances
        Parameters:
        attIndex - the attribute's index (index starts with 0)
        Throws:
        adams.core.exception.NotImplementedException
      • attributeStats

        public weka.core.AttributeStats attributeStats​(int index)
        Calculates summary statistics on the values that appear in this set of instances for a specified attribute.
        Overrides:
        attributeStats in class weka.core.Instances
        Parameters:
        index - the index of the attribute to summarize (index starts with 0)
        Returns:
        an AttributeStats object with it's fields calculated.
      • stratStep

        protected void stratStep​(int numFolds)
        Help function needed for stratification of set.
        Overrides:
        stratStep in class weka.core.Instances
        Parameters:
        numFolds - the number of folds for the stratification
        Throws:
        adams.core.exception.NotImplementedException
      • setClassIndex

        public void setClassIndex​(int classIndex)
        Sets the class index of the set. If the class index is negative there is assumed to be no class. (ie. it is undefined)
        Overrides:
        setClassIndex in class weka.core.Instances
        Parameters:
        classIndex - the new class index (index starts with 0)
        Throws:
        IllegalArgumentException - if the class index is too big or < 0