Skip to content

Instantly share code, notes, and snippets.

@manvillej
Last active October 9, 2017 18:48
Show Gist options
  • Save manvillej/fbaf0e8f3e442241be868b3ca43c57bc to your computer and use it in GitHub Desktop.
Save manvillej/fbaf0e8f3e442241be868b3ca43c57bc to your computer and use it in GitHub Desktop.
Short Kmeans Example from Sklearn
from sklearn.cluster import KMeans
K=3 # number of clusers
# setting random_state to 0 seeds random number generate to get
# the same results every run. X is the a MxN matrix where N is the number of features
# and M is the number of examples.
kmeans = KMeans(n_clusters=K, random_state=0).fit(X)
# location of centroids
centroids = kmeans.cluster_centers_
# labels for each example, an Mx1 matrix
idx = kmeans.labels_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment