2019-07-25【机器学习】无监督学习之聚类 K-Means算法实例 (1999年中国居民消费城市分类)


样本

北京,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

import numpy as np
from sklearn.cluster import KMeans #导入聚类KMean算法

def loadData(filePath):
    fr = open(filePath, ‘r+‘) #以读的方式打开
    lines = fr.readlines()
    retData = []
    retCityName = []
    for line in lines:
        #print(line)
        items = line.strip().split(",") #以,为分割返回列表,strip去除 \n
        #print(items)
        retCityName.append(items[0])#根据数据位置,添加城市名字
        retData.append([float(items[i]) for i in range(1, len(items))])
    return retData, retCityName

if __name__ == ‘__main__‘:
    data, cityName = loadData(‘city.txt‘) #导入数据
    km = KMeans(n_clusters=4) #聚类中心的个数
    label = km.fit_predict(data) #调用算法分标签,分为n_clusters=4类,默认调用欧式空间距离
    #print(label)
    #print(km.cluster_centers_)
    expenses = np.sum(km.cluster_centers_, axis=1) #求和费用
    #print(expenses)
    #print(expenses)
    CityCluster = [[], [], [], []] #二维数组,对应n_clusters=4
    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])

  输出结果

原文地址:https://www.cnblogs.com/ymzm204/p/11247382.html

时间: 2024-10-12 11:45:54

2019-07-25【机器学习】无监督学习之聚类 K-Means算法实例 (1999年中国居民消费城市分类)的相关文章

聚类--K均值算法

聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用    1.用python实现K均值算法 K-means是一个反复迭代的过程,算法分为四个步骤:(x,k,y) import numpy as np x = np.random.randint(1,50,[20,1]) y = np.zeros(20) k = 3 # 选取数据空间中的K个对象作为初始中心,每个对象代表一个聚类中心:def initcenter(x, k): kc def initcenter(x,k)

5.无监督学习-DBSCAN聚类算法及应用

DBSCAN方法及应用 1.DBSCAN密度聚类简介 DBSCAN 算法是一种基于密度的聚类算法: 1.聚类的时候不需要预先指定簇的个数 2.最终的簇的个数不确定DBSCAN算法将数据点分为三类: 1.核心点:在半径Eps内含有超过MinPts数目的点. 2.边界点:在半径Eps内点的数量小于MinPts,但是落在核心点的邻域内的点. 3.噪音点:既不是核心点也不是边界点的点. 如下图所示:图中黄色的点为边界点,因为在半径Eps内,它领域内的点不超过MinPts个,我们这里设置的MinPts为5

聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用

4 用python实现K均值算法 x=np.random.randint(1,100,[20,1]) y=np.zeros(20) k=3 def initcenter(x,k): return x[:k] def nearest(kc,i): d = (abs(kc - i)) w = np.where(d ==np.min(d)) return w [0] [0] kc = initcenter(x,k) nearest(kc,14) for i in range(x.shape[0]):

无监督学习之聚类2——DBSCAN

根据学生月上网时间数据运用DBSCAN算法计算: #coding=utf-8 import numpy as np import sklearn.cluster as skc from sklearn import metrics import matplotlib.pyplot as plt mac2id = dict() onlinetimes = [] f = open('F:\data\TestData.txt', encoding='utf-8') for line in f: mac

无监督学习之聚类1——Kmeans

Kmeans算法分析31省市消费水平 #coding=utf-8 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(",")#以逗号为

【机器学习笔记五】聚类 - k均值聚类

参考资料: [1]Spark Mlib 机器学习实践 [2]机器学习 [3]深入浅出K-means算法  http://www.csdn.net/article/2012-07-03/2807073-k-means 一.概念 K-means聚类是在无监督的情况下,将样本数据进行聚类.以2均值聚类的算法为例: 1.在样本中选择两个初始化中心点: 2.计算所有样本到这两个中心点的距离,并以此为基准将样本分为两类: 3.将中心点移到这类样本的新中心点: 4.重复2.3步骤直到满足要求: K-means

web安全之机器学习入门——3.1 KNN/k近邻算法

目录 sklearn.neighbors.NearestNeighbors 参数/方法 基础用法 用于监督学习 检测异常操作(一) 检测异常操作(二) 检测rootkit 检测webshell sklearn.neighbors.NearestNeighbors 参数: 方法: 基础用法 print(__doc__) from sklearn.neighbors import NearestNeighbors import numpy as np X = np.array([[-1, -1],

《机器学习实战》学习笔记——k近邻算法

1.numpy中一些函数的用法学习 shape()用法: shape : tuple of ints The elements of the shape tuple give the lengths of the corresponding array dimensions.. shape返回一个元组,依次为各维度的长度.shape[0]:第一维长度,shape[1]:第二维长度. tile()用法: numpy.tile(A, reps) Construct an array by repea

Machine Learning Algorithms Study Notes(4)—无监督学习(unsupervised learning)

1    Unsupervised Learning 1.1    k-means clustering algorithm 1.1.1    算法思想 1.1.2    k-means的不足之处 1.1.3    如何选择K值 1.1.4    Spark MLlib 实现 k-means 算法 1.2    Mixture of Gaussians and the EM algorithm 1.3    The EM Algorithm 1.4    Principal Components