Created
August 13, 2019 22:28
-
-
Save lobrien/0f95b690644a862fb4dadb73afb7753b to your computer and use it in GitHub Desktop.
k-means clustering 1d data
This file contains 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
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