本文整理汇总了Python中pyclustering.utils.metric.distance_metric函数的典型用法代码示例。如果您正苦于以下问题:Python distance_metric函数的具体用法?Python distance_metric怎么用?Python distance_metric使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了distance_metric函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
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()
示例2: __init__
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();
示例3: __init__
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()
示例4: templateLengthProcessData
def templateLengthProcessData(path_to_file, start_centers, expected_cluster_length, ccore, **kwargs):
sample = read_sample(path_to_file)
metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE))
itermax = kwargs.get('itermax', 200)
kmeans_instance = kmeans(sample, start_centers, 0.001, ccore, metric=metric, itermax=itermax)
kmeans_instance.process()
clusters = kmeans_instance.get_clusters()
centers = kmeans_instance.get_centers()
wce = kmeans_instance.get_total_wce()
if itermax == 0:
assertion.eq(start_centers, centers)
assertion.eq([], clusters)
assertion.eq(0.0, wce)
return
obtained_cluster_sizes = [len(cluster) for cluster in clusters]
assertion.eq(len(sample), sum(obtained_cluster_sizes))
assertion.eq(len(clusters), len(centers))
for center in centers:
assertion.eq(len(sample[0]), len(center))
if expected_cluster_length is not None:
obtained_cluster_sizes.sort()
expected_cluster_length.sort()
assertion.eq(obtained_cluster_sizes, expected_cluster_length)
示例5: clustering
def clustering(path, amount, threshold, expected, ccore, **kwargs):
metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN));
sample = read_sample(path);
bsas_instance = bsas(sample, amount, threshold, ccore=ccore, metric=metric);
bsas_instance.process();
clusters = bsas_instance.get_clusters();
representatives = bsas_instance.get_representatives();
obtained_length = 0;
obtained_cluster_length = [];
for cluster in clusters:
obtained_length += len(cluster);
obtained_cluster_length.append(len(cluster));
assertion.eq(len(sample), obtained_length);
assertion.eq(len(expected), len(clusters));
assertion.eq(len(expected), len(representatives));
assertion.ge(amount, len(clusters));
dimension = len(sample[0]);
for rep in representatives:
assertion.eq(dimension, len(rep));
expected.sort();
obtained_cluster_length.sort();
assertion.eq(expected, obtained_cluster_length);
示例6: testCalculateMetric
def testCalculateMetric(self):
assertion.eq(1.0, metric.distance_metric(metric.type_metric.EUCLIDEAN)([0.0, 1.0], [0.0, 0.0]))
assertion.eq(4.0, metric.distance_metric(metric.type_metric.EUCLIDEAN_SQUARE)([2.0, 2.0], [4.0, 2.0]))
assertion.eq(4.0, metric.distance_metric(metric.type_metric.MANHATTAN)([1.0, 1.0], [-1.0, -1.0]))
assertion.eq(2.0, metric.distance_metric(metric.type_metric.CHEBYSHEV)([2.0, -2.0], [0.0, 0.0]))
assertion.eq(2.0, metric.distance_metric(metric.type_metric.MINKOWSKI)([-3.0, -3.0], [-5.0, -3.0]))
assertion.eq(2.0, metric.distance_metric(metric.type_metric.MINKOWSKI, degree=2)([-3.0, -3.0], [-5.0, -3.0]))
assertion.eq(4.0, metric.distance_metric(metric.type_metric.USER_DEFINED, func=metric.euclidean_distance_square)([2.0, 2.0], [4.0, 2.0]))
user_function = lambda point1, point2: point1[0] + point2[0] + 2
assertion.eq(5.0, metric.distance_metric(metric.type_metric.USER_DEFINED, func=user_function)([2.0, 3.0], [1.0, 3.0]))
示例7: templateLengthProcessWithMetric
def templateLengthProcessWithMetric(path_to_file, initial_medoids, expected_cluster_length, metric, ccore_flag, **kwargs):
sample = read_sample(path_to_file)
data_type = kwargs.get('data_type', 'points')
input_type = kwargs.get('input_type', 'list')
initialize_medoids = kwargs.get('initialize_medoids', None)
itermax = kwargs.get('itermax', 200)
if metric is None:
metric = distance_metric(type_metric.EUCLIDEAN_SQUARE)
input_data = sample
if data_type == 'distance_matrix':
input_data = calculate_distance_matrix(sample)
if input_type == 'numpy':
input_data = numpy.array(input_data)
testing_result = False
testing_attempts = 1
if initialize_medoids is not None: # in case center initializer randomization appears
testing_attempts = 10
for _ in range(testing_attempts):
if initialize_medoids is not None:
initial_medoids = kmeans_plusplus_initializer(sample, initialize_medoids).initialize(return_index=True)
kmedoids_instance = kmedoids(input_data, initial_medoids, 0.001, ccore_flag, metric=metric, data_type=data_type, itermax=itermax)
kmedoids_instance.process()
clusters = kmedoids_instance.get_clusters()
medoids = kmedoids_instance.get_medoids()
if itermax == 0:
assertion.eq([], clusters)
assertion.eq(medoids, initial_medoids)
return
if len(clusters) != len(medoids):
continue
if len(set(medoids)) != len(medoids):
continue
obtained_cluster_sizes = [len(cluster) for cluster in clusters]
if len(sample) != sum(obtained_cluster_sizes):
continue
if expected_cluster_length is not None:
obtained_cluster_sizes.sort()
expected_cluster_length.sort()
if obtained_cluster_sizes != expected_cluster_length:
continue
testing_result = True
assertion.true(testing_result)
示例8: template_clustering
def template_clustering(path, amount, threshold, **kwargs):
metric = kwargs.get('metric', distance_metric(type_metric.EUCLIDEAN_SQUARE));
ccore = kwargs.get('ccore', False);
draw = kwargs.get('draw', True);
sample = read_sample(path);
print("Sample: ", path);
bsas_instance = bsas(sample, amount, threshold, ccore=ccore, metric=metric);
bsas_instance.process();
clusters = bsas_instance.get_clusters();
representatives = bsas_instance.get_representatives();
if draw is True:
bsas_visualizer.show_clusters(sample, clusters, representatives);
示例9: template_clustering
def template_clustering(start_centers, path, tolerance = 0.25, ccore = False):
sample = read_sample(path)
dimension = len(sample[0])
metric = distance_metric(type_metric.MANHATTAN)
observer = kmeans_observer()
kmeans_instance = kmeans(sample, start_centers, tolerance, ccore, observer=observer, metric=metric)
(ticks, _) = timedcall(kmeans_instance.process)
clusters = kmeans_instance.get_clusters()
centers = kmeans_instance.get_centers()
print("Sample: ", path, "\t\tExecution time: ", ticks, "\n")
visualizer = cluster_visualizer_multidim()
visualizer.append_clusters(clusters, sample)
visualizer.show()
if dimension > 3:
kmeans_visualizer.show_clusters(sample, clusters, centers, start_centers)
kmeans_visualizer.animate_cluster_allocation(sample, observer)
示例10: __init__
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()
示例11: testClusterAllocationSampleSimple1MinkowskiDistanceMatrixByCore
def testClusterAllocationSampleSimple1MinkowskiDistanceMatrixByCore(self):
metric = distance_metric(type_metric.MINKOWSKI, degree=2.0)
KmedoidsTestTemplates.templateLengthProcessWithMetric(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [2, 9], [5, 5], metric, True, data_type='distance_matrix')
示例12: testClusterAllocationSampleSimple1ChebyshevDistanceMatrixByCore
def testClusterAllocationSampleSimple1ChebyshevDistanceMatrixByCore(self):
metric = distance_metric(type_metric.CHEBYSHEV)
KmedoidsTestTemplates.templateLengthProcessWithMetric(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [2, 9], [5, 5], metric, True, data_type='distance_matrix')
示例13: testClusterAllocationSampleSimple1ManhattanDistanceMatrixByCore
def testClusterAllocationSampleSimple1ManhattanDistanceMatrixByCore(self):
metric = distance_metric(type_metric.MANHATTAN)
KmedoidsTestTemplates.templateLengthProcessWithMetric(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [2, 9], [5, 5], metric, True, data_type='distance_matrix')
示例14: testClusterAllocationSampleSimple1SquareEuclideanDistanceMatrixByCore
def testClusterAllocationSampleSimple1SquareEuclideanDistanceMatrixByCore(self):
metric = distance_metric(type_metric.EUCLIDEAN_SQUARE)
KmedoidsTestTemplates.templateLengthProcessWithMetric(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [2, 9], [5, 5], metric, True, data_type='distance_matrix')
示例15: testClusterAllocationSampleSimple1EuclideanByCore
def testClusterAllocationSampleSimple1EuclideanByCore(self):
metric = distance_metric(type_metric.EUCLIDEAN)
KmedoidsTestTemplates.templateLengthProcessWithMetric(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [2, 9], [5, 5], metric, True)