diff options
Diffstat (limited to 'source')
-rwxr-xr-x | source/clusters.py | 7 | ||||
-rw-r--r-- | source/testclusters.py | 2 |
2 files changed, 8 insertions, 1 deletions
diff --git a/source/clusters.py b/source/clusters.py index 02766bdf..e53fac9b 100755 --- a/source/clusters.py +++ b/source/clusters.py @@ -1,6 +1,7 @@ #! /usr/bin/env python
#-*- coding: utf-8 -*-
+import math
import random
from math import sqrt
@@ -51,6 +52,12 @@ def tanimoto(v1, v2): shr += 1 # in both
return 1.0 - (float(shr) / (c1 + c2 - shr))
+def euclidian(v1, v2):
+ d = 0.0
+ for i in range(len(v1)):
+ d += (v1[i] - v2[i])**2
+ return math.sqrt(d)
+
def kcluster(rows,distance=pearson,k=4):
# Determine the minimum and maximum values for each point
ranges=[(min([row[i] for row in rows]),max([row[i] for row in rows]))
diff --git a/source/testclusters.py b/source/testclusters.py index 728e9c1b..6feabfc8 100644 --- a/source/testclusters.py +++ b/source/testclusters.py @@ -10,7 +10,7 @@ blognames,words,data = clusters.readfile("blogdata1.txt") coords = clusters.scaledown(data) print "Generating clusters..." -kclust = clusters.kcluster(data, k=K, distance=clusters.pearson) +kclust = clusters.kcluster(data, k=K, distance=clusters.euclidian) print print "Clusters:" for i in range(K): |