algos

Algorithms for the package.

algos.dtw_distance(x, y, d=<function euclidean_distance>, scaled=False)[source]

Returns the distance of two arrays with dynamic time warping method.

Parameters
  • x (iter) – input array 1

  • y (iter) – input array 2

  • d (func) – distance function, default is euclidean

  • scaled (bool) – should arrays be scaled (i.e. 0-1) before calculation

Returns

distance, 0.0 means arrays are exactly same, upper limit is positive infinity

Return type

float

References

https://en.wikipedia.org/wiki/Dynamic_time_warping

Examples

>>> dtw_distance([1, 2, 3, 4], [1, 2, 3, 4])
0.0
>>> dtw_distance([1, 2, 3, 4], [0, 0, 0])
10.0
>>> dtw_distance([1, 2, 3, 4], [0, 2, 0, 4])
4.0
>>> dtw_distance([1, 2, 3, 4], [10, 20, 30, 40])
90.0
>>> dtw_distance([1, 2, 3, 4], [10, 20, 30, 40], scaled=True)
0.0
algos.fastdtw_distance(x, y, d=<function euclidean_distance>)[source]

Dynamic Time Warping (DTW) algorithm with an O(N) time and memory complexity.

Parameters
  • x (iter) – input array 1

  • y (iter) – input array 2

  • d (func) – distance function, default is euclidean

Returns

distance, 0.0 means arrays are exactly same, upper limit is positive infinity

Return type

float

References

https://pypi.org/project/fastdtw/

Examples

>>> fastdtw_distance([1, 2, 3, 4], [1, 2, 3, 4])
0.0
>>> fastdtw_distance([1, 2, 3, 4], [0, 0, 0])
10.0
>>> fastdtw_distance([1, 2, 3, 4], [0, 2, 0, 4])
4.0
>>> fastdtw_distance([1, 2, 3, 4], [10, 20, 30, 40])
90.0
algos.levenshtein_distance(x, y)[source]

Levenshtein distance for string similarity.

Parameters
  • x (str) – input string 1

  • y (str) – input string 2

Returns

distance, 0 means strings are exactly same, upper limit is positive infinity

Return type

int

References

https://en.wikipedia.org/wiki/Levenshtein_distance

Examples

>>> levenshtein_distance('Apple', 'Apple')
0
>>> levenshtein_distance('Apple', 'apple')
1
>>> levenshtein_distance('Apple Inc.', 'apple inc')
3