安装:
pip install sklearn
一维数组聚类:
from sklearn.cluster import KMeans
import numpy as np
x = np.random.random(1000).reshape(-1,1)
km = KMeans(n_clusters=5,max_iter=1000).fit(x)
print(km.cluster_centers_)
y = KMeans(n_clusters=5,max_iter=1000).fit_predict(x) #会得出每个sample属于哪一类
print(y)
二维数组聚类:
from sklearn.cluster import KMeans
import numpy as np
x = np.random.random(1000).reshape(-1,2)
km = KMeans(n_clusters=5,max_iter=1000).fit(x)
print(km.cluster_centers_)
y = KMeans(n_clusters=5,max_iter=1000).fit_predict(x)#会得出每个sample属于哪一类
print(y)