Beispiel #1
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medoids algorithm.

        @return (kmedoids) Returns itself (K-Medoids instance).

        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medoids()
        
        """
        
        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)
            self.__clusters, self.__medoid_indexes = wrapper.kmedoids(self.__pointer_data, self.__medoid_indexes, self.__tolerance, self.__itermax, ccore_metric.get_pointer(), self.__data_type)
        
        else:
            changes = float('inf')
            iterations = 0

            while changes > self.__tolerance and iterations < self.__itermax:
                self.__clusters = self.__update_clusters()
                update_medoid_indexes = self.__update_medoids()

                changes = max([self.__distance_calculator(self.__medoid_indexes[index], update_medoid_indexes[index]) for index in range(len(update_medoid_indexes))])

                self.__medoid_indexes = update_medoid_indexes

                iterations += 1

        return self
Beispiel #2
0
    def __initialize_by_ccore(self):
        """!
        @brief Performs processing using C/C++ pyclustering library.

        """
        ccore_metric = metric_wrapper.create_instance(self.__metric)
        return pam_build_wrapper.pam_build(self.__data, self.__amount, ccore_metric.get_pointer(), self.__data_type)
Beispiel #3
0
 def testBuildGowerDistanceFromMetricWithNumpyData(self):
     metric = distance_metric(type_metric.GOWER,
                              data=numpy.array([[-3.0, -3.0], [-4.0, -3.0],
                                                [-4.5, -3.0], [-5.0,
                                                               -3.0]]))
     ccore_metric = metric_wrapper.create_instance(metric)
     self.assertEqual(0.5, ccore_metric([-3.0, -3.0], [-5.0, -3.0]))
Beispiel #4
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medoids algorithm.
        
        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medoids()
        
        """
        
        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)

            self.__clusters = wrapper.kmedoids(self.__pointer_data, self.__medoid_indexes, self.__tolerance, ccore_metric.get_pointer(), self.__data_type)
            self.__medoid_indexes = self.__update_medoids()
        
        else:
            changes = float('inf')
             
            stop_condition = self.__tolerance
             
            while changes > stop_condition:
                self.__clusters = self.__update_clusters()
                update_medoid_indexes = self.__update_medoids()

                changes = max([self.__distance_calculator(self.__medoid_indexes[index], update_medoid_indexes[index]) for index in range(len(update_medoid_indexes))])

                self.__medoid_indexes = update_medoid_indexes
Beispiel #5
0
    def __process_by_ccore(self):
        """!
        @brief Performs processing using CCORE (C/C++ part of pyclustering library).

        """
        ccore_metric = metric_wrapper.create_instance(self.__metric)
        self.__score = wrapper.silhoeutte(self.__data, self.__clusters, ccore_metric.get_pointer(), self.__data_type)
Beispiel #6
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medians algorithm.
        
        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medians()
        
        """
        
        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)
            self.__clusters, self.__medians = wrapper.kmedians(self.__pointer_data, self.__medians, self.__tolerance, ccore_metric.get_pointer())

        else:
            changes = float('inf')
             
            # Check for dimension
            if len(self.__pointer_data[0]) != len(self.__medians[0]):
                raise NameError('Dimension of the input data and dimension of the initial medians must be equal.')
             
            while changes > self.__tolerance:
                self.__clusters = self.__update_clusters()
                updated_centers = self.__update_medians()
             
                changes = max([self.__metric(self.__medians[index], updated_centers[index]) for index in range(len(updated_centers))])
                 
                self.__medians = updated_centers
    def testBuildGowerDistanceFromMetricWithNumpyData(self):
        metric = distance_metric(type_metric.GOWER, data=numpy.array([[-3.0, -3.0], [-4.0, -3.0], [-4.5, -3.0], [-5.0, -3.0]]))
        ccore_metric = metric_wrapper.create_instance(metric)
        self.assertEqual(0.5, ccore_metric([-3.0, -3.0], [-5.0, -3.0]))


    # TODO: doesn't work for some platforms.
    #def testUserDefinedMetric(self):
    #    user_metric = lambda p1, p2 : p1[0] + p2[0];
    #    metric_instance = metric_wrapper(type_metric.USER_DEFINED, [], user_metric);
    #    assertion.eq(2.0, metric_instance([0.0, 0.0], [2.0, 0.0]));
    #    assertion.eq(4.0, metric_instance([3.0, 2.0], [1.0, 5.0]));
Beispiel #8
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """

        ccore_metric = metric_wrapper.create_instance(self.__metric)

        result = wrapper.xmeans(self.__pointer_data, self.__centers, self.__kmax, self.__tolerance, self.__criterion,
                                self.__alpha, self.__beta, self.__repeat, self.__random_state,
                                ccore_metric.get_pointer())

        self.__clusters = result[0]
        self.__centers = result[1]
        self.__total_wce = result[2][0]
Beispiel #9
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """
        ccore_metric = metric_wrapper.create_instance(self.__metric)

        results = wrapper.kmeans(self.__pointer_data, self.__centers, self.__tolerance, self.__itermax, (self.__observer is not None), ccore_metric.get_pointer())
        self.__clusters = results[0]
        self.__centers = results[1]

        if self.__observer is not None:
            self.__observer.set_evolution_clusters(results[2])
            self.__observer.set_evolution_centers(results[3])

        self.__total_wce = results[4][0]
Beispiel #10
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """
        ccore_metric = metric_wrapper.create_instance(self.__metric)

        results = wrapper.kmeans(self.__pointer_data, self.__centers, self.__tolerance, self.__itermax, (self.__observer is not None), ccore_metric.get_pointer())
        self.__clusters = results[0]
        self.__centers = results[1]

        if self.__observer is not None:
            self.__observer.set_evolution_clusters(results[2])
            self.__observer.set_evolution_centers(results[3])

        self.__total_wce = results[4][0]
Beispiel #11
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medoids algorithm.

        @return (kmedoids) Returns itself (K-Medoids instance).

        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medoids()
        
        """
        
        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)
            self.__clusters, self.__medoid_indexes, self.__iterations, self.__total_deviation = kmedoids_wrapper.kmedoids(self.__pointer_data, self.__medoid_indexes, self.__tolerance, self.__itermax, ccore_metric.get_pointer(), self.__data_type)

            # TODO: calculate it on C++ side as well
            for index_cluster in range(len(self.__clusters)):
                for index_point in self.__clusters[index_cluster]:
                    self.__labels[index_point] = index_cluster

        else:
            changes = float('inf')
            previous_deviation, self.__total_deviation = float('inf'), 0

            self.__iterations = 0

            if self.__itermax > 0:
                self.__total_deviation = self.__update_clusters()

            while (changes > self.__tolerance) and (self.__iterations < self.__itermax):
                self.__iterations += 1
                swap_cost = self.__swap_medoids()

                if swap_cost != float('inf'):
                    previous_deviation = self.__total_deviation
                    self.__total_deviation = self.__update_clusters()
                    changes = previous_deviation - self.__total_deviation
                else:
                    break



            self.__erase_empty_clusters()

        return self
Beispiel #12
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medoids algorithm.

        @return (kmedoids) Returns itself (K-Medoids instance).

        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medoids()
        
        """

        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)
            self.__clusters, self.__medoid_indexes = wrapper.kmedoids(
                self.__pointer_data, self.__medoid_indexes, self.__tolerance,
                self.__itermax, ccore_metric.get_pointer(), self.__data_type)

        else:
            changes = float('inf')
            previous_deviation, current_deviation = float('inf'), float('inf')

            iterations = 0

            if self.__itermax > 0:
                current_deviation = self.__update_clusters()

            while (changes > self.__tolerance) and (iterations <
                                                    self.__itermax):
                swap_cost = self.__swap_medoids()

                if swap_cost != float('inf'):
                    previous_deviation = current_deviation
                    current_deviation = self.__update_clusters()
                    changes = previous_deviation - current_deviation
                else:
                    break

                iterations += 1

            self.__erase_empty_clusters()

        return self
Beispiel #13
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of K-Medians algorithm.

        @return (kmedians) Returns itself (K-Medians instance).

        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        @see get_medians()
        
        """
        
        if self.__ccore is True:
            ccore_metric = metric_wrapper.create_instance(self.__metric)
            self.__clusters, self.__medians = wrapper.kmedians(self.__pointer_data, self.__medians, self.__tolerance, self.__itermax, ccore_metric.get_pointer())

        else:
            changes = float('inf')
             
            # Check for dimension
            if len(self.__pointer_data[0]) != len(self.__medians[0]):
                raise NameError('Dimension of the input data and dimension of the initial medians must be equal.')

            iterations = 0
            while changes > self.__tolerance and iterations < self.__itermax:
                self.__clusters = self.__update_clusters()
                updated_centers = self.__update_medians()
             
                changes = max([self.__metric(self.__medians[index], updated_centers[index]) for index in range(len(updated_centers))])
                 
                self.__medians = updated_centers

                iterations += 1

        return self
Beispiel #14
0
 def __process_by_ccore(self):
     ccore_metric = metric_wrapper.create_instance(self._metric);
     self._clusters, self._representatives = bsas_wrapper(self._data, self._amount, self._threshold, ccore_metric.get_pointer());
Beispiel #15
0
 def testBuildGowerDistanceFromMetricWithNumpyMaxRange(self):
     metric = distance_metric(type_metric.GOWER,
                              max_range=numpy.array([2.0, 0.0]))
     ccore_metric = metric_wrapper.create_instance(metric)
     self.assertEqual(0.5, ccore_metric([-3.0, -3.0], [-5.0, -3.0]))
Beispiel #16
0
 def testBuildGowerDistanceFromMetricWithMaxRange(self):
     metric = distance_metric(type_metric.GOWER, max_range=[2.0, 0.0])
     ccore_metric = metric_wrapper.create_instance(metric)
     assertion.eq(0.5, ccore_metric([-3.0, -3.0], [-5.0, -3.0]))
Beispiel #17
0
 def __process_by_ccore(self):
     ccore_metric = metric_wrapper.create_instance(self._metric)
     self._clusters, self._representatives = mbsas_wrapper(
         self._data, self._amount, self._threshold,
         ccore_metric.get_pointer())
Beispiel #18
0
 def testBuildGowerDistanceFromMetricWithData(self):
     metric = distance_metric(type_metric.GOWER,
                              data=[[-3.0, -3.0], [-4.0, -3.0],
                                    [-4.5, -3.0], [-5.0, -3.0]])
     ccore_metric = metric_wrapper.create_instance(metric)
     assertion.eq(0.5, ccore_metric([-3.0, -3.0], [-5.0, -3.0]))