K-Means 概念定义:

K-Means 是一种基于距离的排他的聚类划分方法。

上面的 K-Means 描述中包含了几个概念:

  • 聚类(Clustering):K-Means 是一种聚类分析(Cluster Analysis)方法。聚类就是将数据对象分组成为多个类或者簇 (Cluster),使得在同一个簇中的对象之间具有较高的相似度,而不同簇中的对象差别较大。
  • 划分(Partitioning):聚类可以基于划分,也可以基于分层。划分即将对象划分成不同的簇,而分层是将对象分等级。
  • 排他(Exclusive):对于一个数据对象,只能被划分到一个簇中。如果一个数据对象可以被划分到多个簇中,则称为可重叠的(Overlapping)。
  • 距离(Distance):基于距离的聚类是将距离近的相似的对象聚在一起。基于概率分布模型的聚类是在一组对象中,找到能符合特定分布模型的对象的集合,他们不一定是距离最近的或者最相似的,而是能完美的呈现出概率分布模型所描述的模型。

K-Means 问题描述:

给定一个 n 个对象的数据集,它可以构建数据的 k 个划分,每个划分就是一个簇,并且 k ≤ n。同时还需满足:

  1. 每个组至少包含一个对象。
  2. 每个对象必须属于且仅属于一个簇。

Simply speaking, K-Means clustering is an algorithm to classify or to group your objects based on attributes/features, into K number of groups. K is a positive integer number. The grouping is done by minimizing the sum of squares of distances between data and the corresponding cluster centroid. Thus, the purpose of K-means clustering is to classify the data.

例如,有如下包含 10 条数据的集合。集合中每项描述了一个人的身高(Height: inches)和体重(Weight: kilograms)。

Height Weight
-------------
(73.0, 72.6)
(61.0, 54.4)
(67.0, 99.9)
(68.0, 97.3)
(62.0, 59.0)
(75.0, 81.6)
(74.0, 77.1)
(66.0, 97.3)
(68.0, 93.3)
(61.0, 59.0)

通过按照身高和体重的聚类,可以将上述 10 条数据分组成 3 类。

Height Weight
-------------
(67.0, 99.9)
(68.0, 97.3)
(66.0, 97.3)
(68.0, 93.3) (73.0, 72.6)
(75.0, 81.6)
(74.0, 77.1) (61.0, 54.4)
(62.0, 59.0)
(61.0, 59.0)

分类结果可以描述为:中等身高并且很重、很高并且中等体重、矮并且轻。如果用图形来观察分组状况则结果一目了然。

K-Means 算法实现:

由于 K-Means 算法值针对给定的完整数据集进行操作,不需要任何特殊的训练数据,所以 K-Means 是一种无监督的机器学习方法(Unsupervised Machine Learning Technique)。

K-Means 算法最常见的实现方式是使用迭代式精化启发法的 Lloyd's algorithm。

  • 给定划分数量 k。创建一个初始划分,从数据集中随机地选择 k 个对象,每个对象初始地代表了一个簇中心(Cluster Centroid)。对于其他对象,计算其与各个簇中心的距离,将它们划入距离最近的簇。
  • 采用迭代的重定位技术,尝试通过对象在划分间移动来改进划分。所谓重定位技术,就是当有新的对象加入簇或者已有对象离开簇的时候,重新计算簇的平均值,然后对对象进行重新分配。这个过程不断重复,直到各簇中对象不再变化为止。
randomly assign all data items to a cluster
loop until no change in cluster assignments
compute centroids for each cluster
reassign each data item to cluster of closest centroid
end

简洁点儿的表述即为:

initialize clustering
loop
update centroids
update clustering
end loop

应用 K-Means 算法到上述身高与体重的示例,聚类过程如下图所示。

K-Means 优缺点:

当结果簇是密集的,而且簇和簇之间的区别比较明显时,K-Means 的效果较好。对于大数据集,K-Means 是相对可伸缩的和高效的,它的复杂度是 O(nkt),n 是对象的个数,k 是簇的数目,t 是迭代的次数,通常 k << n,且 t << n,所以算法经常以局部最优结束。

K-Means 的最大问题是要求先给出 k 的个数。k 的选择一般基于经验值和多次实验结果,对于不同的数据集,k 的取值没有可借鉴性。另外,K-Means 对孤立点数据是敏感的,少量噪声数据就能对平均值造成极大的影响。

Basic K-Means - Lloyd's algorithm C# 代码实现:

Code below referenced from Machine Learning Using C# Succinctly by James McCaffrey, and article K-Means Data Clustering Using C#.

 using System;

 namespace ClusterNumeric
{
class ClusterNumProgram
{
static void Main(string[] args)
{
Console.WriteLine("\nBegin k-means clustering demo\n"); double[][] rawData = new double[][];
rawData[] = new double[] { , 72.6 };
rawData[] = new double[] { , 54.4 };
rawData[] = new double[] { , 99.9 };
rawData[] = new double[] { , 97.3 };
rawData[] = new double[] { , 59.0 };
rawData[] = new double[] { , 81.6 };
rawData[] = new double[] { , 77.1 };
rawData[] = new double[] { , 97.3 };
rawData[] = new double[] { , 93.3 };
rawData[] = new double[] { , 59.0 }; Console.WriteLine("Raw unclustered height (in.) weight (kg.) data:\n");
Console.WriteLine(" ID Height Weight");
Console.WriteLine("---------------------");
ShowData(rawData, , true, true); int numClusters = ;
Console.WriteLine("\nSetting numClusters to " + numClusters); Console.WriteLine("Starting clustering using k-means algorithm");
Clusterer c = new Clusterer(numClusters);
int[] clustering = c.Cluster(rawData);
Console.WriteLine("Clustering complete\n"); Console.WriteLine("Final clustering in internal form:\n");
ShowVector(clustering, true); Console.WriteLine("Raw data by cluster:\n");
Console.WriteLine(" ID Height Weight");
ShowClustered(rawData, clustering, numClusters, ); Console.WriteLine("\nEnd k-means clustering demo\n");
Console.ReadLine();
} static void ShowData(
double[][] data, int decimals,
bool indices, bool newLine)
{
for (int i = ; i < data.Length; ++i)
{
if (indices == true)
Console.Write(i.ToString().PadLeft() + " "); for (int j = ; j < data[i].Length; ++j)
{
double v = data[i][j];
Console.Write(v.ToString("F" + decimals) + " ");
} Console.WriteLine("");
} if (newLine == true)
Console.WriteLine("");
} static void ShowVector(int[] vector, bool newLine)
{
for (int i = ; i < vector.Length; ++i)
Console.Write(vector[i] + " "); if (newLine == true)
Console.WriteLine("\n");
} static void ShowClustered(
double[][] data, int[] clustering,
int numClusters, int decimals)
{
for (int k = ; k < numClusters; ++k)
{
Console.WriteLine("===================");
for (int i = ; i < data.Length; ++i)
{
int clusterID = clustering[i];
if (clusterID != k) continue;
Console.Write(i.ToString().PadLeft() + " ");
for (int j = ; j < data[i].Length; ++j)
{
double v = data[i][j];
Console.Write(v.ToString("F" + decimals) + " ");
}
Console.WriteLine("");
}
Console.WriteLine("===================");
}
}
} public class Clusterer
{
private int numClusters; // number of clusters
private int[] clustering; // index = a tuple, value = cluster ID
private double[][] centroids; // mean (vector) of each cluster
private Random rnd; // for initialization public Clusterer(int numClusters)
{
this.numClusters = numClusters;
this.centroids = new double[numClusters][];
this.rnd = new Random(); // arbitrary seed
} public int[] Cluster(double[][] data)
{
int numTuples = data.Length;
int numValues = data[].Length;
this.clustering = new int[numTuples]; for (int k = ; k < numClusters; ++k) // allocate each centroid
this.centroids[k] = new double[numValues]; InitRandom(data); Console.WriteLine("\nInitial random clustering:");
for (int i = ; i < clustering.Length; ++i)
Console.Write(clustering[i] + " ");
Console.WriteLine("\n"); bool changed = true; // change in clustering?
int maxCount = numTuples * ; // sanity check
int ct = ;
while (changed == true && ct <= maxCount)
{
++ct; // k-means typically converges very quickly
UpdateCentroids(data); // no effect if fail
changed = UpdateClustering(data); // no effect if fail
} int[] result = new int[numTuples];
Array.Copy(this.clustering, result, clustering.Length);
return result;
} private void InitRandom(double[][] data)
{
int numTuples = data.Length; int clusterID = ;
for (int i = ; i < numTuples; ++i)
{
clustering[i] = clusterID++;
if (clusterID == numClusters)
clusterID = ;
}
for (int i = ; i < numTuples; ++i)
{
int r = rnd.Next(i, clustering.Length);
int tmp = clustering[r];
clustering[r] = clustering[i];
clustering[i] = tmp;
}
} private void UpdateCentroids(double[][] data)
{
int[] clusterCounts = new int[numClusters];
for (int i = ; i < data.Length; ++i)
{
int clusterID = clustering[i];
++clusterCounts[clusterID];
} // zero-out this.centroids so it can be used as scratch
for (int k = ; k < centroids.Length; ++k)
for (int j = ; j < centroids[k].Length; ++j)
centroids[k][j] = 0.0; for (int i = ; i < data.Length; ++i)
{
int clusterID = clustering[i];
for (int j = ; j < data[i].Length; ++j)
centroids[clusterID][j] += data[i][j]; // accumulate sum
} for (int k = ; k < centroids.Length; ++k)
for (int j = ; j < centroids[k].Length; ++j)
centroids[k][j] /= clusterCounts[k]; // danger?
} private bool UpdateClustering(double[][] data)
{
// (re)assign each tuple to a cluster (closest centroid)
// returns false if no tuple assignments change OR
// if the reassignment would result in a clustering where
// one or more clusters have no tuples. bool changed = false; // did any tuple change cluster? int[] newClustering = new int[clustering.Length]; // proposed result
Array.Copy(clustering, newClustering, clustering.Length); double[] distances = new double[numClusters]; // from tuple to centroids for (int i = ; i < data.Length; ++i) // walk through each tuple
{
for (int k = ; k < numClusters; ++k)
distances[k] = Distance(data[i], centroids[k]); int newClusterID = MinIndex(distances); // find closest centroid
if (newClusterID != newClustering[i])
{
changed = true; // note a new clustering
newClustering[i] = newClusterID; // accept update
}
} if (changed == false)
return false; // no change so bail // check proposed clustering cluster counts
int[] clusterCounts = new int[numClusters];
for (int i = ; i < data.Length; ++i)
{
int clusterID = newClustering[i];
++clusterCounts[clusterID];
} for (int k = ; k < numClusters; ++k)
if (clusterCounts[k] == )
return false; // bad clustering Array.Copy(newClustering, clustering, newClustering.Length); // update
return true; // good clustering and at least one change
} // Euclidean distance between two vectors for UpdateClustering()
private static double Distance(double[] tuple, double[] centroid)
{
double sumSquaredDiffs = 0.0;
for (int j = ; j < tuple.Length; ++j)
sumSquaredDiffs += (tuple[j] - centroid[j]) * (tuple[j] - centroid[j]);
return Math.Sqrt(sumSquaredDiffs);
} // helper for UpdateClustering() to find closest centroid
private static int MinIndex(double[] distances)
{
int indexOfMin = ;
double smallDist = distances[];
for (int k = ; k < distances.Length; ++k)
{
if (distances[k] < smallDist)
{
smallDist = distances[k];
indexOfMin = k;
}
}
return indexOfMin;
}
}
}

运行结果如下:

参考资料

本篇文章《K-Means 聚类算法》由 Dennis Gao 发表自博客园个人博客,未经作者本人同意禁止以任何的形式转载,任何自动的或人为的爬虫转载行为均为耍流氓。

K-Means 聚类算法的更多相关文章

  1. k均值聚类算法原理和(TensorFlow)实现

    顾名思义,k均值聚类是一种对数据进行聚类的技术,即将数据分割成指定数量的几个类,揭示数据的内在性质及规律. 我们知道,在机器学习中,有三种不同的学习模式:监督学习.无监督学习和强化学习: 监督学习,也 ...

  2. K均值聚类算法

    k均值聚类算法(k-means clustering algorithm)是一种迭代求解的聚类分析算法,其步骤是随机选取K个对象作为初始的聚类中心,然后计算每个对象与各个种子聚类中心之间的距离,把每个 ...

  3. 机器学习实战---K均值聚类算法

    一:一般K均值聚类算法实现 (一)导入数据 import numpy as np import matplotlib.pyplot as plt def loadDataSet(filename): ...

  4. 基于改进人工蜂群算法的K均值聚类算法(附MATLAB版源代码)

    其实一直以来也没有准备在园子里发这样的文章,相对来说,算法改进放在园子里还是会稍稍显得格格不入.但是最近邮箱收到的几封邮件让我觉得有必要通过我的博客把过去做过的东西分享出去更给更多需要的人.从论文刊登 ...

  5. K均值聚类算法的MATLAB实现

    1.K-均值聚类法的概述    之前在参加数学建模的过程中用到过这种聚类方法,但是当时只是简单知道了在matlab中如何调用工具箱进行聚类,并不是特别清楚它的原理.最近因为在学模式识别,又重新接触了这 ...

  6. 聚类之K均值聚类和EM算法

    这篇博客整理K均值聚类的内容,包括: 1.K均值聚类的原理: 2.初始类中心的选择和类别数K的确定: 3.K均值聚类和EM算法.高斯混合模型的关系. 一.K均值聚类的原理 K均值聚类(K-means) ...

  7. [聚类算法] K-means 算法

    聚类 和 k-means简单概括. 聚类是一种 无监督学习 问题,它的目标就是基于 相似度 将相似的子集聚合在一起. k-means算法是聚类分析中使用最广泛的算法之一.它把n个对象根据它们的属性分为 ...

  8. 转载: scikit-learn学习之K-means聚类算法与 Mini Batch K-Means算法

    版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] ================== ...

  9. 转载 | Python AI 教学│k-means聚类算法及应用

    关注我们的公众号哦!获取更多精彩哦! 1.问题导入 假如有这样一种情况,在一天你想去某个城市旅游,这个城市里你想去的有70个地方,现在你只有每一个地方的地址,这个地址列表很长,有70个位置.事先肯定要 ...

  10. FCM聚类算法介绍

    FCM算法是一种基于划分的聚类算法,它的思想就是使得被划分到同一簇的对象之间相似度最大,而不同簇之间的相似度最小.模糊C均值算法是普通C均值算法的改进,普通C均值算法对于数据的划分是硬性的,而FCM则 ...

随机推荐

  1. 【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器

    一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从 ...

  2. ABP文档 - 导航

    文档目录 本节内容: 创建菜单 注册导航供应器 显示菜单 每个web应用都有一些菜单用来在页面/屏幕之间导航,ABP提供了一个通用的基础框架创建并显示菜单给用户. 创建菜单 一个应用可能由不同模块组成 ...

  3. [.NET] C# 知识回顾 - Event 事件

    C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...

  4. SVD奇异值分解的基本原理和运用

    SVD奇异值分解: SVD是一种可靠的正交矩阵分解法.可以把A矩阵分解成U,∑,VT三个矩阵相乘的形式.(Svd(A)=[U*∑*VT],A不必是方阵,U,VT必定是正交阵,S是对角阵<以奇异值 ...

  5. 【NLP】十分钟快览自然语言处理学习总结

    十分钟学习自然语言处理概述 作者:白宁超 2016年9月23日00:24:12 摘要:近来自然语言处理行业发展朝气蓬勃,市场应用广泛.笔者学习以来写了不少文章,文章深度层次不一,今天因为某种需要,将文 ...

  6. 微信小程序(微信应用号)组件讲解

    这篇文章主要讲解微信小程序的组件. 首先,讲解新建项目.现在有句话:招聘三天以上微信小程序开发,这个估计只能去挖微信的工程师了.技术新,既然讲解,那我们就从开始建项目讲解. 打开微信web开发者工具, ...

  7. JVM类加载

    JVM的类加载机制就是:JVM把描述类的class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被JVM直接使用的Java类型 ClassLoader JVM中的ClassLoade ...

  8. java观察者模式

      像activeMQ等消息队列中,我们经常会使用发布订阅模式,但是你有没有想过,客户端时如何及时得到订阅的主题的信息?其实就里就用到了观察者模式.在软件系统中,当一个对象的行为依赖于另一个对象的状态 ...

  9. Activity之概览屏幕(Overview Screen)

    概览屏幕 概览屏幕(也称为最新动态屏幕.最近任务列表或最近使用的应用)是一个系统级别 UI,其中列出了最近访问过的 Activity 和任务. 用户可以浏览该列表并选择要恢复的任务,也可以通过滑动清除 ...

  10. 解决:win10_x64 VMware Workstation and Hyper-V are not compatible. Remove the Hyper-V role from the system before running VMware Workstation

    bcdedit /set hypervisorlaunchtype off A reboot of of the Windows OS is necessary  必须重启才能生效   To enab ...