Example #1
0
    def _nearest(self, point):
        """
        Returns the cluster nearest to point

        This method uses the distance method of each Cluster to compute the distance
        between point and the cluster centroid. It returns the Cluster that is closest.

        Ties are broken in favor of clusters occurring earlier self._clusters.

        Parameter point: The point to compare.
        Precondition: point is a list of numbers (int or float), with the same dimension
        as the dataset.
        """

        assert a6checks.is_point(point)
        assert len(point) == self._dataset.getDimension()

        list_of_clusters = self.getClusters()
        nearest_cluster = None
        nearest_distance = None

        for i in range(len(list_of_clusters)):
            if (i == 0):
                nearest_cluster = list_of_clusters[i]
                nearest_distance = list_of_clusters[i].distance(point)

            if (list_of_clusters[i].distance(point) < nearest_distance):
                nearest_cluster = list_of_clusters[i]
                nearest_distance = list_of_clusters[i].distance(point)
        return nearest_cluster
Example #2
0
    def distance(self, point):
        """
        Returns the euclidean distance from point to this cluster's centroid.

        Parameter point: The point to be measured
        Precondition: point is a list of numbers (int or float), with the same dimension
        as the centroid.
        """
        assert a6checks.is_point(point) and len(point)==len(self._centroid)
        return numpy.sqrt(sum([(b-a)*(b-a) for a,b in zip(point, self._centroid)]))
Example #3
0
    def addPoint(self, point):
        """
        Adds a COPY of point at the end of _contents.

        This method does not add the point directly. It adds a copy of the point.

        Precondition: point is a list of numbers (int or float),  len(point) = _dimension."""
        assert a6checks.is_point(point) and len(point) == self._dimension
        apoint = []
        for value in point:
            apoint.append(value)
        self._contents.append(apoint)
Example #4
0
    def addPoint(self,point):
        """
        Adds a COPY of point at the end of _contents.

        This method does not add the point directly. It adds a copy of the point.

        Precondition: point is a list of numbers (int or float),  len(point) = _dimension."""
        assert a6checks.is_point(point) == True
        assert len(point) == self._dimension
        pointcopy = []
        for x in range(0, len(point)):
            pointcopy.append(point[x])
        self._contents.append(pointcopy)
Example #5
0
    def addPoint(self, point):
        """
        Adds a COPY of point at the end of _contents.

        This method does not add the point directly. It adds a copy of the point.

        Precondition: point is a list of numbers (int or float),  len(point) = _dimension."""

        #Assert Precondition
        assert a6checks.is_point(point) == True

        copy_point = point[:]
        return self._contents.append(copy_point)
Example #6
0
    def _nearest(self, point):
        """
        Returns the cluster nearest to point

        Parameter point: The point to compare.
        Precondition: point is a list of numbers (int or float), with the same dimension
        as the dataset.
        """
        assert a6checks.is_point(point) == True
        assert len(point) == self._dataset.getDimension()
        mindist = self._clusters[0].distance(point)
        nearestcluster = self._clusters[0]
        for newclusterobject in self._clusters:
            if newclusterobject.distance(point) < mindist:
                mindist = newclusterobject.distance(point)
                nearestcluster = newclusterobject
        return nearestcluster
Example #7
0
    def distance(self, point):
        """
        Returns the euclidean distance from point to this cluster's centroid.

        Parameter point: The point to be measured
        Precondition: point is a list of numbers (int or float), with the same dimension
        as the centroid.
        """
        assert a6checks.is_point(point) == True
        assert len(point) == len(self._centroid)

        pointresult = 0
        pointdim = len(point)
        total = 0
        centroidind = self.getCentroid()
        for x in range(pointdim):
            pointresult = point[x] - centroidind[x]
            square = (pointresult)**2
            total = total + square
        root = math.sqrt(total)
        return root
Example #8
0
    def __init__(self, dset, centroid):
        """
        Initializes a new empty cluster whose centroid is a copy of <centroid>

        Parameter dset: the dataset
        Precondition: dset is an instance of Dataset

        Parameter centroid: the cluster centroid
        Precondition: centroid is a list of ds.getDimension() numbers
        """
        assert isinstance(dset, a6dataset.Dataset)
        self._dataset= dset
        self._indices= []
        result=[]
        for apoint in centroid:
            result.append(apoint)
        self._centroid= result
        if self._centroid != []:
            assert a6checks.is_point(centroid) and len(centroid)==self._dataset.getDimension()
        if self._indices != []:
            assert 0 <= min(self.indices) and max(self._indices) <= self._dataset.getSize()