数据描述
KMeans函数介绍
代码实现
import numpy as np
from sklearn.cluster import KMeansdef loadData(filePath):fr = open(filePath,'r+')lines = fr.readlines()retData = []retCityName = []for line in lines:items = line.strip().split(",")retCityName.append(items[0])retData.append([float(items[i]) for i in range(1,len(items))])return retData,retCityNameif __name__ == '__main__':data,cityName = loadData('city.txt')km = KMeans(n_clusters=4)label = km.fit_predict(data)expenses = np.sum(km.cluster_centers_,axis=1)#print(expenses)CityCluster = [[],[],[],[]]for i in range(len(cityName)):CityCluster[label[i]].append(cityName[i])for i in range(len(CityCluster)):print("Expenses:%.2f" % expenses[i])print(CityCluster[i])
结果输出