Class Distribution

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class Distribution
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable
    Class for handling a distribution of class values.
    Version:
    $Revision: 1.8.2.1 $
    Author:
    Eibe Frank (eibe@cs.waikato.ac.nz)
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      double[] m_perBag
      Weight of instances per bag.
      double[] m_perClass
      Weight of instances per class.
      double[][] m_perClassPerBag
      Weight of instances per class per bag.
      double[] m_sa  
    • Constructor Summary

      Constructors 
      Constructor Description
      Distribution​(double[][] table)
      Creates and initializes a new distribution using the given array.
      Distribution​(int numBags, int numClasses)
      Creates and initializes a new distribution.
      Distribution​(Distribution toMerge)
      Creates distribution with only one bag by merging all bags of given distribution.
      Distribution​(Distribution toMerge, int index)
      Creates distribution with two bags by merging all bags apart of the indicated one.
      Distribution​(Instances source)
      Creates a distribution with only one bag according to instances in source.
      Distribution​(Instances source, ClassifierSplitModel modelToUse)
      Creates a distribution according to given instances and split model.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int actualNumBags()
      Returns number of non-empty bags of distribution.
      int actualNumClasses()
      Returns number of classes actually occuring in distribution.
      int actualNumClasses​(int bagIndex)
      Returns number of classes actually occuring in given bag.
      void add​(int bagIndex, double[] counts)
      Adds counts to given bag.
      void add​(int bagIndex, Instance instance)
      Adds given instance to given bag.
      void addInstWithUnknown​(Instances source, int attIndex)
      Adds all instances with unknown values for given attribute, weighted according to frequency of instances in each bag.
      void addRange​(int bagIndex, Instances source, int startIndex, int lastPlusOne)
      Adds all instances in given range to given bag.
      void addWeights​(Instance instance, double[] weights)
      Adds given instance to all bags weighting it according to given weights.
      boolean check​(double minNoObj)
      Checks if at least two bags contain a minimum number of instances.
      java.lang.Object clone()
      Clones distribution (Deep copy of distribution).
      void del​(int bagIndex, Instance instance)
      Deletes given instance from given bag.
      void delRange​(int bagIndex, Instances source, int startIndex, int lastPlusOne)
      Deletes all instances in given range from given bag.
      java.lang.String dumpDistribution()
      Prints distribution.
      double[][] getDisc()  
      double getNumChanges()  
      void initialize()
      Sets all counts to zero.
      double laplaceProb​(int classIndex)
      Returns relative frequency of class over all bags with Laplace correction.
      double laplaceProb​(int classIndex, int intIndex)
      Returns relative frequency of class for given bag.
      double[][] matrix()
      Returns matrix with distribution of class values.
      int maxBag()
      Returns index of bag containing maximum number of instances.
      int maxClass()
      Returns class with highest frequency over all bags.
      int maxClass​(int index)
      Returns class with highest frequency for given bag.
      int numBags()
      Returns number of bags.
      int numClasses()
      Returns number of classes.
      double numCorrect()
      Returns perClass(maxClass()).
      double numCorrect​(int index)
      Returns perClassPerBag(index,maxClass(index)).
      double numIncorrect()
      Returns total-numCorrect().
      double numIncorrect​(int index)
      Returns perBag(index)-numCorrect(index).
      double perBag​(int bagIndex)
      Returns number of (possibly fractional) instances in given bag.
      double perClass​(int classIndex)
      Returns number of (possibly fractional) instances of given class.
      double perClassPerBag​(int bagIndex, int classIndex)
      Returns number of (possibly fractional) instances of given class in given bag.
      double prob​(int classIndex)
      Returns relative frequency of class over all bags.
      double prob​(int classIndex, int intIndex)
      Returns relative frequency of class for given bag.
      void shift​(int from, int to, Instance instance)
      Shifts given instance from one bag to another one.
      void shiftRange​(int from, int to, Instances source, int startIndex, int lastPlusOne)
      Shifts all instances in given range from one bag to another one.
      void sub​(int bagIndex, Instance instance)
      Subtracts given instance from given bag.
      Distribution subtract​(Distribution toSubstract)
      Subtracts the given distribution from this one.
      double total()
      Returns total number of (possibly fractional) instances.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_perClassPerBag

        public double[][] m_perClassPerBag
        Weight of instances per class per bag.
      • m_perBag

        public double[] m_perBag
        Weight of instances per bag.
      • m_perClass

        public double[] m_perClass
        Weight of instances per class.
      • m_sa

        public double[] m_sa
    • Constructor Detail

      • Distribution

        public Distribution​(int numBags,
                            int numClasses)
        Creates and initializes a new distribution.
      • Distribution

        public Distribution​(double[][] table)
        Creates and initializes a new distribution using the given array. WARNING: it just copies a reference to this array.
      • Distribution

        public Distribution​(Instances source)
                     throws java.lang.Exception
        Creates a distribution with only one bag according to instances in source.
        Throws:
        java.lang.Exception - if something goes wrong
      • Distribution

        public Distribution​(Instances source,
                            ClassifierSplitModel modelToUse)
                     throws java.lang.Exception
        Creates a distribution according to given instances and split model.
        Throws:
        java.lang.Exception - if something goes wrong
      • Distribution

        public Distribution​(Distribution toMerge)
        Creates distribution with only one bag by merging all bags of given distribution.
      • Distribution

        public Distribution​(Distribution toMerge,
                            int index)
        Creates distribution with two bags by merging all bags apart of the indicated one.
    • Method Detail

      • actualNumBags

        public final int actualNumBags()
        Returns number of non-empty bags of distribution.
      • actualNumClasses

        public final int actualNumClasses()
        Returns number of classes actually occuring in distribution.
      • actualNumClasses

        public final int actualNumClasses​(int bagIndex)
        Returns number of classes actually occuring in given bag.
      • add

        public final void add​(int bagIndex,
                              Instance instance)
                       throws java.lang.Exception
        Adds given instance to given bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • sub

        public final void sub​(int bagIndex,
                              Instance instance)
                       throws java.lang.Exception
        Subtracts given instance from given bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • add

        public final void add​(int bagIndex,
                              double[] counts)
        Adds counts to given bag.
      • addInstWithUnknown

        public final void addInstWithUnknown​(Instances source,
                                             int attIndex)
                                      throws java.lang.Exception
        Adds all instances with unknown values for given attribute, weighted according to frequency of instances in each bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • addRange

        public final void addRange​(int bagIndex,
                                   Instances source,
                                   int startIndex,
                                   int lastPlusOne)
                            throws java.lang.Exception
        Adds all instances in given range to given bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • addWeights

        public final void addWeights​(Instance instance,
                                     double[] weights)
                              throws java.lang.Exception
        Adds given instance to all bags weighting it according to given weights.
        Throws:
        java.lang.Exception - if something goes wrong
      • check

        public final boolean check​(double minNoObj)
        Checks if at least two bags contain a minimum number of instances.
      • clone

        public final java.lang.Object clone()
        Clones distribution (Deep copy of distribution).
        Overrides:
        clone in class java.lang.Object
      • del

        public final void del​(int bagIndex,
                              Instance instance)
                       throws java.lang.Exception
        Deletes given instance from given bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • delRange

        public final void delRange​(int bagIndex,
                                   Instances source,
                                   int startIndex,
                                   int lastPlusOne)
                            throws java.lang.Exception
        Deletes all instances in given range from given bag.
        Throws:
        java.lang.Exception - if something goes wrong
      • dumpDistribution

        public final java.lang.String dumpDistribution()
        Prints distribution.
      • initialize

        public final void initialize()
        Sets all counts to zero.
      • matrix

        public final double[][] matrix()
        Returns matrix with distribution of class values.
      • maxBag

        public final int maxBag()
        Returns index of bag containing maximum number of instances.
      • maxClass

        public final int maxClass()
        Returns class with highest frequency over all bags.
      • maxClass

        public final int maxClass​(int index)
        Returns class with highest frequency for given bag.
      • numBags

        public final int numBags()
        Returns number of bags.
      • numClasses

        public final int numClasses()
        Returns number of classes.
      • numCorrect

        public final double numCorrect()
        Returns perClass(maxClass()).
      • numCorrect

        public final double numCorrect​(int index)
        Returns perClassPerBag(index,maxClass(index)).
      • numIncorrect

        public final double numIncorrect()
        Returns total-numCorrect().
      • getDisc

        public final double[][] getDisc()
      • getNumChanges

        public final double getNumChanges()
      • numIncorrect

        public final double numIncorrect​(int index)
        Returns perBag(index)-numCorrect(index).
      • perClassPerBag

        public final double perClassPerBag​(int bagIndex,
                                           int classIndex)
        Returns number of (possibly fractional) instances of given class in given bag.
      • perBag

        public final double perBag​(int bagIndex)
        Returns number of (possibly fractional) instances in given bag.
      • perClass

        public final double perClass​(int classIndex)
        Returns number of (possibly fractional) instances of given class.
      • laplaceProb

        public final double laplaceProb​(int classIndex)
        Returns relative frequency of class over all bags with Laplace correction.
      • laplaceProb

        public final double laplaceProb​(int classIndex,
                                        int intIndex)
        Returns relative frequency of class for given bag.
      • prob

        public final double prob​(int classIndex)
        Returns relative frequency of class over all bags.
      • prob

        public final double prob​(int classIndex,
                                 int intIndex)
        Returns relative frequency of class for given bag.
      • subtract

        public final Distribution subtract​(Distribution toSubstract)
        Subtracts the given distribution from this one. The results has only one bag.
      • total

        public final double total()
        Returns total number of (possibly fractional) instances.
      • shift

        public final void shift​(int from,
                                int to,
                                Instance instance)
                         throws java.lang.Exception
        Shifts given instance from one bag to another one.
        Throws:
        java.lang.Exception - if something goes wrong
      • shiftRange

        public final void shiftRange​(int from,
                                     int to,
                                     Instances source,
                                     int startIndex,
                                     int lastPlusOne)
                              throws java.lang.Exception
        Shifts all instances in given range from one bag to another one.
        Throws:
        java.lang.Exception - if something goes wrong