import pandas as pd from sklearn.cluster import KMeans import matplotlib.pyplot as plt df_features = pd.read_csv(r'11111111.csv',encoding='gbk') # 讀入數據 #print(df_features) '利用SSE選擇k' SSE = [] # 存放每次結果的誤差平方和 for k in range(1,9): estimator = KMeans(n_clusters=k) # 構造聚類器 estimator.fit(df_features[['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32']]) SSE.append(estimator.inertia_) # estimator.inertia_獲取聚類准則的總和 X = range(1,9) plt.xlabel('k') plt.ylabel('SSE') plt.plot(X,SSE,'o-') plt.show()