 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, 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:

user_function = lambda point1, point2: point1 + point2 + 2
metric = distance_metric(type_metric.USER_DEFINED, func=user_function)
distance = metric([2.0, 3.0], [1.0, 3.0])

Definition at line 58 of file metric.py.

## ◆ __init__()

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

Creates distance metric instance for calculation distance between two points.

Parameters
 [in] type (type_metric): [in] **kwargs Arbitrary 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 92 of file metric.py.

## ◆ __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 115 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 169 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 158 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

Definition at line 138 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 148 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 128 of file metric.py.

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