1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2018 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
1 |
|
"""abydos.distance._euclidean. |
20
|
|
|
|
21
|
|
|
Euclidean distance & similarity |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._minkowski import Minkowski |
32
|
|
|
|
33
|
1 |
|
__all__ = ['Euclidean', 'dist_euclidean', 'euclidean', 'sim_euclidean'] |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
class Euclidean(Minkowski): |
|
|
|
|
37
|
|
|
"""Euclidean distance. |
38
|
|
|
|
39
|
|
|
Euclidean distance is the straigh-line or as-the-crow-flies distance, |
40
|
|
|
equivalent to Minkowski distance in :math:`L^2`-space. |
41
|
|
|
""" |
42
|
|
|
|
43
|
1 |
|
def dist_abs(self, src, tar, qval=2, normalized=False, alphabet=None): |
|
|
|
|
44
|
|
|
"""Return the Euclidean distance between two strings. |
45
|
|
|
|
46
|
|
|
Parameters |
47
|
|
|
---------- |
48
|
|
|
src : str |
49
|
|
|
Source string (or QGrams/Counter objects) for comparison |
50
|
|
|
tar : str |
51
|
|
|
Target string (or QGrams/Counter objects) for comparison |
52
|
|
|
qval : int |
53
|
|
|
The length of each q-gram; 0 for non-q-gram version |
54
|
|
|
normalized : bool |
55
|
|
|
Normalizes to [0, 1] if True |
56
|
|
|
alphabet : collection or int |
57
|
|
|
The values or size of the alphabet |
58
|
|
|
|
59
|
|
|
Returns |
60
|
|
|
------- |
61
|
|
|
float |
62
|
|
|
The Euclidean distance |
63
|
|
|
|
64
|
|
|
Examples |
65
|
|
|
-------- |
66
|
|
|
>>> cmp = Euclidean() |
67
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
68
|
|
|
2.0 |
69
|
|
|
>>> round(cmp.dist_abs('Niall', 'Neil'), 12) |
70
|
|
|
2.645751311065 |
71
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
72
|
|
|
3.0 |
73
|
|
|
>>> round(cmp.dist_abs('ATCG', 'TAGC'), 12) |
74
|
|
|
3.162277660168 |
75
|
|
|
|
76
|
|
|
""" |
77
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
78
|
|
|
src, tar, qval, 2, normalized, alphabet |
79
|
|
|
) |
80
|
|
|
|
81
|
1 |
|
def dist(self, src, tar, qval=2, alphabet=None): |
|
|
|
|
82
|
|
|
"""Return the normalized Euclidean distance between two strings. |
83
|
|
|
|
84
|
|
|
The normalized Euclidean distance is a distance |
85
|
|
|
metric in :math:`L^2`-space, normalized to [0, 1]. |
86
|
|
|
|
87
|
|
|
Parameters |
88
|
|
|
---------- |
89
|
|
|
src : str |
90
|
|
|
Source string (or QGrams/Counter objects) for comparison |
91
|
|
|
tar : str |
92
|
|
|
Target string (or QGrams/Counter objects) for comparison |
93
|
|
|
qval : int |
94
|
|
|
The length of each q-gram; 0 for non-q-gram version |
95
|
|
|
alphabet : collection or int |
96
|
|
|
The values or size of the alphabet |
97
|
|
|
|
98
|
|
|
Returns |
99
|
|
|
------- |
100
|
|
|
float |
101
|
|
|
The normalized Euclidean distance |
102
|
|
|
|
103
|
|
|
Examples |
104
|
|
|
-------- |
105
|
|
|
>>> cmp = Euclidean() |
106
|
|
|
>>> round(cmp.dist('cat', 'hat'), 12) |
107
|
|
|
0.57735026919 |
108
|
|
|
>>> round(cmp.dist('Niall', 'Neil'), 12) |
109
|
|
|
0.683130051064 |
110
|
|
|
>>> round(cmp.dist('Colin', 'Cuilen'), 12) |
111
|
|
|
0.727606875109 |
112
|
|
|
>>> cmp.dist('ATCG', 'TAGC') |
113
|
|
|
1.0 |
114
|
|
|
|
115
|
|
|
""" |
116
|
1 |
|
return self.dist_abs(src, tar, qval, True, alphabet) |
117
|
|
|
|
118
|
|
|
|
119
|
1 |
|
def euclidean(src, tar, qval=2, normalized=False, alphabet=None): |
120
|
|
|
"""Return the Euclidean distance between two strings. |
121
|
|
|
|
122
|
|
|
This is a wrapper for :py:meth:`Euclidean.dist_abs`. |
123
|
|
|
|
124
|
|
|
Parameters |
125
|
|
|
---------- |
126
|
|
|
src : str |
127
|
|
|
Source string (or QGrams/Counter objects) for comparison |
128
|
|
|
tar : str |
129
|
|
|
Target string (or QGrams/Counter objects) for comparison |
130
|
|
|
qval : int |
131
|
|
|
The length of each q-gram; 0 for non-q-gram version |
132
|
|
|
normalized : bool |
133
|
|
|
Normalizes to [0, 1] if True |
134
|
|
|
alphabet : collection or int |
135
|
|
|
The values or size of the alphabet |
136
|
|
|
|
137
|
|
|
Returns |
138
|
|
|
------- |
139
|
|
|
float: The Euclidean distance |
140
|
|
|
|
141
|
|
|
Examples |
142
|
|
|
-------- |
143
|
|
|
>>> euclidean('cat', 'hat') |
144
|
|
|
2.0 |
145
|
|
|
>>> round(euclidean('Niall', 'Neil'), 12) |
146
|
|
|
2.645751311065 |
147
|
|
|
>>> euclidean('Colin', 'Cuilen') |
148
|
|
|
3.0 |
149
|
|
|
>>> round(euclidean('ATCG', 'TAGC'), 12) |
150
|
|
|
3.162277660168 |
151
|
|
|
|
152
|
|
|
""" |
153
|
1 |
|
return Euclidean().dist_abs(src, tar, qval, normalized, alphabet) |
154
|
|
|
|
155
|
|
|
|
156
|
1 |
|
def dist_euclidean(src, tar, qval=2, alphabet=None): |
157
|
|
|
"""Return the normalized Euclidean distance between two strings. |
158
|
|
|
|
159
|
|
|
This is a wrapper for :py:meth:`Euclidean.dist`. |
160
|
|
|
|
161
|
|
|
Parameters |
162
|
|
|
---------- |
163
|
|
|
src : str |
164
|
|
|
Source string (or QGrams/Counter objects) for comparison |
165
|
|
|
tar : str |
166
|
|
|
Target string (or QGrams/Counter objects) for comparison |
167
|
|
|
qval : int |
168
|
|
|
The length of each q-gram; 0 for non-q-gram version |
169
|
|
|
alphabet : collection or int |
170
|
|
|
The values or size of the alphabet |
171
|
|
|
|
172
|
|
|
Returns |
173
|
|
|
------- |
174
|
|
|
float |
175
|
|
|
The normalized Euclidean distance |
176
|
|
|
|
177
|
|
|
Examples |
178
|
|
|
-------- |
179
|
|
|
>>> round(dist_euclidean('cat', 'hat'), 12) |
180
|
|
|
0.57735026919 |
181
|
|
|
>>> round(dist_euclidean('Niall', 'Neil'), 12) |
182
|
|
|
0.683130051064 |
183
|
|
|
>>> round(dist_euclidean('Colin', 'Cuilen'), 12) |
184
|
|
|
0.727606875109 |
185
|
|
|
>>> dist_euclidean('ATCG', 'TAGC') |
186
|
|
|
1.0 |
187
|
|
|
|
188
|
|
|
""" |
189
|
1 |
|
return Euclidean().dist(src, tar, qval, alphabet) |
190
|
|
|
|
191
|
|
|
|
192
|
1 |
|
def sim_euclidean(src, tar, qval=2, alphabet=None): |
193
|
|
|
"""Return the normalized Euclidean similarity of two strings. |
194
|
|
|
|
195
|
|
|
This is a wrapper for :py:meth:`Euclidean.sim`. |
196
|
|
|
|
197
|
|
|
Parameters |
198
|
|
|
---------- |
199
|
|
|
src : str |
200
|
|
|
Source string (or QGrams/Counter objects) for comparison |
201
|
|
|
tar : str |
202
|
|
|
Target string (or QGrams/Counter objects) for comparison |
203
|
|
|
qval : int |
204
|
|
|
The length of each q-gram; 0 for non-q-gram version |
205
|
|
|
alphabet : collection or int |
206
|
|
|
The values or size of the alphabet |
207
|
|
|
|
208
|
|
|
Returns |
209
|
|
|
------- |
210
|
|
|
float |
211
|
|
|
The normalized Euclidean similarity |
212
|
|
|
|
213
|
|
|
Examples |
214
|
|
|
-------- |
215
|
|
|
>>> round(sim_euclidean('cat', 'hat'), 12) |
216
|
|
|
0.42264973081 |
217
|
|
|
>>> round(sim_euclidean('Niall', 'Neil'), 12) |
218
|
|
|
0.316869948936 |
219
|
|
|
>>> round(sim_euclidean('Colin', 'Cuilen'), 12) |
220
|
|
|
0.272393124891 |
221
|
|
|
>>> sim_euclidean('ATCG', 'TAGC') |
222
|
|
|
0.0 |
223
|
|
|
|
224
|
|
|
""" |
225
|
1 |
|
return Euclidean().sim(src, tar, qval, alphabet) |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
if __name__ == '__main__': |
229
|
|
|
import doctest |
230
|
|
|
|
231
|
|
|
doctest.testmod() |
232
|
|
|
|