码迷,mamicode.com
首页 > 其他好文 > 详细

聚类-31省市居民家庭消费水平-city

时间:2019-02-11 14:09:56      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:style   http   mic   readlines   float   center   文件名   ===   learn   

===分三类的=====

技术图片

======分四类的========

技术图片

直接写文件名,那么你的那个txt文件应该是和py文件在同一个路径的

 

============code===========

import numpy as np
from sklearn.cluster import KMeans
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))])
    for i in range(1,len(items)):
        return retData,retCityName
if __name__ == ‘__main__‘:
    data,cityName=loadData(‘city.txt‘)
    km = KMeans(n_clusters=3)
    label = km.fit_predict(data)
    expenses = np.sum(km.cluster_centers_,axis=1)
    #print(expense)
    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])
   

聚类-31省市居民家庭消费水平-city

标签:style   http   mic   readlines   float   center   文件名   ===   learn   

原文地址:https://www.cnblogs.com/wanghui626/p/10361631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!