示例#1
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of OPTICS algorithm.
        
        @remark Results of clustering can be obtained using corresponding gets methods.
        
        @see get_clusters()
        @see get_noise()
        @see get_ordering()
        
        """

        if (self.__ccore is True):
            (self.__clusters, self.__noise, self.__ordering,
             self.__eps) = wrapper.optics(self.__sample_pointer, self.__eps,
                                          self.__minpts,
                                          self.__amount_clusters)

        else:
            self.__kdtree = kdtree(self.__sample_pointer,
                                   range(len(self.__sample_pointer)))
            self.__allocate_clusters()

            if ((self.__amount_clusters is not None)
                    and (self.__amount_clusters != len(self.get_clusters()))):
                analyser = ordering_analyser(self.get_ordering())
                radius, _ = analyser.calculate_connvectivity_radius(
                    self.__amount_clusters)
                if (radius is not None):
                    self.__eps = radius
                    self.__allocate_clusters()
示例#2
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """

        (self.__clusters, self.__noise, self.__ordering, self.__eps,
         objects_indexes, objects_core_distances, objects_reachability_distances) = \
            wrapper.optics(self.__sample_pointer, self.__eps, self.__minpts, self.__amount_clusters, self.__data_type)

        self.__optics_objects = []
        for i in range(len(objects_indexes)):
            if objects_core_distances[i] < 0.0:
                objects_core_distances[i] = None

            if objects_reachability_distances[i] < 0.0:
                objects_reachability_distances[i] = None

            optics_object = optics_descriptor(objects_indexes[i], objects_core_distances[i], objects_reachability_distances[i])
            optics_object.processed = True

            self.__optics_objects.append(optics_object)
示例#3
0
    def fit(self, X):
        """Cluster analysis in line with rules of OPTICS algorithm.

        Results of clustering can be obtained using corresponding gets methods.
        """
        if not self.eps > 0.0:
            raise ValueError("eps must be positive.")

        if self.ccore:
            # use c implementation by pyclustering
            import pyclustering.core.optics_wrapper as wrapper
            (self.clusters_, self.noise_, self.ordering_, self.eps) = \
                wrapper.optics(X, self.eps, self.minpts,
                               self.n_clusters)
        else:
            # Performs cluster allocation and builds ordering diagram based on
            # reachability-distances.
            self.allocate_clusters(X)

            if self.n_clusters is not None and self.n_clusters != len(
                    self.clusters_):
                radius = calculate_connectivity_radius(
                    self.get_ordering(), self.n_clusters)
                if radius is not None:
                    self.eps = radius
                    self.allocate_clusters(X)

        labels = np.empty(X.shape[0], dtype=int)
        for i, cluster in enumerate(self.clusters_):
            cluster = np.array(cluster, dtype=int)
            labels[cluster] = i

        # noise do not belong to any cluster, so they belong to their own
        len_clusters = len(self.clusters_)
        for i, cluster in enumerate(self.noise_):
            labels[cluster] = -1 #i + len_clusters

        self.labels_ = labels
        return self
示例#4
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """

        (self.__clusters, self.__noise, self.__ordering, self.__eps,
         objects_indexes, objects_core_distances, objects_reachability_distances) = \
            wrapper.optics(self.__sample_pointer, self.__eps, self.__minpts, self.__amount_clusters, self.__data_type)

        self.__optics_objects = []
        for i in range(len(objects_indexes)):
            if objects_core_distances[i] < 0.0:
                objects_core_distances[i] = None

            if objects_reachability_distances[i] < 0.0:
                objects_reachability_distances[i] = None

            optics_object = optics_descriptor(objects_indexes[i], objects_core_distances[i], objects_reachability_distances[i])
            optics_object.processed = True

            self.__optics_objects.append(optics_object)