Class Centroid

  • All Implemented Interfaces:
    FeatureVector<java.lang.Number>, NumberVector, SpatialComparable
    Direct Known Subclasses:
    ProjectedCentroid

    public class Centroid
    extends java.lang.Object
    implements NumberVector
    Class to compute the centroid of some data.

    This is a more numerically stable approach than simply taking the sum divided by the count repeatedly, and allows weighting.

    Since:
    0.4.0
    Author:
    Erich Schubert
    • Field Detail

      • wsum

        protected double wsum
        The current weight.
      • elements

        protected double[] elements
        Vector elements.
    • Constructor Detail

      • Centroid

        public Centroid​(int dim)
        Constructor.
        Parameters:
        dim - Dimensionality
    • Method Detail

      • put

        public void put​(double[] val)
        Add a single value with weight 1.0.
        Parameters:
        val - Value
      • put

        public void put​(double[] val,
                        double weight)
        Add data with a given weight.
        Parameters:
        val - data
        weight - weight
      • put

        public void put​(NumberVector val)
        Add a single value with weight 1.0.
        Parameters:
        val - Value
      • put

        public void put​(NumberVector val,
                        double weight)
        Add data with a given weight.
        Parameters:
        val - data
        weight - weight
      • doubleValue

        public double doubleValue​(int dimension)
        Description copied from interface: NumberVector
        Returns the value in the specified dimension as double.

        Note: this might seem redundant with respect to getValue(dim).doubleValue(), but usually this is much more efficient due to boxing/unboxing cost.

        Specified by:
        doubleValue in interface NumberVector
        Parameters:
        dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
        Returns:
        the value in the specified dimension
      • longValue

        public long longValue​(int dimension)
        Description copied from interface: NumberVector
        Returns the value in the specified dimension as long.

        Note: this might seem redundant with respect to getValue(dim).longValue(), but usually this is much more efficient due to boxing/unboxing cost.

        Specified by:
        longValue in interface NumberVector
        Parameters:
        dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
        Returns:
        the value in the specified dimension
      • getDimensionality

        public int getDimensionality()
        Description copied from interface: FeatureVector
        The dimensionality of the vector space where of this FeatureVector of V is an element.
        Specified by:
        getDimensionality in interface FeatureVector<java.lang.Number>
        Specified by:
        getDimensionality in interface SpatialComparable
        Returns:
        the number of dimensions of this FeatureVector of V
      • toArray

        public double[] toArray()
        Description copied from interface: NumberVector
        Returns a double array copy of this vector.
        Specified by:
        toArray in interface NumberVector
        Returns:
        Copy as double[]
      • make

        public static Centroid make​(Relation<? extends NumberVector> relation,
                                    DBIDs ids)
        Static constructor from an existing relation.
        Parameters:
        relation - Relation to use
        ids - IDs to use
        Returns:
        Centroid
      • getArrayRef

        public double[] getArrayRef()
        Low-level access to the element array.
        Returns:
        Array access