pyclustering.utils.metric.distance_metric Class Reference

Distance metric performs distance calculation between two points in line with encapsulated function, for example, euclidean distance or chebyshev distance, or even user-defined. More...

Public Member Functions

def __init__ (self, metric_type, kwargs)
 Creates distance metric instance for calculation distance between two points. More...
 
def __call__ (self, point1, point2)
 Calculates distance between two points. More...
 
def get_type (self)
 Return type of distance metric that is used. More...
 
def get_arguments (self)
 Return additional arguments that are used by distance metric. More...
 
def get_function (self)
 Return user-defined function for calculation distance metric. More...
 
def enable_numpy_usage (self)
 Start numpy for distance calculation. More...
 
def disable_numpy_usage (self)
 Stop using numpy for distance calculation. More...
 

Detailed Description

Distance metric performs distance calculation between two points in line with encapsulated function, for example, euclidean distance or chebyshev distance, or even user-defined.

Example of Euclidean distance metric:

metric = distance_metric(type_metric.EUCLIDEAN)
distance = metric([1.0, 2.5], [-1.2, 3.4])

Example of Chebyshev distance metric:

metric = distance_metric(type_metric.CHEBYSHEV)
distance = metric([0.0, 0.0], [2.5, 6.0])

In following example additional argument should be specified (generally, 'degree' is a optional argument that is equal to '2' by default) that is specific for Minkowski distance:

metric = distance_metric(type_metric.MINKOWSKI, degree=4)
distance = metric([4.0, 9.2, 1.0], [3.4, 2.5, 6.2])

User may define its own function for distance calculation. In this case input is two points, for example, you want to implement your own version of Manhattan distance:

from pyclustering.utils.metric import distance_metric, type_metric
def my_manhattan(point1, point2):
dimension = len(point1)
result = 0.0
for i in range(dimension):
result += abs(point1[i] - point2[i]) * 0.1
return result
metric = distance_metric(type_metric.USER_DEFINED, func=my_manhattan)
distance = metric([2.0, 3.0], [1.0, 3.0])

Definition at line 64 of file metric.py.

Constructor & Destructor Documentation

◆ __init__()

def pyclustering.utils.metric.distance_metric.__init__ (   self,
  metric_type,
  kwargs 
)

Creates distance metric instance for calculation distance between two points.

Parameters
[in]metric_type(type_metric):
[in]**kwargsArbitrary keyword arguments (available arguments: 'numpy_usage' 'func' and corresponding additional argument for for specific metric types).

Keyword Args:

  • func (callable): Callable object with two arguments (point #1 and point #2) or (object #1 and object #2) in case of numpy usage. This argument is used only if metric is 'type_metric.USER_DEFINED'.
  • degree (numeric): Only for 'type_metric.MINKOWSKI' - degree of Minkowski equation.
  • numpy_usage (bool): If True then numpy is used for calculation (by default is False).

Definition at line 107 of file metric.py.

Member Function Documentation

◆ __call__()

def pyclustering.utils.metric.distance_metric.__call__ (   self,
  point1,
  point2 
)

Calculates distance between two points.

Parameters
[in]point1(list): The first point.
[in]point2(list): The second point.
Returns
(double) Distance between two points.

Definition at line 130 of file metric.py.

◆ disable_numpy_usage()

def pyclustering.utils.metric.distance_metric.disable_numpy_usage (   self)

Stop using numpy for distance calculation.

Useful in case of big amount of small data portion when numpy call is longer than calculation itself. No effect in case of type_metric.USER_DEFINED type.

Definition at line 184 of file metric.py.

◆ enable_numpy_usage()

def pyclustering.utils.metric.distance_metric.enable_numpy_usage (   self)

Start numpy for distance calculation.

Useful in case matrices to increase performance. No effect in case of type_metric.USER_DEFINED type.

Definition at line 173 of file metric.py.

◆ get_arguments()

def pyclustering.utils.metric.distance_metric.get_arguments (   self)

Return additional arguments that are used by distance metric.

Returns
(dict) Additional arguments.

Definition at line 153 of file metric.py.

◆ get_function()

def pyclustering.utils.metric.distance_metric.get_function (   self)

Return user-defined function for calculation distance metric.

Returns
(callable): User-defined distance metric function.

Definition at line 163 of file metric.py.

◆ get_type()

def pyclustering.utils.metric.distance_metric.get_type (   self)

Return type of distance metric that is used.

Returns
(type_metric) Type of distance metric.

Definition at line 143 of file metric.py.


The documentation for this class was generated from the following file: