当前位置: 代码迷 >> 综合 >> 【scikit-learn】机器学习(八)——K-means聚类
  详细解决方案

【scikit-learn】机器学习(八)——K-means聚类

热度:34   发布时间:2023-12-16 22:50:55.0

数据描述


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])

结果输出



  相关解决方案