于涂涂 2021-11-08 09:09 采纳率: 100%
浏览 188
已结题

Kmeans聚类算法应用问题,八维数据的分类

这是我看到别人写的根据消费水平划分城市的代码,我理解K-means的作用应该是根据数据八个维度的表现,去划分出4个比较相似的类,但是这个作者在代码中指定了np.sum(),是按照总分高低去进行划分了吗?而且用np.sum()和np.average()的分类结果并不一样,如果np.average()是按平均分高低去划分,那python里的k-means应用解决不就只是计算量问题了吗?如何实现自动迭代聚类呢?

原始数据:
北京,2959.19,730.79,749.41,513.34,467.87,1141.82,478.42,457.64
天津,2459.77,495.47,697.33,302.87,284.19,735.97,570.84,305.08
河北,1495.63,515.90,362.37,285.32,272.95,540.58,364.91,188.63
山西,1406.33,477.77,290.15,208.57,201.50,414.72,281.84,212.10
内蒙古,1303.97,524.29,254.83,192.17,249.81,463.09,287.87,192.96
辽宁,1730.84,553.90,246.91,279.81,239.18,445.20,330.24,163.86
吉林,1561.86,492.42,200.49,218.36,220.69,459.62,360.48,147.76
黑龙江,1410.11,510.71,211.88,277.11,224.65,376.82,317.61,152.85
上海,3712.31,550.74,893.37,346.93,527.00,1034.98,720.33,462.03
江苏,2207.58,449.37,572.40,211.92,302.09,585.23,429.77,252.54
浙江,2629.16,557.32,689.73,435.69,514.66,795.87,575.76,323.36
安徽,1844.78,430.29,271.28,126.33,250.56,513.18,314.00,151.39
福建,2709.46,428.11,334.12,160.77,405.14,461.67,535.13,232.29
江西,1563.78,303.65,233.81,107.90,209.70,393.99,509.39,160.12
山东,1675.75,613.32,550.71,219.79,272.59,599.43,371.62,211.84
河南,1427.65,431.79,288.55,208.14,217.00,337.76,421.31,165.32
湖南,1942.23,512.27,401.39,206.06,321.29,697.22,492.60,226.45
湖北,1783.43,511.88,282.84,201.01,237.60,617.74,523.52,182.52
广东,3055.17,353.23,564.56,356.27,811.88,873.06,1082.82,420.81
广西,2033.87,300.82,338.65,157.78,329.06,621.74,587.02,218.27
海南,2057.86,186.44,202.72,171.79,329.65,477.17,312.93,279.19
重庆,2303.29,589.99,516.21,236.55,403.92,730.05,438.41,225.80
四川,1974.28,507.76,344.79,203.21,240.24,575.10,430.36,223.46
贵州,1673.82,437.75,461.61,153.32,254.66,445.59,346.11,191.48
云南,2194.25,537.01,369.07,249.54,290.84,561.91,407.70,330.95
西藏,2646.61,839.70,204.44,209.11,379.30,371.04,269.59,389.33
陕西,1472.95,390.89,447.95,259.51,230.61,490.90,469.10,191.34
甘肃,1525.57,472.98,328.90,219.86,206.65,449.69,249.66,228.19
青海,1654.69,437.77,258.78,303.00,244.93,479.53,288.56,236.51
宁夏,1375.46,480.89,273.84,317.32,251.08,424.75,228.73,195.93
新疆,1608.82,536.05,432.46,235.82,250.28,541.30,344.85,214.40

版权声明:本文为CSDN博主「L_imbo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:


# 31省市居民家庭消费调查
# 1.建立工程,导入sklearn相关包
import numpy as np
from sklearn.cluster import KMeans

# 2.加载数据data,创建K-Means算法实例,并进行训练,获得标签label:
# a.利用loadData方法读取数据
# b.创建实例
# c.调用Kmeans()和fit_predict()方法进行计算

if __name__ == '__main__':

    def loadData(filePath):
        fr = open(filePath, 'r+')  # r+读写打开一个文本文件
        lines = fr.readlines()     # 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

    data, cityName = loadData('city.txt')
    km = KMeans(n_clusters=4)  # n_clusters用于指定聚类中心的个数,init初始聚类中心的初始化方法,max_iter最大的迭代次数,init默认是k-means++ max_iter默认300
    label = km.fit_predict(data)  # 计算簇中心以及为簇分配序号
    expenses = np.sum(km.cluster_centers_, axis=1)  # 平均消费水平
    # print(expenses)
    CityCluster = [[], [], [], []] # 将城市按label分成设定的簇
    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])

  • 写回答

1条回答 默认 最新

  • CSDN专家-kaily 2021-11-08 15:14
    关注

    分类:

    km = KMeans(n_clusters=4)
    km.fit_predict(data)
    

    分完类之后得到了聚类中心,也就是km.cluster_centers_,你可以print出来:

    [[1525.81533333  478.672       322.88266667  232.4         236.41866667
       457.53133333  344.81866667  190.21933333]
     [2549.658       582.118       488.366       268.998       397.442
       618.92        477.946       295.172     ]
     [2004.785       429.48        347.8925      190.955       287.66625
       581.16125     437.2375      233.09625   ]
     [3242.22333333  544.92        735.78        405.51333333  602.25
      1016.62        760.52333333  446.82666667]]
    

    至于之后的np.sum是对聚类中心的8个值进行了求和,axis是用来指定行还是列的,求和的结果是expenses=[3788.758 5678.62 4512.27375 7754.65666667]
    聚类还是按照八个特征来聚类的,分为四类,只不过最后对四个聚类的聚类中心的值消费水平进行求和

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月16日
  • 已采纳回答 11月8日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统