sklearn scikit-learn k-means 聚类算法 一维数组聚类 python 二维数组聚类

安装:

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)

猜你喜欢

转载自blog.csdn.net/x1131230123/article/details/113870224