Skip to content

Instantly share code, notes, and snippets.

@Ely-S
Created February 2, 2016 02:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ely-S/88e18f6f35bb5613ed09 to your computer and use it in GitHub Desktop.
Save Ely-S/88e18f6f35bb5613ed09 to your computer and use it in GitHub Desktop.
#!/usr/bin ipython
import numpy as np
# data
X = np.asarray([(1.0, 1.0), (1.5, 2.0), (3.0, 4.0), (5.0, 7.0), (3.5, 5.0), (4.5, 5.0), (3.5, 4.5)])
# Loyds algorithm with Forgy method
import random
# euclidian distance
def dist(x1, x2):
return np.linalg.norm(x1-x2)
# Takes n means in n dimensions
def find_center(points):
return reduce(np.add, points) / len(points)
def init(X, k=3, iterations = 10):
centers = random.sample(X, k)
while iterations > 0:
print centers
iterations -= 1
# since numpy arrays aint hashable
groups = { id(c): [] for c in centers }
# assign points to groups
for x in X:
center = min(centers, key=lambda i: dist(i, x))
groups[id(center)].append(x)
centers = map(find_center, groups.itervalues())
return centers
print init(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment