手动实现kmenas算法

# 数据初始化
import numpy as np
import random
import re
import matplotlib.pyplot as plt


def calcuDistance(vec1, vec2):
    # 计算向量1与向量2之间的欧式距离
    return np.sqrt(np.sum(np.square(vec1 - vec2)))  # 注意这里的减号


def showCluster(centroidList, clusterDict):
    # 展示聚类结果
    colorMark = ['or', 'ob', 'og', 'ok', 'oy', 'ow']  # 不同簇类标记,o表示圆形,另一个表示颜色
    centroidMark = ['dr', 'db', 'dg', 'dk', 'dy', 'dw']

    for key in clusterDict.keys():
        plt.plot(centroidList[key][0], centroidList[key][1], centroidMark[key], markersize=12)  # 质心点
        for item in clusterDict[key]:
            plt.plot(item[0], item[1], colorMark[key])
    plt.show()


def loadDataSet():
    dataSet = np.loadtxt("dataSet.csv")
    return dataSet


# 1.随机选取k个点作为初始质心。
def initCentroids(dataSet, k):
    # 从数据集中随机选取k个数据返回
    dataSet = list(dataSet)
    return random.sample(dataSet, k)


# 2.对于样本中每一个点,分别求与k点的距离。距离最小者就属于该类。
def minDistance(dataSet, centroidList):
    # 对每个属于dataSet的item, 计算item与centroidList中k个质心的距离,找出距离最小的,并将item加入相应的簇类中
    clusterDict = dict()  # dict保存簇类结果
    k = len(centroidList)
    for item in dataSet:
        vec1 = item
        flag = -1
        minDis = float("inf")  # 初始化为最大值
        for i in range(k):
            vec2 = centroidList[i]
            distance = calcuDistance(vec1, vec2)  # error
            if distance < minDis:
                minDis = distance
                flag = i  # 循环结束时, flag保存与当前item最近的蔟标记
        if flag not in clusterDict.keys():
            clusterDict.setdefault(flag, [])
        clusterDict[flag].append(item)  # 加入相应的类别中
    return clusterDict  # 不同的类别


# 3.此时对得到的k各类,重新计算新的质心。
def getCentroids(clusterDict):
    # 重新计算k个质心
    centroidList = []
    for key in clusterDict.keys():
        centroid = np.mean(clusterDict[key], axis=0)
        centroidList.append(centroid)
    return centroidList  # 得到新的质心


# 4.计算计算各蔟集合间的均方误差,来衡量聚类的效果
def getVar(centroidList, clusterDict):
    # 计算各蔟集合间的均方误差
    # 将蔟类中各个向量与质心的距离累加求和
    sum = 0.0
    for key in clusterDict.keys():
        vec1 = centroidList[key]
        distance = 0.0
        for item in clusterDict[key]:
            vec2 = item
            distance += calcuDistance(vec1, vec2)
        sum += distance
    return sum


# 测试聚类效果,并可视化
def show_k_means():
    dataSet = loadDataSet()
    # [[ 1.658985  4.285136], [-3.453687  3.424321], [ 4.838138 -1.151539], [-5.379713 -3.362104], [ 0.972564  2.924086], [-3.567919  1.531611], [ 0.450614 -3.302219], [-3.487105 -1.724432], [ 2.668759  1.594842], [-3.156485  3.191137], [ 3.165506 -3.999838], [-2.786837 -3.099354], [ 4.208187  2.984927], [-2.123337  2.943366], [ 0.704199 -0.479481], [-0.39237  -3.963704], [ 2.831667  1.574018], [-0.790153  3.343144], [ 2.943496 -3.357075], [-3.195883 -2.283926], [ 2.336445  2.875106], [-1.786345  2.554248], [ 2.190101 -1.90602 ], [-3.403367 -2.778288], [ 1.778124  3.880832], [-1.688346  2.230267], [ 2.592976 -2.054368], [-4.007257 -3.207066], [ 2.257734  3.387564], [-2.679011  0.785119], [ 0.939512 -4.023563], [-3.674424 -2.261084], [ 2.046259  2.735279], [-3.18947   1.780269], [ 4.372646 -0.822248], [-2.579316 -3.497576], [ 1.889034  5.1904  ], [-0.798747  2.185588], [ 2.83652  -2.658556], [-3.837877 -3.253815], [ 2.096701  3.886007], [-2.709034  2.923887], [ 3.367037 -3.184789], [-2.121479...
    centroidList = initCentroids(dataSet, 4)
    clusterDict = minDistance(dataSet, centroidList)
    # # getCentroids(clusterDict)
    # showCluster(centroidList, clusterDict)
    newVar = getVar(centroidList, clusterDict)
    oldVar = 1  # 当两次聚类的误差小于某个值是,说明质心基本确定。

    times = 2
    while abs(newVar - oldVar) >= 0.00001:
        centroidList = getCentroids(clusterDict)
        clusterDict = minDistance(dataSet, centroidList)
        oldVar = newVar
        newVar = getVar(centroidList, clusterDict)
        times += 1
        showCluster(centroidList, clusterDict)


if __name__ == '__main__':
    show_k_means()

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页