Last active
February 1, 2019 10:36
-
-
Save zkan/77a8bf05b7b2105337d3f7b457e73efe to your computer and use it in GitHub Desktop.
K-Means clustering
This file contains hidden or 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 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