K-Means算法-Python实现

本文详细介绍了K-means聚类算法的实现过程,包括数据加载、计算两个向量间的欧式距离、构建簇的质心及算法的具体步骤。通过Python代码实现了K-means算法,并解释了如何使用该算法进行数据点的聚类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# !/usr/bin/env python
# -*-coding:utf-8-*-
# Author:TG

from numpy import *

# 从文件加载数据
def loadDataSet(fileName):
    dataMat = []  # assume last column is target value
    fr = open(fileName)
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        fltLine = map(float, curLine)  # map all elements to float()
        dataMat.append(fltLine)
    return dataMat


# 计算两个向量的欧式距离
def distEclud(vecA, vecB):
    return sqrt(sum(power(vecA - vecB, 2)))


# 构建簇的质心
def ranCent(dataSet, k):
    n = shape(dataSet)[1]
    centorids = mat(zeros((k, n)))  # create centorid mat
    for j in range(n):  # create random cluster centers, within bounds of each dimension
        minJ = min(dataSet[:, j])
        rangeJ = float(max(dataSet[:, j]) - minJ)
        centorids[:, j] = mat(minJ + rangeJ * random.rand(k, 1))
    return centorids


# K-均值聚类算法
def kMeans(dataSet, k, distMeas=distEclud, createCent=ranCent):
    m = shape(dataSet)[0]
    # create mat to assign data points
    clusterAssment = mat(zeros((m, 2)))
    # to a centorid, also holds SE of each point
    # 创建K个点作为质心
    centorids = createCent(dataSet, k)
    clusterChanged = True
    # 对任意一个点的簇分配结果发生改变时
    while clusterChanged:
        clusterChanged = False
        # 对数据集中的每个数据点
        for i in range(m):  # for each data point assign it to the closest centorid
            minDist = inf;
            minIndex = -1
            # 对每个质心
            for j in range(k):
                # 计算距离
                distJI = distMeas(centorids[j, :], dataSet[i, :])
                # 将数据点分配到和其距离最近的簇
                if distJI < minDist:
                    minDist = distJI;
                    minIndex = j
            # 重新计算质心
            if clusterAssment[i, 0] != minIndex: clusterChanged = True
            clusterAssment[i, :] = minIndex, minDist ** 2
        print centorids
        for cent in range(k):  # recalculate centroids
            # get all the point in this cluster
            ptsInClust = dataSet[nonzero(clusterAssment[:, 0].A == cent)[0]]
            # assign centorid to mean
            centorids[cent, :] = mean(ptsInClust, axis=0)
    return centorids, clusterAssment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值