Skip to content

Instantly share code, notes, and snippets.

@zkan
Last active February 1, 2019 10:36
Show Gist options
  • Save zkan/77a8bf05b7b2105337d3f7b457e73efe to your computer and use it in GitHub Desktop.
Save zkan/77a8bf05b7b2105337d3f7b457e73efe to your computer and use it in GitHub Desktop.
K-Means clustering
import pandas as pd
from sklearn import cluster
data = {
'data': [2, 3, 4, 10, 12, 20, 30, 11, 25]
}
df = pd.DataFrame(data)
kmeans = cluster.KMeans(n_clusters=2)
kmeans.fit(df)
labels = kmeans.predict(df)
df['cluster'] = labels
print('Final Results:')
print(df)
print('Centroids:')
print(kmeans.cluster_centers_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment