K-Means聚类代码实现

#encoding:utf-8
import numpy as np
from sklearn.cluster import KMeans

def LoadData(filePath):
    f=open(filePath,'r+')
    lines=f.readlines()
    retCityName=[]
    retData=[]
    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 retCityName,retData

if  __name__=='__main__':
    CityName,Data=LoadData('city.txt')

    km=KMeans(n_clusters=3)
    label=km.fit_predict(Data)
    # 横向计算每个城市的总开销,并把它归类到相应的簇里面。然后对每个簇进行求平均数。得到expense[i]i从0到3
    expenses=np.sum(km.cluster_centers_,axis=1)
    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])

你可能感兴趣的:(Algorithm)