Beispiel #1
0
    def __init__(self, data, amount_intervals, density_threshold, **kwargs):
        """!
        @brief Create CLIQUE clustering algorithm.

        @param[in] data (list): Input data (list of points) that should be clustered.
        @param[in] amount_intervals (uint): Amount of intervals in each dimension that defines amount of CLIQUE blocks
                    as \f[N_{blocks} = intervals^{dimensions}\f].
        @param[in] density_threshold (uint): Minimum number of points that should contain CLIQUE block to consider its
                    points as non-outliers.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'ccore').

        <b>Keyword Args:</b><br>
            - ccore (bool): By default is True. If True then C++ implementation is used for cluster analysis, otherwise
               Python implementation is used.

        """
        self.__data = data
        self.__amount_intervals = amount_intervals
        self.__density_threshold = density_threshold

        self.__ccore = kwargs.get('ccore', True)
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__clusters = []
        self.__noise = []

        self.__cells = []
        self.__cells_map = {}

        self.__validate_arguments()
    def __init__(self, sample, radius, conn_repr = conn_represent.MATRIX, initial_phases = initial_type.RANDOM_GAUSSIAN, enable_conn_weight = False, ccore = True):
        """!
        @brief Contructor of the oscillatory network SYNC for cluster analysis.
        
        @param[in] sample (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] radius (double): Connectivity radius between points, points should be connected if distance between them less then the radius.
        @param[in] conn_repr (conn_represent): Internal representation of connection in the network: matrix or list. Ignored in case of usage of CCORE library.
        @param[in] initial_phases (initial_type): Type of initialization of initial phases of oscillators (random, uniformly distributed, etc.).
        @param[in] enable_conn_weight (bool): If True - enable mode when strength between oscillators depends on distance between two oscillators.
              If False - all connection between oscillators have the same strength that equals to 1 (True).
        @param[in] ccore (bool): Defines should be CCORE C++ library used instead of Python code or not.
        
        """
        
        self._ccore_network_pointer = None;
        self._osc_loc = sample;
        self._num_osc = len(sample);
        
        if ( (ccore is True) and ccore_library.workable() ):
            self._ccore_network_pointer = syncnet_create_network(sample, radius, initial_phases, enable_conn_weight);
            
            # Default representation that is returned by CCORE is matrix.
            self._conn_represent = conn_represent.MATRIX;

        else:
            super().__init__(len(sample), 1, 0, conn_type.DYNAMIC, conn_repr, initial_phases, False);
            
            self._conn_weight = None;
            self._ena_conn_weight = enable_conn_weight;
            
            # Create connections.
            if (radius is not None):
                self._create_connections(radius);
    def __init__(self, data, number_clusters, link=None, ccore=True):
        """!
        @brief Constructor of agglomerative hierarchical algorithm.
        
        @param[in] data (list): Input data that is presented as a list of points (objects), each point should be represented by list, for example
                    [[0.1, 0.2], [0.4, 0.5], [1.3, 0.9]].
        @param[in] number_clusters (uint): Number of clusters that should be allocated.
        @param[in] link (type_link): Link type that is used for calculation similarity between objects and clusters, if it is not specified centroid link will be used by default.
        @param[in] ccore (bool): Defines should be CCORE (C++ pyclustering library) used instead of Python code or not (by default it is 'False').
        
        """

        self.__pointer_data = data
        self.__number_clusters = number_clusters
        self.__similarity = link

        self.__verify_arguments()

        if self.__similarity is None:
            self.__similarity = type_link.CENTROID_LINK

        self.__clusters = []
        self.__ccore = ccore
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        if self.__similarity == type_link.CENTROID_LINK:
            self.__centers = self.__pointer_data.copy(
            )  # used in case of usage of centroid links
Beispiel #4
0
    def __init__(self,
                 data,
                 amount_clusters,
                 epouch=100,
                 ccore=True,
                 **kwargs):
        """!
        @brief Creates SOM-SC (Self Organized Map for Simple Clustering) algorithm for clustering analysis.
        
        @param[in] data (list): List of points that are used for processing.
        @param[in] amount_clusters (uint): Amount of clusters that should be allocated.
        @param[in] epouch (uint): Number of epochs for training of SOM.
        @param[in] ccore (bool): If it is True then CCORE implementation will be used for clustering analysis.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: `random_state`).

        <b>Keyword Args:</b><br>
            - random_state (int): Seed for random state (by default is `None`, current system time is used).

        """

        self.__data_pointer = data
        self.__amount_clusters = amount_clusters
        self.__epouch = epouch
        self.__ccore = ccore
        self.__random_state = kwargs.get('random_state', None)

        self.__network = None

        if self.__ccore is True:
            self.__ccore = ccore_library.workable()

        self.__verify_arguments()
Beispiel #5
0
    def __init__(self,
                 data,
                 number_cluster,
                 number_represent_points=5,
                 compression=0.5,
                 ccore=True):
        """!
        @brief Constructor of clustering algorithm CURE.
        
        @param[in] data (array_like): Input data that should be processed.
        @param[in] number_cluster (uint): Number of clusters that should be allocated.
        @param[in] number_represent_points (uint): Number of representative points for each cluster.
        @param[in] compression (double): Coefficient defines level of shrinking of representation points toward the mean of the new created cluster after merging on each step. Usually it destributed from 0 to 1.
        @param[in] ccore (bool): If True then CCORE (C++ solution) will be used for solving.
        
        """

        self.__pointer_data = self.__prepare_data_points(data)

        self.__clusters = None
        self.__representors = None
        self.__means = None

        self.__number_cluster = number_cluster
        self.__number_represent_points = number_represent_points
        self.__compression = compression

        self.__ccore = ccore
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__validate_arguments()
Beispiel #6
0
    def __init__(self, data, eps, number_clusters, threshold=0.5, ccore=True):
        """!
        @brief Constructor of clustering algorithm ROCK.
        
        @param[in] data (list): Input data - list of points where each point is represented by list of coordinates.
        @param[in] eps (double): Connectivity radius (similarity threshold), points are neighbors if distance between them is less than connectivity radius.
        @param[in] number_clusters (uint): Defines number of clusters that should be allocated from the input data set.
        @param[in] threshold (double): Value that defines degree of normalization that influences on choice of clusters for merging during processing.
        @param[in] ccore (bool): Defines should be CCORE (C++ pyclustering library) used instead of Python code or not.
        
        """

        self.__pointer_data = data
        self.__eps = eps
        self.__number_clusters = number_clusters
        self.__threshold = threshold

        self.__clusters = None

        self.__ccore = ccore
        if (self.__ccore):
            self.__ccore = ccore_library.workable()

        self.__degree_normalization = 1.0 + 2.0 * ((1.0 - threshold) /
                                                   (1.0 + threshold))

        self.__adjacency_matrix = None
        self.__create_adjacency_matrix()
Beispiel #7
0
    def __init__(self, data, initial_index_medoids, tolerance=0.001, ccore=True, **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Medoids.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_index_medoids (list): Indexes of intial medoids (indexes of points in input data).
        @param[in] tolerance (double): Stop condition: if maximum value of distance change of medoids of clusters is less than tolerance than algorithm will stop processing.
        @param[in] ccore (bool): If specified than CCORE library (C++ pyclustering library) is used for clustering instead of Python code.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'data_type', 'itermax').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.
            - data_type (string): Data type of input sample 'data' that is processed by the algorithm ('points', 'distance_matrix').
            - itermax (uint): Maximum number of iteration for cluster analysis.

        """
        self.__pointer_data = data
        self.__clusters = []
        self.__medoid_indexes = initial_index_medoids
        self.__tolerance = tolerance

        self.__metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        self.__data_type = kwargs.get('data_type', 'points')
        self.__itermax = kwargs.get('itermax', 200)

        self.__distance_calculator = self.__create_distance_calculator()

        self.__ccore = ccore and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #8
0
    def __init__(self, sample, eps, minpts, amount_clusters=None, ccore=True):
        """!
        @brief Constructor of clustering algorithm OPTICS.
        
        @param[in] sample (list): Input data that is presented as a list of points (objects), where each point is represented by list or tuple.
        @param[in] eps (double): Connectivity radius between points, points may be connected if distance between them less than the radius.
        @param[in] minpts (uint): Minimum number of shared neighbors that is required for establishing links between points.
        @param[in] amount_clusters (uint): Optional parameter where amount of clusters that should be allocated is specified.
                    In case of usage 'amount_clusters' connectivity radius can be greater than real, in other words, there is place for mistake
                    in connectivity radius usage.
        @param[in] ccore (bool): if True than DLL CCORE (C++ solution) will be used for solving the problem.
        
        """

        self.__sample_pointer = sample
        # Algorithm parameter - pointer to sample for processing.
        self.__eps = eps
        # Algorithm parameter - connectivity radius between object for establish links between object.
        self.__minpts = minpts
        # Algorithm parameter - minimum number of neighbors that is required for establish links between object.
        self.__amount_clusters = amount_clusters

        self.__ordering = None
        self.__clusters = None
        self.__noise = None

        self.__kdtree = None
        self.__ccore = ccore

        if (self.__ccore):
            self.__ccore = ccore_library.workable()
    def __init__(self, data, clusters, **kwargs):
        """!
        @brief Initializes Silhouette method for analysis.

        @param[in] data (array_like): Input data that was used for cluster analysis.
        @param[in] clusters (list): Cluster that have been obtained after cluster analysis.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that was used for cluster analysis and should be used for Silhouette
               score calculation (by default Square Euclidean distance).
            - ccore (bool): If True then CCORE (C++ implementation of pyclustering library) is used (by default True).

        """
        self.__data = data
        self.__clusters = clusters
        self.__metric = kwargs.get(
            'metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))

        if self.__metric.get_type() != type_metric.USER_DEFINED:
            self.__metric.enable_numpy_usage()
        else:
            self.__metric.disable_numpy_usage()

        self.__score = [0.0] * len(data)

        self.__ccore = kwargs.get(
            'ccore',
            True) and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        if self.__ccore is False:
            self.__data = numpy.array(data)
Beispiel #10
0
    def __init__(self,
                 data,
                 number_cluster,
                 number_represent_points=5,
                 compression=0.5,
                 ccore=True):
        """!
        @brief Constructor of clustering algorithm CURE.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] number_cluster (uint): Number of clusters that should be allocated.
        @param[in] number_represent_points (uint): Number of representative points for each cluster.
        @param[in] compression (double): Coefficient defines level of shrinking of representation points toward the mean of the new created cluster after merging on each step. Usually it destributed from 0 to 1.
        @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
        
        """

        self.__pointer_data = data

        self.__clusters = None
        self.__representors = None
        self.__means = None

        self.__number_cluster = number_cluster
        self.__number_represent_points = number_represent_points
        self.__compression = compression

        self.__ccore = ccore
        if (self.__ccore):
            self.__ccore = ccore_library.workable()

        if (self.__ccore is False):
            self.__create_queue()
            # queue
            self.__create_kdtree()
Beispiel #11
0
    def __init__(self,
                 num_osc,
                 increase_strength1,
                 increase_strength2,
                 ccore=True):
        """!
        @brief Constructor of oscillatory network for pattern recognition based on Kuramoto model.
        
        @param[in] num_osc (uint): Number of oscillators in the network.
        @param[in] increase_strength1 (double): Parameter for increasing strength of the second term of the Fourier component.
        @param[in] increase_strength2 (double): Parameter for increasing strength of the third term of the Fourier component.
        @param[in] ccore (bool): If True simulation is performed by CCORE library (C++ implementation of pyclustering).
        
        """

        if ((ccore is True) and ccore_library.workable()):
            self._ccore_network_pointer = wrapper.syncpr_create(
                num_osc, increase_strength1, increase_strength2)

        else:
            self._increase_strength1 = increase_strength1
            self._increase_strength2 = increase_strength2
            self._coupling = [[0.0 for i in range(num_osc)]
                              for j in range(num_osc)]

            super().__init__(num_osc, 1, 0, conn_type.ALL_TO_ALL,
                             conn_represent.MATRIX,
                             initial_type.RANDOM_GAUSSIAN, ccore)
Beispiel #12
0
    def __init__(self, data, amount_intervals, density_threshold, **kwargs):
        """!
        @brief Create CLIQUE clustering algorithm.

        @param[in] data (list): Input data (list of points) that should be clustered.
        @param[in] amount_intervals (uint): Amount of intervals in each dimension that defines amount of CLIQUE block
                    as \f[N_{blocks} = intervals^{dimensions}\f].
        @param[in] density_threshold (uint): Minimum number of points that should contain CLIQUE block to consider its
                    points as non-outliers.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'ccore').

        <b>Keyword Args:</b><br>
            - ccore (bool): By default is True. If True then C++ implementation is used for cluster analysis, otherwise
               Python implementation is used.

        """
        self.__data = data
        self.__amount_intervals = amount_intervals
        self.__density_threshold = density_threshold

        self.__ccore = kwargs.get('ccore', True)
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__clusters = []
        self.__noise = []

        self.__cells = []
        self.__cells_map = {}

        self.__validate_arguments()
Beispiel #13
0
    def __init__(self, data, initial_centers, **kwargs):
        """!
        @brief Initialize Fuzzy C-Means algorithm.

        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] initial_centers (array_like): Initial coordinates of centers of clusters that are represented by array_like data structure: [center1, center2, ...].
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'tolerance', 'itermax', 'm').

        <b>Keyword Args:</b><br>
            - ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
            - tolerance (float): Stop condition: if maximum value of change of centers of clusters is less than tolerance then algorithm stops processing.
            - itermax (uint): Maximum number of iterations that is used for clustering process (by default: 200).
            - m (float): Hyper-parameter that controls how fuzzy the cluster will be. The higher it is, the fuzzier the cluster will be in the end.
               This parameter should be greater than 1 (by default: 2).

        """

        self.__data = data
        self.__clusters = []
        self.__centers = initial_centers
        self.__membership = []

        self.__tolerance = kwargs.get('tolerance', 0.001)
        self.__itermax = kwargs.get('itermax', 200)
        self.__m = kwargs.get('m', 2)

        self.__degree = 2.0 / (self.__m - 1)

        self.__ccore = kwargs.get('ccore', True)
        if self.__ccore is True:
            self.__ccore = ccore_library.workable()
Beispiel #14
0
    def __init__(self,
                 data,
                 initial_centers=None,
                 kmax=20,
                 tolerance=0.001,
                 criterion=splitting_type.BAYESIAN_INFORMATION_CRITERION,
                 ccore=True,
                 **kwargs):
        """!
        @brief Constructor of clustering algorithm X-Means.
        
        @param[in] data (array_like): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_centers (list): Initial coordinates of centers of clusters that are represented by list: `[center1, center2, ...]`,
                    if it is not specified then X-Means starts from the random center.
        @param[in] kmax (uint): Maximum number of clusters that can be allocated.
        @param[in] tolerance (double): Stop condition for each iteration: if maximum value of change of centers of clusters is less than tolerance than algorithm will stop processing.
        @param[in] criterion (splitting_type): Type of splitting creation (by default `splitting_type.BAYESIAN_INFORMATION_CRITERION`).
        @param[in] ccore (bool): Defines if C++ pyclustering library should be used instead of Python implementation.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: `repeat`, `random_state`, `metric`, `alpha`, `beta`).

        <b>Keyword Args:</b><br>
            - repeat (unit): How many times K-Means should be run to improve parameters (by default is `1`).
               With larger `repeat` values suggesting higher probability of finding global optimum.
            - random_state (int): Seed for random state (by default is `None`, current system time is used).
            - metric (distance_metric): Metric that is used for distance calculation between two points (by default
               euclidean square distance).
            - alpha (double): Parameter distributed [0.0, 1.0] for alpha probabilistic bound \f$Q\left(\alpha\right)\f$.
               The parameter is used only in case of MNDL splitting criterion, in all other cases this value is ignored.
            - beta (double): Parameter distributed [0.0, 1.0] for beta probabilistic bound \f$Q\left(\beta\right)\f$.
               The parameter is used only in case of MNDL splitting criterion, in all other cases this value is ignored.

        """

        self.__pointer_data = numpy.array(data)
        self.__clusters = []
        self.__random_state = kwargs.get('random_state', None)
        self.__metric = copy.copy(
            kwargs.get('metric',
                       distance_metric(type_metric.EUCLIDEAN_SQUARE)))

        if initial_centers is not None:
            self.__centers = numpy.array(initial_centers)
        else:
            self.__centers = kmeans_plusplus_initializer(
                data, 2, random_state=self.__random_state).initialize()

        self.__kmax = kmax
        self.__tolerance = tolerance
        self.__criterion = criterion
        self.__total_wce = 0.0
        self.__repeat = kwargs.get('repeat', 1)
        self.__alpha = kwargs.get('alpha', 0.9)
        self.__beta = kwargs.get('beta', 0.9)

        self.__ccore = ccore and self.__metric.get_type(
        ) != type_metric.USER_DEFINED
        if self.__ccore is True:
            self.__ccore = ccore_library.workable()

        self.__verify_arguments()
Beispiel #15
0
    def __init__(self, data, eps, neighbors, ccore=True):
        """!
        @brief Constructor of clustering algorithm DBSCAN.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] eps (double): Connectivity radius between points, points may be connected if distance between them less then the radius.
        @param[in] neighbors (uint): minimum number of shared neighbors that is required for establish links between points.
        @param[in] ccore (bool): if True than DLL CCORE (C++ solution) will be used for solving the problem.
        
        """

        self.__pointer_data = data
        self.__kdtree = None
        self.__eps = eps
        self.__sqrt_eps = eps * eps
        self.__neighbors = neighbors

        self.__visited = [False] * len(self.__pointer_data)
        self.__belong = [False] * len(self.__pointer_data)

        self.__clusters = []
        self.__noise = []

        self.__ccore = ccore
        if (self.__ccore):
            self.__ccore = ccore_library.workable()
Beispiel #16
0
    def __init__(self, data, eps, neighbors, ccore=True, **kwargs):
        """!
        @brief Constructor of clustering algorithm DBSCAN.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] eps (double): Connectivity radius between points, points may be connected if distance between them less then the radius.
        @param[in] neighbors (uint): minimum number of shared neighbors that is required for establish links between points.
        @param[in] ccore (bool): if True than DLL CCORE (C++ solution) will be used for solving the problem.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'data_type').

        <b>Keyword Args:</b><br>
            - data_type (string): Data type of input sample 'data' that is processed by the algorithm ('points', 'distance_matrix').
        
        """

        self.__pointer_data = data
        self.__kdtree = None
        self.__eps = eps
        self.__sqrt_eps = eps * eps
        self.__neighbors = neighbors

        self.__visited = [False] * len(self.__pointer_data)
        self.__belong = [False] * len(self.__pointer_data)

        self.__data_type = kwargs.get('data_type', 'points')

        self.__clusters = []
        self.__noise = []

        self.__neighbor_searcher = self.__create_neighbor_searcher(
            self.__data_type)

        self.__ccore = ccore
        if self.__ccore:
            self.__ccore = ccore_library.workable()
    def __init__(self,
                 data,
                 initial_index_medoids,
                 tolerance=0.001,
                 ccore=True,
                 **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Medoids.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_index_medoids (list): Indexes of intial medoids (indexes of points in input data).
        @param[in] tolerance (double): Stop condition: if maximum value of distance change of medoids of clusters is less than tolerance than algorithm will stop processing.
        @param[in] ccore (bool): If specified than CCORE library (C++ pyclustering library) is used for clustering instead of Python code.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'data_type').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.
            - data_type (string): Data type of input sample 'data' that is processed by the algorithm ('points', 'distance_matrix').

        """
        self.__pointer_data = data
        self.__clusters = []
        self.__medoid_indexes = initial_index_medoids
        self.__tolerance = tolerance

        self.__metric = kwargs.get(
            'metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        self.__data_type = kwargs.get('data_type', 'points')
        self.__distance_calculator = self.__create_distance_calculator()

        self.__ccore = ccore and self.__metric.get_type(
        ) != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #18
0
    def __init__(self, data, maximum_clusters, threshold, ccore=True, **kwargs):
        """!
        @brief Creates classical BSAS algorithm.

        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] maximum_clusters: Maximum allowable number of clusters that can be allocated during processing.
        @param[in] threshold: Threshold of dissimilarity (maximum distance) between points.
        @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric').

        Keyword Args:
            metric (distance_metric): Metric that is used for distance calculation between two points.

        """

        self._data = data;
        self._amount = maximum_clusters;
        self._threshold = threshold;
        self._metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN));
        self._ccore = ccore and self._metric.get_type() != type_metric.USER_DEFINED;

        self._clusters = [];
        self._representatives = [];

        if self._ccore is True:
            self._ccore = ccore_library.workable();
Beispiel #19
0
    def __init__(self, data, initial_centers, tolerance=0.001, ccore=True, **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Medians.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_centers (list): Initial coordinates of medians of clusters that are represented by list: [center1, center2, ...].
        @param[in] tolerance (double): Stop condition: if maximum value of change of centers of clusters is less than tolerance than algorithm will stop processing
        @param[in] ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.
        
        """
        self.__pointer_data = data
        self.__clusters = []
        self.__medians = initial_centers[:]
        self.__tolerance = tolerance

        self.__metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        if self.__metric is None:
            self.__metric = distance_metric(type_metric.EUCLIDEAN_SQUARE)

        self.__ccore = ccore and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #20
0
    def __init__(self, data, eps, neighbors, ccore = True, **kwargs):
        """!
        @brief Constructor of clustering algorithm DBSCAN.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] eps (double): Connectivity radius between points, points may be connected if distance between them less then the radius.
        @param[in] neighbors (uint): minimum number of shared neighbors that is required for establish links between points.
        @param[in] ccore (bool): if True than DLL CCORE (C++ solution) will be used for solving the problem.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'data_type').

        <b>Keyword Args:</b><br>
            - data_type (string): Data type of input sample 'data' that is processed by the algorithm ('points', 'distance_matrix').
        
        """
        
        self.__pointer_data = data
        self.__kdtree = None
        self.__eps = eps
        self.__sqrt_eps = eps * eps
        self.__neighbors = neighbors

        self.__visited = [False] * len(self.__pointer_data)
        self.__belong = [False] * len(self.__pointer_data)

        self.__data_type = kwargs.get('data_type', 'points')

        self.__clusters = []
        self.__noise = []

        self.__neighbor_searcher = self.__create_neighbor_searcher(self.__data_type)

        self.__ccore = ccore
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #21
0
    def __init__(self,
                 data,
                 initial_centers,
                 tolerance=0.001,
                 ccore=True,
                 **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Means.
        @details Center initializer can be used for creating initial centers, for example, K-Means++ method.
        
        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] initial_centers (array_like): Initial coordinates of centers of clusters that are represented by array_like data structure: [center1, center2, ...].
        @param[in] tolerance (double): Stop condition: if maximum value of change of centers of clusters is less than tolerance then algorithm stops processing.
        @param[in] ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'observer').
        
        <b>Keyword Args:</b><br>
            - observer (kmeans_observer): Observer of the algorithm to collect information about clustering process on each iteration.
        
        @see center_initializer
        
        """
        self.__pointer_data = numpy.matrix(data)
        self.__clusters = []
        self.__centers = numpy.matrix(initial_centers)
        self.__tolerance = tolerance

        self.__observer = None
        if 'observer' in kwargs:
            self.__observer = kwargs['observer']

        self.__ccore = ccore
        if self.__ccore is True:
            self.__ccore = ccore_library.workable()
Beispiel #22
0
    def __init__(self, data, k_init=1, ccore=True, **kwargs):
        """!
        @brief Initializes G-Means algorithm.

        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be
                    represented by array_like data structure.
        @param[in] k_init (uint): Initial amount of centers (by default started search from 1).
        @param[in] ccore (bool): Defines whether CCORE library (C/C++ part of the library) should be used instead of
                    Python code.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'tolerance', 'repeat').

        <b>Keyword Args:</b><br>
            - tolerance (double): tolerance (double): Stop condition for each K-Means iteration: if maximum value of
               change of centers of clusters is less than tolerance than algorithm will stop processing.
            - repeat (unit): How many times K-Means should be run to improve parameters (by default is 3).
               With larger 'repeat' values suggesting higher probability of finding global optimum.

        """
        self.__data = data
        self.__k_init = k_init

        self.__clusters = []
        self.__centers = []
        self.__total_wce = 0.0
        self.__ccore = ccore

        self.__tolerance = kwargs.get('tolerance', 0.025)
        self.__repeat = kwargs.get('repeat', 3)

        if self.__ccore is True:
            self.__ccore = ccore_library.workable()

        self._verify_arguments()
Beispiel #23
0
 def __init__(self, data, number_clusters, link = None, ccore = True):
     """!
     @brief Constructor of agglomerative hierarchical algorithm.
     
     @param[in] data (list): Input data that is presented as a list of points (objects), each point should be represented by list, for example
                 [[0.1, 0.2], [0.4, 0.5], [1.3, 0.9]].
     @param[in] number_clusters (uint): Number of clusters that should be allocated.
     @param[in] link (type_link): Link type that is used for calculation similarity between objects and clusters, if it is not specified centroid link will be used by default.
     @param[in] ccore (bool): Defines should be CCORE (C++ pyclustering library) used instead of Python code or not (by default it is 'False').
     
     """  
     
     self.__pointer_data = data
     self.__number_clusters = number_clusters
     self.__similarity = link
     
     if self.__similarity is None:
         self.__similarity = type_link.CENTROID_LINK
     
     self.__clusters = []
     self.__ccore = ccore
     if (self.__ccore):
         self.__ccore = ccore_library.workable()
     
     if (self.__similarity == type_link.CENTROID_LINK):
         self.__centers = self.__pointer_data.copy()    # used in case of usage of centroid links
Beispiel #24
0
    def __init__(self, source_data, number_clusters, osc_initial_phases = initial_type.RANDOM_GAUSSIAN, initial_neighbors = 3, increase_persent = 0.15, ccore = True):
        """!
        @brief Costructor of the oscillatory network hSyncNet for cluster analysis.

        @param[in] source_data (list): Input data set defines structure of the network.
        @param[in] number_clusters (uint): Number of clusters that should be allocated.
        @param[in] osc_initial_phases (initial_type): Type of initialization of initial values of phases of oscillators.
        @param[in] initial_neighbors (uint): Defines initial radius connectivity by calculation average distance to connect specify number of oscillators.
        @param[in] increase_persent (double): Percent of increasing of radius connectivity on each step (input values in range (0.0; 1.0) correspond to (0%; 100%)).
        @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
        
        """
        
        self.__ccore_network_pointer = None;
        
        if (initial_neighbors >= len(source_data)):
            initial_neighbors = len(source_data) - 1;
        
        if ( (ccore is True) and ccore_library.workable() ):
            self.__ccore_network_pointer = wrapper.hsyncnet_create_network(source_data, number_clusters, osc_initial_phases, initial_neighbors, increase_persent);
        else: 
            super().__init__(source_data, 0, initial_phases = osc_initial_phases, ccore=False);
            
            self.__initial_neighbors = initial_neighbors;
            self.__increase_persent = increase_persent;
            self._number_clusters = number_clusters;
Beispiel #25
0
    def __init__(self, data, maximum_clusters, threshold, ccore=True, **kwargs):
        """!
        @brief Creates classical BSAS algorithm.

        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] maximum_clusters: Maximum allowable number of clusters that can be allocated during processing.
        @param[in] threshold: Threshold of dissimilarity (maximum distance) between points.
        @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.

        """

        self._data = data;
        self._amount = maximum_clusters;
        self._threshold = threshold;
        self._metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN));
        self._ccore = ccore and self._metric.get_type() != type_metric.USER_DEFINED;

        self._clusters = [];
        self._representatives = [];

        if self._ccore is True:
            self._ccore = ccore_library.workable();
Beispiel #26
0
    def __init__(self,
                 data,
                 initial_index_medoids,
                 tolerance=0.25,
                 ccore=True):
        """!
        @brief Constructor of clustering algorithm K-Medoids.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_index_medoids (list): Indexes of intial medoids (indexes of points in input data).
        @param[in] tolerance (double): Stop condition: if maximum value of distance change of medoids of clusters is less than tolerance than algorithm will stop processing.
        @param[in] ccore (bool): If specified than CCORE library (C++ pyclustering library) is used for clustering instead of Python code.
        
        """
        self.__pointer_data = data
        self.__clusters = []
        self.__medoids = [
            data[medoid_index] for medoid_index in initial_index_medoids
        ]
        self.__medoid_indexes = initial_index_medoids
        self.__tolerance = tolerance
        self.__ccore = ccore

        if (self.__ccore):
            self.__ccore = ccore_library.workable()
Beispiel #27
0
    def __init__(self, data, kmin, kmax, **kwargs):
        """!
        @brief Initialize Silhouette search algorithm to find out optimal amount of clusters.

        @param[in] data (array_like): Input data that is used for searching optimal amount of clusters.
        @param[in] kmin (uint): Amount of clusters from which search is performed. Should be equal or greater than 2.
        @param[in] kmax (uint): Amount of clusters to which search is performed. Should be equal or less than amount of
                    points in input data.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'algorithm').

        <b>Keyword Args:</b><br>
            - algorithm (silhouette_ksearch_type): Defines algorithm that is used for searching optimal number of
               clusters (by default K-Means).
            - ccore (bool): If True then CCORE (C++ implementation of pyclustering library) is used (by default True).

        """
        self.__data = data
        self.__kmin = kmin
        self.__kmax = kmax

        self.__algorithm = kwargs.get('algorithm',
                                      silhouette_ksearch_type.KMEANS)
        self.__return_index = self.__algorithm == silhouette_ksearch_type.KMEDOIDS

        self.__amount = -1
        self.__score = -1.0
        self.__scores = {}

        self.__verify_arguments()

        self.__ccore = kwargs.get('ccore', True)
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #28
0
    def __init__(self, data, initial_centers, tolerance=0.001, ccore=True, **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Medians.
        
        @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
        @param[in] initial_centers (list): Initial coordinates of medians of clusters that are represented by list: [center1, center2, ...].
        @param[in] tolerance (double): Stop condition: if maximum value of change of centers of clusters is less than tolerance than algorithm will stop processing
        @param[in] ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'itermax').

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.
            - itermax (uint): Maximum number of iterations for cluster analysis.
        
        """
        self.__pointer_data = data
        self.__clusters = []
        self.__medians = initial_centers[:]
        self.__tolerance = tolerance

        self.__itermax = kwargs.get('itermax', 100)
        self.__metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        if self.__metric is None:
            self.__metric = distance_metric(type_metric.EUCLIDEAN_SQUARE)

        self.__ccore = ccore and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #29
0
    def __init__(self, data, k, ccore=True, **kwargs):
        """!
        @brief Create PAM BUILD algorithm to initialize medoids.

        @param[in] data (array-like): Points where each point is represented by a list of coordinates.
        @param[in] k (uint): The amount of medoids to generate.
        @param[in] ccore (bool): If `True` then C++ implementation is used instead of Python code (by default is `True`).
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: `data_type`, `metric`).

        <b>Keyword Args:</b><br>
            - metric (distance_metric): Metric that is used for distance calculation between two points.
            - data_type (string): Data type of input sample `data` (`points`, `distance_matrix`).

        """
        self.__data = data
        self.__amount = k

        self.__metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        self.__data_type = kwargs.get('data_type', 'points')

        self.__distance_calculator = self.__create_distance_calculator()
        self.__ccore = ccore and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__verify_arguments()
Beispiel #30
0
    def __init__(self, data, initial_centers, **kwargs):
        """!
        @brief Initialize Fuzzy C-Means algorithm.

        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] initial_centers (array_like): Initial coordinates of centers of clusters that are represented by array_like data structure: [center1, center2, ...].
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'tolerance', 'itermax', 'm').

        <b>Keyword Args:</b><br>
            - ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
            - tolerance (float): Stop condition: if maximum value of change of centers of clusters is less than tolerance then algorithm stops processing.
            - itermax (uint): Maximum number of iterations that is used for clustering process (by default: 200).
            - m (float): Hyper-parameter that controls how fuzzy the cluster will be. The higher it is, the fuzzier the cluster will be in the end.
               This parameter should be greater than 1 (by default: 2).

        """

        self.__data = data
        self.__clusters = []
        self.__centers = initial_centers
        self.__membership = []

        self.__tolerance = kwargs.get('tolerance', 0.001)
        self.__itermax = kwargs.get('itermax', 200)
        self.__m = kwargs.get('m', 2)

        self.__degree = 2.0 / (self.__m - 1)

        self.__ccore = kwargs.get('ccore', True)
        if self.__ccore is True:
            self.__ccore = ccore_library.workable()
Beispiel #31
0
 def __init__(self, data, initial_centers = None, kmax = 20, tolerance = 0.025, criterion = splitting_type.BAYESIAN_INFORMATION_CRITERION, ccore = True):
     """!
     @brief Constructor of clustering algorithm X-Means.
     
     @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
     @param[in] initial_centers (list): Initial coordinates of centers of clusters that are represented by list: [center1, center2, ...], 
                 if it is not specified then X-Means starts from the random center.
     @param[in] kmax (uint): Maximum number of clusters that can be allocated.
     @param[in] tolerance (double): Stop condition for each iteration: if maximum value of change of centers of clusters is less than tolerance than algorithm will stop processing.
     @param[in] criterion (splitting_type): Type of splitting creation.
     @param[in] ccore (bool): Defines should be CCORE (C++ pyclustering library) used instead of Python code or not.
     
     """
     
     self.__pointer_data = data
     self.__clusters = []
     
     if initial_centers is not None:
         self.__centers = initial_centers[:]
     else:
         self.__centers = [ [random.random() for _ in range(len(data[0])) ] ]
     
     self.__kmax = kmax
     self.__tolerance = tolerance
     self.__criterion = criterion
      
     self.__ccore = ccore
     if self.__ccore:
         self.__ccore = ccore_library.workable()
Beispiel #32
0
    def __init__(self, data, kmin, kmax, **kwargs):
        """!
        @brief Construct Elbow method.

        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] kmin (int): Minimum amount of clusters that should be considered.
        @param[in] kmax (int): Maximum amount of clusters that should be considered.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'ccore', 'initializer').

        <b>Keyword Args:</b><br>
            - ccore (bool): If True then CCORE (C++ implementation of pyclustering library) is used (by default True).
            - initializer (callable): Center initializer that is used by K-Means algorithm (by default K-Means++).

        """

        self.__initializer = kwargs.get('initializer',
                                        kmeans_plusplus_initializer)

        self.__ccore = kwargs.get('ccore', True) or \
                       isinstance(self.__initializer, kmeans_plusplus_initializer) or \
                       isinstance(self.__initializer, random_center_initializer)

        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__data = data
        self.__kmin = kmin
        self.__kmax = kmax

        self.__wce = []
        self.__elbows = []
        self.__kvalue = -1

        self.__verify_arguments()
Beispiel #33
0
    def __init__(self, rows, cols, conn_type=type_conn.grid_eight, parameters=None, ccore=True):
        """!
        @brief Constructor of self-organized map.
        
        @param[in] rows (uint): Number of neurons in the column (number of rows).
        @param[in] cols (uint): Number of neurons in the row (number of columns).
        @param[in] conn_type (type_conn): Type of connection between oscillators in the network (grid four, grid eight, honeycomb, function neighbour).
        @param[in] parameters (som_parameters): Other specific parameters.
        @param[in] ccore (bool): If True simulation is performed by CCORE library (C++ implementation of pyclustering).

        """

        # some of these parameters are required despite core implementation, for example, for network visualization.
        self._cols = cols

        self._rows = rows

        self._size = cols * rows

        self._conn_type = conn_type

        self._data = None

        self._neighbors = None

        self._local_radius = 0.0

        self._learn_rate = 0.0

        self.__ccore_som_pointer = None

        self._params = parameters or som_parameters()

        if self._params.init_radius is None:
            self._params.init_radius = self.__initialize_initial_radius(rows, cols)

        if (ccore is True) and ccore_library.workable():
            self.__ccore_som_pointer = wrapper.som_create(rows, cols, conn_type, self._params)

        else:
            # location
            self._location = self.__initialize_locations(rows, cols)

            # default weights
            self._weights = [[0.0]] * self._size

            # awards
            self._award = [0] * self._size

            # captured objects
            self._capture_objects = [[] for i in range(self._size)]

            # distances - calculate and store them only during training
            self._sqrt_distances = None

            # connections
            if conn_type != type_conn.func_neighbor:
                self._create_connections(conn_type)
Beispiel #34
0
    def __init__(self,
                 num_osc,
                 stimulus=None,
                 parameters=None,
                 type_conn=None,
                 type_conn_represent=conn_represent.MATRIX,
                 ccore=True):
        """!
        @brief Constructor of oscillatory network based on Hodgkin-Huxley neuron model.
        
        @param[in] num_osc (uint): Number of peripheral oscillators in the network.
        @param[in] stimulus (list): List of stimulus for oscillators, number of stimulus should be equal to number of peripheral oscillators.
        @param[in] parameters (hhn_parameters): Parameters of the network.
        @param[in] type_conn (conn_type): Type of connections between oscillators in the network (ignored for this type of network).
        @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
        @param[in] ccore (bool): If 'True' then CCORE is used (C/C++ implementation of the model).
        
        """

        super().__init__(num_osc, conn_type.NONE, type_conn_represent)

        if (stimulus is None):
            self._stimulus = [0.0] * num_osc
        else:
            self._stimulus = stimulus

        if (parameters is not None):
            self._params = parameters
        else:
            self._params = hhn_parameters()

        self.__ccore_hhn_pointer = None
        self.__ccore_hhn_dynamic_pointer = None

        if ((ccore is True) and ccore_library.workable()):
            self.__ccore_hhn_pointer = wrapper.hhn_create(
                num_osc, self._params)
        else:
            self._membrane_dynamic_pointer = None
            # final result is stored here.

            self._membrane_potential = [0.0] * self._num_osc
            self._active_cond_sodium = [0.0] * self._num_osc
            self._inactive_cond_sodium = [0.0] * self._num_osc
            self._active_cond_potassium = [0.0] * self._num_osc
            self._link_activation_time = [0.0] * self._num_osc
            self._link_pulse_counter = [0.0] * self._num_osc
            self._link_deactivation_time = [0.0] * self._num_osc
            self._link_weight3 = [0.0] * self._num_osc
            self._pulse_generation_time = [[] for i in range(self._num_osc)]
            self._pulse_generation = [False] * self._num_osc

            self._noise = [
                random.random() * 2.0 - 1.0 for i in range(self._num_osc)
            ]

            self._central_element = [central_element(),
                                     central_element()]
Beispiel #35
0
    def __setstate__(self, som_state):
        """
        @brief Set state of SOM network that can be used to load network.

        """
        if som_state['ccore'] is True and ccore_library.workable():
            self.__upload_dump_to_ccore(som_state['state'])
        else:
            self.__upload_dump_to_python(som_state['state'])
 def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True):
     """!
     @brief Constructor of oscillatory network is based on Kuramoto model.
     
     @param[in] num_osc (uint): Number of oscillators in the network.
     @param[in] parameters (pcnn_parameters): Parameters of the network.
     @param[in] type_conn (conn_type): Type of connection between oscillators in the network (all-to-all, grid, bidirectional list, etc.).
     @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
     @param[in] height (uint): Number of oscillators in column of the network, this argument is used 
                 only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
     @param[in] width (uint): Number of oscillotors in row of the network, this argument is used only 
                 for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
     @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering).
     
     """
     
     self._outputs = None            # list of outputs of oscillators.
 
     self._feeding = None            # feeding compartment of each oscillator.
     self._linking = None            # linking compartment of each oscillator.
     self._threshold = None          # threshold of each oscillator.
     
     self._params = None
     
     self.__ccore_pcnn_pointer = None
     
     # set parameters of the network
     if parameters is not None:
         self._params = parameters
     else:
         self._params = pcnn_parameters()
     
     if (ccore is True) and ccore_library.workable():
         network_height = height
         network_width = width
         
         if (type_conn == conn_type.GRID_FOUR) or (type_conn == conn_type.GRID_EIGHT):
             if (network_height is None) or (network_width is None):
                 side_size = num_osc ** (0.5)
                 if side_size - math.floor(side_size) > 0:
                     raise NameError('Invalid number of oscillators in the network in case of grid structure')
             
                 network_height = int(side_size)
                 network_width = int(side_size)
         else:
             network_height = 0
             network_width = 0
             
         self.__ccore_pcnn_pointer = wrapper.pcnn_create(num_osc, type_conn, network_height, network_width, self._params)
     else:
         super().__init__(num_osc, type_conn, type_conn_represent, height, width)
         
         self._outputs = [0.0] * self._num_osc
         
         self._feeding = [0.0] * self._num_osc
         self._linking = [0.0] * self._num_osc
         self._threshold = [ random.random() for i in range(self._num_osc) ]
Beispiel #37
0
 def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True):
     """!
     @brief Constructor of oscillatory network is based on Kuramoto model.
     
     @param[in] num_osc (uint): Number of oscillators in the network.
     @param[in] parameters (pcnn_parameters): Parameters of the network.
     @param[in] type_conn (conn_type): Type of connection between oscillators in the network (all-to-all, grid, bidirectional list, etc.).
     @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
     @param[in] height (uint): Number of oscillators in column of the network, this argument is used 
                 only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
     @param[in] width (uint): Number of oscillotors in row of the network, this argument is used only 
                 for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
     @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering).
     
     """
     
     self._outputs = None            # list of outputs of oscillators.
 
     self._feeding = None            # feeding compartment of each oscillator.
     self._linking = None            # linking compartment of each oscillator.
     self._threshold = None          # threshold of each oscillator.
     
     self._params = None
     
     self.__ccore_pcnn_pointer = None
     
     # set parameters of the network
     if parameters is not None:
         self._params = parameters
     else:
         self._params = pcnn_parameters()
     
     if (ccore is True) and ccore_library.workable():
         network_height = height
         network_width = width
         
         if (type_conn == conn_type.GRID_FOUR) or (type_conn == conn_type.GRID_EIGHT):
             if (network_height is None) or (network_width is None):
                 side_size = num_osc ** (0.5)
                 if side_size - math.floor(side_size) > 0:
                     raise NameError('Invalid number of oscillators in the network in case of grid structure')
             
                 network_height = int(side_size)
                 network_width = int(side_size)
         else:
             network_height = 0
             network_width = 0
             
         self.__ccore_pcnn_pointer = wrapper.pcnn_create(num_osc, type_conn, network_height, network_width, self._params)
     else:
         super().__init__(num_osc, type_conn, type_conn_represent, height, width)
         
         self._outputs = [0.0] * self._num_osc
         
         self._feeding = [0.0] * self._num_osc
         self._linking = [0.0] * self._num_osc
         self._threshold = [ random.random() for i in range(self._num_osc) ]
Beispiel #38
0
    def __setstate__(self, som_state):
        """
        @brief Set state of SOM network that can be used to load network.

        """
        if som_state['ccore'] is True and ccore_library.workable():
            self.__upload_dump_to_ccore(som_state['state'])
        else:
            self.__upload_dump_to_python(som_state['state'])
Beispiel #39
0
    def __init__(self,
                 num_osc,
                 parameters=None,
                 type_conn=conn_type.ALL_TO_ALL,
                 type_conn_represent=conn_represent.MATRIX,
                 ccore=True):
        """!
        @brief Constructor of oscillatory network LEGION (local excitatory global inhibitory oscillatory network).
        
        @param[in] num_osc (uint): Number of oscillators in the network.
        @param[in] parameters (legion_parameters): Parameters of the network that are defined by structure 'legion_parameters'.
        @param[in] type_conn (conn_type): Type of connection between oscillators in the network.
        @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
        @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering).
        
        """

        self._params = None
        # parameters of the network

        self.__ccore_legion_pointer = None
        self._params = parameters

        # set parameters of the network
        if (self._params is None):
            self._params = legion_parameters()

        if ((ccore is True) and ccore_library.workable()):
            self.__ccore_legion_pointer = wrapper.legion_create(
                num_osc, type_conn, self._params)

        else:
            super().__init__(num_osc, type_conn, type_conn_represent)

            # initial states
            self._excitatory = [random.random() for _ in range(self._num_osc)]
            self._inhibitory = [0.0] * self._num_osc
            self._potential = [0.0] * self._num_osc

            self._coupling_term = None
            # coupling term of each oscillator
            self._global_inhibitor = 0
            # value of global inhibitory
            self._stimulus = None
            # stimulus of each oscillator

            self._dynamic_coupling = None
            # dynamic connection between oscillators
            self._coupling_term = [0.0] * self._num_osc
            self._buffer_coupling_term = [0.0] * self._num_osc

            # generate first noises
            self._noise = [
                random.random() * self._params.ro for i in range(self._num_osc)
            ]
Beispiel #40
0
    def __initialize_ccore_state(self, ccore):
        """!
        @brief Initializes C++ pyclustering state.
        @details Check if it is requested and if it is available for the current platform. These information is used to
                  set status of C++ pyclustering library.

        @param[in] ccore (bool):

        """
        self.__ccore = ccore
        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #41
0
 def __init__(self, num_osc, stimulus = None, parameters = None, type_conn = None, type_conn_represent = conn_represent.MATRIX, ccore = True):
     """!
     @brief Constructor of oscillatory network based on Hodgkin-Huxley neuron model.
     
     @param[in] num_osc (uint): Number of peripheral oscillators in the network.
     @param[in] stimulus (list): List of stimulus for oscillators, number of stimulus should be equal to number of peripheral oscillators.
     @param[in] parameters (hhn_parameters): Parameters of the network.
     @param[in] type_conn (conn_type): Type of connections between oscillators in the network (ignored for this type of network).
     @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
     @param[in] ccore (bool): If 'True' then CCORE is used (C/C++ implementation of the model).
     
     """
       
     super().__init__(num_osc, conn_type.NONE, type_conn_represent);
     
     if (stimulus is None):
         self._stimulus = [0.0] * num_osc;
     else:
         self._stimulus = stimulus;
     
     if (parameters is not None):
         self._params = parameters;
     else:
         self._params = hhn_parameters();
     
     self.__ccore_hhn_pointer = None;
     self.__ccore_hhn_dynamic_pointer = None;
     
     if ( (ccore is True) and ccore_library.workable() ):
         self.__ccore_hhn_pointer = wrapper.hhn_create(num_osc, self._params);
     else:
         self._membrane_dynamic_pointer = None;        # final result is stored here.
         
         self._membrane_potential        = [0.0] * self._num_osc;
         self._active_cond_sodium        = [0.0] * self._num_osc;
         self._inactive_cond_sodium      = [0.0] * self._num_osc;
         self._active_cond_potassium     = [0.0] * self._num_osc;
         self._link_activation_time      = [0.0] * self._num_osc;
         self._link_pulse_counter        = [0.0] * self._num_osc;
         self._link_deactivation_time    = [0.0] * self._num_osc;
         self._link_weight3              = [0.0] * self._num_osc;
         self._pulse_generation_time     = [ [] for i in range(self._num_osc) ];
         self._pulse_generation          = [False] * self._num_osc;
         
         self._noise = [random.random() * 2.0 - 1.0 for i in range(self._num_osc)];
         
         self._central_element = [central_element(), central_element()];
Beispiel #42
0
 def __init__(self, num_osc, parameters = None, type_conn = conn_type.ALL_TO_ALL, type_conn_represent = conn_represent.MATRIX, ccore = True):
     """!
     @brief Constructor of oscillatory network LEGION (local excitatory global inhibitory oscillatory network).
     
     @param[in] num_osc (uint): Number of oscillators in the network.
     @param[in] parameters (legion_parameters): Parameters of the network that are defined by structure 'legion_parameters'.
     @param[in] type_conn (conn_type): Type of connection between oscillators in the network.
     @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list.
     @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering).
     
     """
     
     self._params = None;                 # parameters of the network
 
     self.__ccore_legion_pointer = None;
     self._params = parameters;
     
     # set parameters of the network
     if (self._params is None):
         self._params = legion_parameters();
     
     if ( (ccore is True) and ccore_library.workable() ):
         self.__ccore_legion_pointer = wrapper.legion_create(num_osc, type_conn, self._params);
         
     else: 
         super().__init__(num_osc, type_conn, type_conn_represent);
             
         # initial states
         self._excitatory = [ random.random() for _ in range(self._num_osc) ];
         self._inhibitory = [0.0] * self._num_osc;
         self._potential = [0.0] * self._num_osc;
         
         self._coupling_term = None;      # coupling term of each oscillator
         self._global_inhibitor = 0;      # value of global inhibitory
         self._stimulus = None;           # stimulus of each oscillator
         
         self._dynamic_coupling = None;   # dynamic connection between oscillators
         self._coupling_term = [0.0] * self._num_osc;
         self._buffer_coupling_term = [0.0] * self._num_osc;
             
         # generate first noises
         self._noise = [random.random() * self._params.ro for i in range(self._num_osc)];
Beispiel #43
0
    def __init__(self, num_osc, increase_strength1, increase_strength2, ccore = True):
        """!
        @brief Constructor of oscillatory network for pattern recognition based on Kuramoto model.
        
        @param[in] num_osc (uint): Number of oscillators in the network.
        @param[in] increase_strength1 (double): Parameter for increasing strength of the second term of the Fourier component.
        @param[in] increase_strength2 (double): Parameter for increasing strength of the third term of the Fourier component.
        @param[in] ccore (bool): If True simulation is performed by CCORE library (C++ implementation of pyclustering).
        
        """
        
        if ( (ccore is True) and ccore_library.workable() ):
            self._ccore_network_pointer = wrapper.syncpr_create(num_osc, increase_strength1, increase_strength2);
            
        else:
            self._increase_strength1 = increase_strength1;
            self._increase_strength2 = increase_strength2;
            self._coupling = [ [0.0 for i in range(num_osc)] for j in range(num_osc) ];

            super().__init__(num_osc, 1, 0, conn_type.ALL_TO_ALL, conn_represent.MATRIX, initial_type.RANDOM_GAUSSIAN, ccore)
Beispiel #44
0
    def __init__(self, data, kmin, kmax, **kwargs):
        """!
        @brief Construct Elbow method.

        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] kmin (int): Minimum amount of clusters that should be considered.
        @param[in] kmax (int): Maximum amount of clusters that should be considered.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'ccore', 'initializer').

        <b>Keyword Args:</b><br>
            - ccore (bool): If True then CCORE (C++ implementation of pyclustering library) is used (by default True).
            - initializer (callable): Center initializer that is used by K-Means algorithm (by default K-Means++).

        """
        if kmax - kmin < 3:
            raise ValueError("Amount of K (" + str(kmax - kmin) + ") is too small for analysis. "
                             "It is require to have at least three K to build elbow.")

        if len(data) < kmax:
            raise ValueError("K max value '%d' is greater than amount of points in data '%d'." % (kmax, len(data)))

        self.__initializer = kwargs.get('initializer', kmeans_plusplus_initializer)

        self.__ccore = kwargs.get('ccore', True) or \
                       isinstance(self.__initializer, kmeans_plusplus_initializer) or \
                       isinstance(self.__initializer, random_center_initializer)

        if self.__ccore:
            self.__ccore = ccore_library.workable()

        self.__data = data
        self.__kmin = kmin
        self.__kmax = kmax

        self.__wce = []
        self.__elbows = []
        self.__kvalue = -1
Beispiel #45
0
    def __init__(self, data, initial_centers, tolerance=0.001, ccore=True, **kwargs):
        """!
        @brief Constructor of clustering algorithm K-Means.
        @details Center initializer can be used for creating initial centers, for example, K-Means++ method.
        
        @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
        @param[in] initial_centers (array_like): Initial coordinates of centers of clusters that are represented by array_like data structure: [center1, center2, ...].
        @param[in] tolerance (double): Stop condition: if maximum value of change of centers of clusters is less than tolerance then algorithm stops processing.
        @param[in] ccore (bool): Defines should be CCORE library (C++ pyclustering library) used instead of Python code or not.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'observer', 'metric', 'itermax').
        
        <b>Keyword Args:</b><br>
            - observer (kmeans_observer): Observer of the algorithm to collect information about clustering process on each iteration.
            - metric (distance_metric): Metric that is used for distance calculation between two points (by default euclidean square distance).
            - itermax (uint): Maximum number of iterations that is used for clustering process (by default: 200).
        
        @see center_initializer
        
        """
        self.__pointer_data = numpy.array(data)
        self.__clusters = []
        self.__centers = numpy.array(initial_centers)
        self.__tolerance = tolerance
        self.__total_wce = 0

        self.__observer = kwargs.get('observer', None)
        self.__metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
        self.__itermax = kwargs.get('itermax', 100)

        if self.__metric.get_type() != type_metric.USER_DEFINED:
            self.__metric.enable_numpy_usage()
        else:
            self.__metric.disable_numpy_usage()
        
        self.__ccore = ccore and self.__metric.get_type() != type_metric.USER_DEFINED
        if self.__ccore is True:
            self.__ccore = ccore_library.workable()
Beispiel #46
0
    def __init__(self, sample, eps, minpts, amount_clusters = None, ccore = True, **kwargs):
        """!
        @brief Constructor of clustering algorithm OPTICS.
        
        @param[in] sample (list): Input data that is presented as a list of points (objects), where each point is represented by list or tuple.
        @param[in] eps (double): Connectivity radius between points, points may be connected if distance between them less than the radius.
        @param[in] minpts (uint): Minimum number of shared neighbors that is required for establishing links between points.
        @param[in] amount_clusters (uint): Optional parameter where amount of clusters that should be allocated is specified.
                    In case of usage 'amount_clusters' connectivity radius can be greater than real, in other words, there is place for mistake
                    in connectivity radius usage.
        @param[in] ccore (bool): if True than DLL CCORE (C++ solution) will be used for solving the problem.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'data_type').

        <b>Keyword Args:</b><br>
            - data_type (string): Data type of input sample 'data' that is processed by the algorithm ('points', 'distance_matrix').

        """
        
        self.__sample_pointer = sample      # Algorithm parameter - pointer to sample for processing.
        self.__eps = eps                    # Algorithm parameter - connectivity radius between object for establish links between object.
        self.__minpts = minpts              # Algorithm parameter - minimum number of neighbors that is required for establish links between object.
        self.__amount_clusters = amount_clusters
        
        self.__ordering = None
        self.__clusters = None
        self.__noise = None
        self.__optics_objects = None

        self.__data_type = kwargs.get('data_type', 'points')
        
        self.__kdtree = None
        self.__ccore = ccore

        self.__neighbor_searcher = self.__create_neighbor_searcher(self.__data_type)

        if self.__ccore:
            self.__ccore = ccore_library.workable()
Beispiel #47
0
    def __init__(self, rows, cols, conn_type = type_conn.grid_eight, parameters = None, ccore = True):
        """!
        @brief Constructor of self-organized map.
        
        @param[in] rows (uint): Number of neurons in the column (number of rows).
        @param[in] cols (uint): Number of neurons in the row (number of columns).
        @param[in] conn_type (type_conn): Type of connection between oscillators in the network (grid four, grid eight, honeycomb, function neighbour).
        @param[in] parameters (som_parameters): Other specific parameters.
        @param[in] ccore (bool): If True simulation is performed by CCORE library (C++ implementation of pyclustering).
        
        """
        
        # some of these parameters are required despite core implementation, for example, for network demonstration.
        self._cols = cols
        
        self._rows = rows
        
        self._size = cols * rows
        
        self._conn_type = conn_type
        
        self._data = None
        
        self._neighbors = None
        
        self._local_radius = 0.0
        
        self._learn_rate = 0.0
        
        self.__ccore_som_pointer = None

        if parameters is not None:
            self._params = parameters
        else:
            self._params = som_parameters()
            
        if self._params.init_radius is None:
            self._params.init_radius = self.__initialize_initial_radius(rows, cols)
        
        if (ccore is True) and ccore_library.workable():
            self.__ccore_som_pointer = wrapper.som_create(rows, cols, conn_type, self._params)
            
        else:
            # location
            self._location = self.__initialize_locations(rows, cols)
            
            # default weights
            self._weights = [ [0.0] ] * self._size
            
            # awards
            self._award = [0] * self._size
            
            # captured objects
            self._capture_objects = [ [] for i in range(self._size) ]
            
            # distances - calculate and store them only during training
            self._sqrt_distances = None
        
            # connections
            if conn_type != type_conn.func_neighbor:
                self._create_connections(conn_type)