Created
May 6, 2012 10:49
-
-
Save joelcox/2621608 to your computer and use it in GitHub Desktop.
KMeans plot
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
import os | |
import sys | |
import csv | |
sys.path.insert(0, os.path.abspath('../miner/')) | |
import miner.utils | |
import miner.clustering | |
def render_to_file(space, clusters): | |
colors = {0: 'r', 1: 'b', 2: 'g', 3: 'y', 4: 'b'} | |
import numpy as np | |
import matplotlib.pyplot as plt | |
fig = plt.figure(1, figsize=(5, 5)) | |
ax = fig.add_subplot(111) | |
ax.set_aspect(1) | |
# Redo our clusters in NumPy | |
for cluster in range(len(clusters)): | |
color = colors[cluster] | |
x = [] | |
y = [] | |
for point in clusters[cluster]['points']: | |
x.append(space.points[point][0]) | |
y.append(space.points[point][1]) | |
np.array(x) | |
np.array(y) | |
ax.scatter(x, y, color=color) | |
ax.set_xlim(-2, 2) | |
ax.set_ylim(-1, 3.5) | |
plt.draw() | |
plt.savefig('kmeans.png') | |
plt.close() | |
os.system('rm kmeans*.png') | |
space = miner.utils.Space(); | |
file = csv.reader(open('dataforkmeans.csv')) | |
for parts in file: | |
space.point(parts[0], parts[1]) | |
kmeans = miner.clustering.KMeans(3, space) | |
kmeans.converge(render=render_to_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment