Skip to content

Instantly share code, notes, and snippets.

@dwinter
Created April 28, 2011 09:05
Show Gist options
  • Save dwinter/946053 to your computer and use it in GitHub Desktop.
Save dwinter/946053 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy import cluster
from matplotlib import pyplot
#fake some data
tests = np.reshape( np.random.uniform(0,100,60), (30,2) )
#plot remaining variance for each value for 'k' between 1,10
initial = [cluster.vq.kmeans(tests,i) for i in range(1,10)]
pyplot.plot([var for (cent,var) in initial])
pyplot.show()
#pick a winner
cent, var = initial[3]
#use vq() to get as assignment for each obs.
assignment,cdist = cluster.vq.vq(tests,cent)
pyplot.scatter(tests[:,0], tests[:,1], c=assignment)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment