聚类算法 sklearn k_means (返回一维数据的最优聚类)
生活随笔
收集整理的這篇文章主要介紹了
聚类算法 sklearn k_means (返回一维数据的最优聚类)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
from sklearn.cluster import KMeans
import numpy
import collections
import pandas
from sklearn import metricsdef k_means(pp1,clus):pv=list(pp1)if len(set(pv))>clus:gf=numpy.array([pv]).Testimator = KMeans(n_clusters=clus)#構(gòu)造聚類器estimator.fit(gf)#聚類label_pred = estimator.labels_ #獲取聚類標簽#print(label_pred)aa=collections.Counter(label_pred)print('aa=',aa)v=pandas.Series(aa)gg=list(v)index_max=gg.index(max(gg))print('index_max=',index_max)centroids = estimator.cluster_centers_ #獲取聚類中心print('centroids=',centroids)#inertia = estimator.inertia_ # 獲取聚類準則的總和center=centroids[index_max][0]return ((center))else:return (pp1.mean())def k_means_label(a):def km_index(k):pv=list(a)gf=numpy.array([pv]).T#from sklearn.cluster import KMeansy_pred = KMeans(n_clusters=k, random_state=9).fit_predict(gf)index=metrics.silhouette_score(gf, y_pred, metric='euclidean')print('index',index)return indexcs=list(range(2,6))df=list(map(km_index,cs))df1=pandas.Series(df,index=cs)df2=df1.sort_values(ascending=False)df3=list(df2.index)[0]return df3a=numpy.random.randint(0,1000,10)cc=k_means_label(a)b=k_means(a,cc)print('b=',b)
index 0.804055967401
index 0.805649685362
index 0.65899543985
index 0.517110170591
aa= Counter({0: 5, 1: 3, 2: 2})
index_max= 0
centroids= [[ 160.8][ 610. ][ 824.5]]
b= 160.8
總結(jié)
以上是生活随笔為你收集整理的聚类算法 sklearn k_means (返回一维数据的最优聚类)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 勾股数列
- 下一篇: Ubuntu 安装redis deskt