Completed
Push — 0.8.dev ( b131aa...acf4bd )
by Andrei
54s
created

distance_metric   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_arguments() 0 8 1
A get_function() 0 8 1
A __init__() 0 16 1
C __call__() 0 30 7
A get_type() 0 8 1
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
56
class distance_metric:
57
    """!
58
    @brief Distance metric performs distance calculation between two points in line with encapsulated function, for
59
            example, euclidean distance or chebyshev distance, or even user-defined.
60
61
    @details
62
63
    Example of Euclidean distance metric:
64
    @code
65
        metric = distance_metric(type_metric.EUCLIDEAN);
66
        distance = metric([1.0, 2.5], [-1.2, 3.4]);
67
    @endcode
68
69
    In following example additional argument should be specified (generally, 'degree' is a optional argument that is
70
     equal to '2' by default) that is specific for Minkowski distance:
71
    @code
72
        metric = distance_metric(type_metric.MINKOWSKI, degree=4);
73
        distance = metric([4.0, 9.2, 1.0], [3.4, 2.5, 6.2]);
74
    @endcode
75
76
    User may define its own function for distance calculation:
77
    @code
78
        user_function = lambda point1, point2: point1[0] + point2[0] + 2;
79
        metric = distance_metric(type_metric.USER_DEFINED, func=user_function);
80
        distance = metric([2.0, 3.0], [1.0, 3.0]);
81
    @endcode
82
83
    """
84
    def __init__(self, type, **kwargs):
85
        """!
86
        @brief Creates distance metric instance for calculation distance between two points.
87
88
        @param[in] type (type_metric):
89
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'func' and corresponding additional argument for
90
                    for specific metric types).
91
92
        Keyword Args:
93
            func (callable): Callable object with two arguments (point #1 and point #2) that is used only if metric is 'type_metric.USER_DEFINED'.
94
            degree (numeric): Only for 'type_metric.MINKOWSKI' - degree of Minkowski equation.
95
96
        """
97
        self.__type = type;
98
        self.__args = kwargs;
99
        self.__func = self.__args.get('func', None);
100
101
102
    def __call__(self, point1, point2):
103
        """!
104
        @brief Calculates distance between two points.
105
106
        @param[in] point1 (list): The first point.
107
        @param[in] point2 (list): The second point.
108
109
        @return (double) Distance between two points.
110
111
        """
112
        if self.__type == type_metric.EUCLIDEAN:
113
            return euclidean_distance(point1, point2);
114
115
        elif self.__type == type_metric.EUCLIDEAN_SQUARE:
116
            return euclidean_distance_square(point1, point2);
117
118
        elif self.__type == type_metric.MANHATTAN:
119
            return manhattan_distance(point1, point2);
120
121
        elif self.__type == type_metric.CHEBYSHEV:
122
            return chebyshev_distance(point1, point2);
123
124
        elif self.__type == type_metric.MINKOWSKI:
125
            return minkowski_distance(point1, point2, self.__args.get('degree', 2));
126
127
        elif self.__type == type_metric.USER_DEFINED:
128
            return self.__func(point1, point2);
129
130
        else:
131
            raise ValueError("Unknown type of metric: '%d'", self.__type);
132
133
134
    def get_type(self):
135
        """!
136
        @brief Return type of distance metric that is used.
137
138
        @return (type_metric) Type of distance metric.
139
140
        """
141
        return self.__type;
142
143
144
    def get_arguments(self):
145
        """!
146
        @brief Return additional arguments that are used by distance metric.
147
148
        @return (dict) Additional arguments.
149
150
        """
151
        return self.__args;
152
153
154
    def get_function(self):
155
        """!
156
        @brief Return user-defined function for calculation distance metric.
157
158
        @return (callable): User-defined distance metric function.
159
160
        """
161
        return self.__func;
162
163
164
165
def euclidean_distance(point1, point2):
166
    """!
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...
167
    @brief Calculate Euclidean distance between two vectors.
168
    @details The Euclidean between vectors (points) a and b is calculated by following formula:
169
170
    \f[
171
    dist(a, b) = \sqrt{ \sum_{i=0}^{N}(a_{i} - b_{i})^{2} };
172
    \f]
173
174
    Where N is a length of each vector.
175
176
    @param[in] point1 (list): The first vector.
177
    @param[in] point2 (list): The second vector.
178
179
    @return (double) Euclidean distance between two vectors.
180
181
    @see euclidean_distance_square, manhattan_distance, chebyshev_distance
182
183
    """
184
    distance = euclidean_distance_square(point1, point2);
185
    return distance ** 0.5;
186
187
188
def euclidean_distance_square(point1, point2):
189
    """!
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...
190
    @brief Calculate square Euclidean distance between two vectors.
191
192
    \f[
193
    dist(a, b) = \sum_{i=0}^{N}(a_{i} - b_{i})^{2};
194
    \f]
195
196
    @param[in] point1 (list): The first vector.
197
    @param[in] point2 (list): The second vector.
198
199
    @return (double) Square Euclidean distance between two vectors.
200
201
    @see euclidean_distance, manhattan_distance, chebyshev_distance
202
203
    """
204
    distance = 0.0;
205
    for i in range(len(point1)):
206
        distance += (point1[i] - point2[i]) ** 2.0;
207
208
    return distance;
209
210
211
def manhattan_distance(point1, point2):
212
    """!
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...
213
    @brief Calculate Manhattan distance between between two vectors.
214
215
    \f[
216
    dist(a, b) = \sum_{i=0}^{N}\left | a_{i} - b_{i} \right |;
217
    \f]
218
219
    @param[in] point1 (list): The first vector.
220
    @param[in] point2 (list): The second vector.
221
222
    @return (double) Manhattan distance between two vectors.
223
224
    @see euclidean_distance_square, euclidean_distance, chebyshev_distance
225
226
    """
227
    distance = 0.0;
228
    dimension = len(point1);
229
230
    for i in range(dimension):
231
        distance += abs(point1[i] - point2[i]);
232
233
    return distance;
234
235
236
def chebyshev_distance(point1, point2):
237
    """!
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...
238
    @brief Calculate Chebyshev distance between between two vectors.
239
240
    \f[
241
    dist(a, b) = \max_{}i\left (\left | a_{i} - b_{i} \right |\right );
242
    \f]
243
244
    @param[in] point1 (list): The first vector.
245
    @param[in] point2 (list): The second vector.
246
247
    @return (double) Chebyshev distance between two vectors.
248
249
    @see euclidean_distance_square, euclidean_distance, minkowski_distance
250
251
    """
252
    distance = 0.0;
253
    dimension = len(point1);
254
255
    for i in range(dimension):
256
        distance = max(distance, abs(point1[i] - point2[i]));
257
258
    return distance;
259
260
261
def minkowski_distance(point1, point2, degree=2):
262
    """!
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...
263
    @brief Calculate Minkowski distance between two vectors.
264
265
    \f[
266
    dist(a, b) = \sqrt[p]{ \sum_{i=0}^{N}\left(a_{i} - b_{i}\right)^{p} };
267
    \f]
268
269
    @param[in] point1 (list): The first vector.
270
    @param[in] point2 (list): The second vector.
271
    @param[in] degree (numeric): Degree of that is used for Minkowski distance.
272
273
    @return (double) Minkowski distance between two vectors.
274
275
    @see euclidean_distance
276
277
    """
278
    distance = 0.0;
279
    for i in range(len(point1)):
280
        distance += (point1[i] - point2[i]) ** degree;
281
282
    return distance ** (1.0 / degree);