1. 程式人生 > >K-mean聚類應用:城市消費水平分類

K-mean聚類應用:城市消費水平分類

import numpy as np
from sklearn.cluster import KMeans

if__name__=='__main__':
    data,cityName=loadData('city.txt')
    km=KMeans(n_clusters=3)#定義聚類中心的數目為3的KMeans方法
    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]) def 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,retCityName#返回城市的名稱和各項消費資訊。