Created
February 2, 2016 02:54
-
-
Save Ely-S/88e18f6f35bb5613ed09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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