Completed
Push — 0.8.dev ( 0bd906...5803f4 )
by Andrei
01:32
created

distance_metric   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 16 1
C __call__() 0 30 7
1
"""!
2
3
@brief Module provides various distance metrics - abstraction of the notion of distance in a metric space.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2018
7
@copyright GNU Public License
8
9
@cond GNU_PUBLIC_LICENSE
10
    PyClustering is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    PyClustering is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
@endcond
23
24
"""
25
26
27
from enum import IntEnum;
28
29
30
class type_metric(IntEnum):
31
    """!
32
    @brief Enumeration of supported metrics in the module for distance calculation between two points.
33
34
    """
35
36
    ## Euclidean distance, for more information see function 'euclidean_distance'.
37
    EUCLIDEAN = 0;
38
39
    ## Square Euclidean distance, for more information see function 'euclidean_distance_square'.
40
    EUCLIDEAN_SQUARE = 1;
41
42
    ## Manhattan distance, for more information see function 'manhattan_distance'.
43
    MANHATTAN = 2;
44
45
    ## Chebyshev distance, for more information see function 'chebyshev_distance'.
46
    CHEBYSHEV = 3;
47
48
    ## Minkowski distance, for more information see function 'minkowski_distance'.
49
    MINKOWSKI = 4;
50
51
    ## User defined function for distance calculation between two points.
52
    USER_DEFINED = 1000;
53
54
55
class distance_metric:
56
    """!
57
    @brief Distance metric performs distance calculation between two points in line with encapsulated function, for
58
            example, euclidean distance or chebyshev distance, or even user-defined.
59
60
    @details
61
62
    Example of Euclidean distance metric:
63
    @code
64
        metric = distance_metric(type_metric.EUCLIDEAN);
65
        distance = metric([1.0, 2.5], [-1.2, 3.4]);
66
    @endcode
67
68
    In following example additional argument should be specified (generally, 'degree' is a optional argument that is
69
     equal to '2' by default) that is specific for Minkowski distance:
70
    @code
71
        metric = distance_metric(type_metric.MINKOWSKI, degree=4);
72
        distance = metric([4.0, 9.2, 1.0], [3.4, 2.5, 6.2]);
73
    @endcode
74
75
    User may define its own function for distance calculation:
76
    @code
77
        user_function = lambda point1, point2: point1[0] + point2[0] + 2;
78
        metric = distance_metric(type_metric.USER_DEFINED, func=user_function);
79
        distance = metric([2.0, 3.0], [1.0, 3.0]);
80
    @endcode
81
82
    """
83
    def __init__(self, type, **kwargs):
84
        """!
85
        @brief Creates distance metric instance for calculation distance between two points.
86
87
        @param[in] type (type_metric):
88
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'func' and corresponding additional argument for
89
                    for specific metric types).
90
91
        Keyword Args:
92
            func (callable): Callable object with two arguments (point #1 and point #2) that is used only if metric is 'type_metric.USER_DEFINED'.
93
            degree (numeric): Only for 'type_metric.MINKOWSKI' - degree of Minkowski equation.
94
95
        """
96
        self.__type = type;
97
        self.__args = kwargs;
98
        self.__func = self.__args.get('func', None);
99
100
101
    def __call__(self, point1, point2):
102
        """!
103
        @brief Calculates distance between two points.
104
105
        @param[in] point1 (list): The first point.
106
        @param[in] point2 (list): The second point.
107
108
        @return (double) Distance between two points.
109
110
        """
111
        if self.__type == type_metric.EUCLIDEAN:
112
            return euclidean_distance(point1, point2);
113
114
        elif self.__type == type_metric.EUCLIDEAN_SQUARE:
115
            return euclidean_distance_square(point1, point2);
116
117
        elif self.__type == type_metric.MANHATTAN:
118
            return manhattan_distance(point1, point2);
119
120
        elif self.__type == type_metric.CHEBYSHEV:
121
            return chebyshev_distance(point1, point2);
122
123
        elif self.__type == type_metric.MINKOWSKI:
124
            return minkowski_distance(point1, point2, self.__args.get('degree', 2));
125
126
        elif self.__type == type_metric.USER_DEFINED:
127
            return self.__func(point1, point2);
128
129
        else:
130
            raise ValueError("Unknown type of metric: '%d'", self.__type);
131
132
133
def euclidean_distance(point1, point2):
134
    """!
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
135
    @brief Calculate Euclidean distance between two vectors.
136
    @details The Euclidean between vectors (points) a and b is calculated by following formula:
137
138
    \f[
139
    dist(a, b) = \sqrt{ \sum_{i=0}^{N}(a_{i} - b_{i})^{2} };
140
    \f]
141
142
    Where N is a length of each vector.
143
144
    @param[in] point1 (list): The first vector.
145
    @param[in] point2 (list): The second vector.
146
147
    @return (double) Euclidean distance between two vectors.
148
149
    @see euclidean_distance_square, manhattan_distance, chebyshev_distance
150
151
    """
152
    distance = euclidean_distance_square(point1, point2);
153
    return distance ** 0.5;
154
155
156
def euclidean_distance_square(point1, point2):
157
    """!
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
158
    @brief Calculate square Euclidean distance between two vectors.
159
160
    \f[
161
    dist(a, b) = \sum_{i=0}^{N}(a_{i} - b_{i})^{2};
162
    \f]
163
164
    @param[in] point1 (list): The first vector.
165
    @param[in] point2 (list): The second vector.
166
167
    @return (double) Square Euclidean distance between two vectors.
168
169
    @see euclidean_distance, manhattan_distance, chebyshev_distance
170
171
    """
172
    distance = 0.0;
173
    for i in range(len(point1)):
174
        distance += (point1[i] - point2[i]) ** 2.0;
175
176
    return distance;
177
178
179
def manhattan_distance(point1, point2):
180
    """!
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \l was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
181
    @brief Calculate Manhattan distance between between two vectors.
182
183
    \f[
184
    dist(a, b) = \sum_{i=0}^{N}\left | a_{i} - b_{i} \right |;
185
    \f]
186
187
    @param[in] point1 (list): The first vector.
188
    @param[in] point2 (list): The second vector.
189
190
    @return (double) Manhattan distance between two vectors.
191
192
    @see euclidean_distance_square, euclidean_distance, chebyshev_distance
193
194
    """
195
    distance = 0.0;
196
    dimension = len(point1);
197
198
    for i in range(dimension):
199
        distance += abs(point1[i] - point2[i]);
200
201
    return distance;
202
203
204
def chebyshev_distance(point1, point2):
205
    """!
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \m was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \l was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
206
    @brief Calculate Chebyshev distance between between two vectors.
207
208
    \f[
209
    dist(a, b) = \max_{}i\left (\left | a_{i} - b_{i} \right |\right );
210
    \f]
211
212
    @param[in] point1 (list): The first vector.
213
    @param[in] point2 (list): The second vector.
214
215
    @return (double) Chebyshev distance between two vectors.
216
217
    @see euclidean_distance_square, euclidean_distance, minkowski_distance
218
219
    """
220
    distance = 0.0;
221
    dimension = len(point1);
222
223
    for i in range(dimension):
224
        distance = max(distance, abs(point1[i] - point2[i]));
225
226
    return distance;
227
228
229
def minkowski_distance(point1, point2, degree=2):
230
    """!
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \l was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
231
    @brief Calculate Minkowski distance between two vectors.
232
233
    \f[
234
    dist(a, b) = \sqrt[p]{ \sum_{i=0}^{N}\left(a_{i} - b_{i}\right)^{p} };
235
    \f]
236
237
    @param[in] point1 (list): The first vector.
238
    @param[in] point2 (list): The second vector.
239
    @param[in] degree (numeric): Degree of that is used for Minkowski distance.
240
241
    @return (double) Minkowski distance between two vectors.
242
243
    @see euclidean_distance
244
245
    """
246
    distance = 0.0;
247
    for i in range(len(point1)):
248
        distance += (point1[i] - point2[i]) ** degree;
249
250
    return distance ** (1.0 / degree);