| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import print_function |
| 19 |
import logging |
| 20 |
|
| 21 |
|
| 22 |
logger = logging.getLogger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
class ClusteringError(Exception): |
| 26 |
pass |
| 27 |
|
| 28 |
|
| 29 |
def flatten(L): |
| 30 |
""" |
| 31 |
Flattens a list. |
| 32 |
|
| 33 |
Example: |
| 34 |
|
| 35 |
>>> flatten([a,b,[c,d,[e,f]]]) |
| 36 |
[a,b,c,d,e,f] |
| 37 |
""" |
| 38 |
if not isinstance(L, list): |
| 39 |
return [L] |
| 40 |
|
| 41 |
if L == []: |
| 42 |
return L |
| 43 |
|
| 44 |
return flatten(L[0]) + flatten(L[1:]) |
| 45 |
|
| 46 |
|
| 47 |
def fullyflatten(container): |
| 48 |
""" |
| 49 |
Completely flattens out a cluster and returns a one-dimensional set |
| 50 |
containing the cluster's items. This is useful in cases where some items of |
| 51 |
the cluster are clusters in their own right and you only want the items. |
| 52 |
|
| 53 |
:param container: the container to flatten. |
| 54 |
""" |
| 55 |
flattened_items = [] |
| 56 |
|
| 57 |
for item in container: |
| 58 |
if hasattr(item, 'items'): |
| 59 |
flattened_items = flattened_items + fullyflatten(item.items) |
| 60 |
else: |
| 61 |
flattened_items.append(item) |
| 62 |
|
| 63 |
return flattened_items |
| 64 |
|
| 65 |
|
| 66 |
def median(numbers): |
| 67 |
""" |
| 68 |
Return the median of the list of numbers. |
| 69 |
see: http://mail.python.org/pipermail/python-list/2004-December/294990.html |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
n = len(numbers) |
| 74 |
copy = sorted(numbers) |
| 75 |
if n & 1: |
| 76 |
return copy[n // 2] |
| 77 |
else: |
| 78 |
return (copy[n // 2 - 1] + copy[n // 2]) / 2.0 |
| 79 |
|
| 80 |
|
| 81 |
def mean(numbers): |
| 82 |
""" |
| 83 |
Returns the arithmetic mean of a numeric list. |
| 84 |
see: http://mail.python.org/pipermail/python-list/2004-December/294990.html |
| 85 |
""" |
| 86 |
return float(sum(numbers)) / float(len(numbers)) |
| 87 |
|
| 88 |
|
| 89 |
def minkowski_distance(x, y, p=2): |
| 90 |
""" |
| 91 |
Calculates the minkowski distance between two points. |
| 92 |
|
| 93 |
:param x: the first point |
| 94 |
:param y: the second point |
| 95 |
:param p: the order of the minkowski algorithm. If *p=1* it is equal |
| 96 |
to the manhatten distance, if *p=2* it is equal to the euclidian |
| 97 |
distance. The higher the order, the closer it converges to the |
| 98 |
Chebyshev distance, which has *p=infinity*. |
| 99 |
""" |
| 100 |
from math import pow |
| 101 |
assert len(y) == len(x) |
| 102 |
assert len(x) >= 1 |
| 103 |
sum = 0 |
| 104 |
for i in range(len(x)): |
| 105 |
sum += abs(x[i] - y[i]) ** p |
| 106 |
return pow(sum, 1.0 / float(p)) |
| 107 |
|
| 108 |
|
| 109 |
def magnitude(a): |
| 110 |
"calculates the magnitude of a vecor" |
| 111 |
from math import sqrt |
| 112 |
sum = 0 |
| 113 |
for coord in a: |
| 114 |
sum += coord ** 2 |
| 115 |
return sqrt(sum) |
| 116 |
|
| 117 |
|
| 118 |
def dotproduct(a, b): |
| 119 |
"Calculates the dotproduct between two vecors" |
| 120 |
assert(len(a) == len(b)) |
| 121 |
out = 0 |
| 122 |
for i in range(len(a)): |
| 123 |
out += a[i] * b[i] |
| 124 |
return out |
| 125 |
|
| 126 |
|
| 127 |
def centroid(data, method=median): |
| 128 |
"returns the central vector of a list of vectors" |
| 129 |
out = [] |
| 130 |
for i in range(len(data[0])): |
| 131 |
out.append(method([x[i] for x in data])) |
| 132 |
return tuple(out) |