獲取聚類結果中每一類的數(shù)據(jù),該數(shù)據(jù)類型是DataFrame
思路:獲取clf_KMeans的標簽,我這里是聚三類,標簽就是0,1,2
將Label轉成Series類型,再篩選出指定標簽的res0,我篩選了1
最后在DataFrame里獲取Label為1的數(shù)據(jù)
import pandas as pd
from sklearn.cluster import KMeans
# 建立模型。n_clusters參數(shù)用來設置分類個數(shù),即K值,這里表示將樣本分為兩類。
clf_KMeans = KMeans(n_clusters=3, max_iter=10)
# 模型訓練。得到預測值。
print "clf_KMeans聚類中心\n", (clf_KMeans.cluster_centers_)
quantity = pd.Series(clf_KMeans.labels_).value_counts()
print "cluster2聚類數(shù)量\n", (quantity)
#獲取聚類之后每個聚類中心的數(shù)據(jù)
res0Series = pd.Series(clf_KMeans.labels_)
res0 = res0Series[res0Series.values == 1]
print"類別為1的數(shù)據(jù)\n",(df.iloc[res0.index])
另外一種方法,更簡潔
res = dataframe[(clf_KMeans.labels_ == 1)]








暫無數(shù)據(jù)