Skip to content

Instantly share code, notes, and snippets.

@lobrien
Created August 13, 2019 22:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobrien/0f95b690644a862fb4dadb73afb7753b to your computer and use it in GitHub Desktop.
Save lobrien/0f95b690644a862fb4dadb73afb7753b to your computer and use it in GitHub Desktop.
k-means clustering 1d data
from sklearn.cluster import KMeans
import numpy as np
data = np.array([101, 107, 106, 199, 204, 205, 207, 306, 310, 312, 312, 314, 317, 318, 380, 377, 379, 382, 466, 469, 471, 472, 557, 559, 562, 566, 569])
kmeans = KMeans(n_clusters=5).fit(data.reshape(-1,1))
kmeans.predict(data.reshape(-1,1))
# array([4, 4, 4, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3,
# 3, 3, 3, 3], dtype=int32)
kmeans.cluster_centers_
#array([[ 337. ],
# [ 469.5 ],
# [ 203.75 ],
# [ 562.6 ],
# [ 104.66666667]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment