无监督学习算法K-means算法总结与c++编程实现




Figure 1: K-means algorithm. Training examples are shown as dots, and cluster centroids are shown as crosses. (a) Original dataset. (b) Random initial cluster centroids (in this instance, not chosen to be equal to twotrainingexamples). (c-f) Illustration of running two iterations of k-means. In each iteration, we assign each training example to the closest cluster centroid(shown by “painting” the training examples the same color as the cluster centroid to which is assigned); then we move each cluster centroid to the mean of the points assigned to it. (Best viewed in color.) Images courtesy Michael Jordan.
注意:
问题1:K值选取问题
K的选取通常是我们的目标,也就是说,我们要将这队数据分为几类。因此,是相对明确的。
问题2:初始值的选取问题
初始值的选取对于迭代的结果有较大的影响,选取不当,会出现所有点都归为一类的情况。一个通常的解决方案是:随机选取多组初始值进行分类,选取损失函数最小的分类结果。
编程举例:
将如下三维空间的点进行k-means分类:
[input.txt]
1.0 , 5.7 , 2.8
4.5 , 5.2 , -0.3
-0.9 , 8.1 , 1.4
0.5 , 6.6 , 2.3
3.5 , 4.7 , 0.2
4.7 , 5.9 , -1
5.1 , 8.2 , 0.9
2.1 , 7.4 , 3.0
0.6 , 6.5 , 3.8
在三维空间的图及k-means分类的结果

K-means算法的c++实现代码:
matlab plot code
% *@author:郑海波 zhb931706659@126.com% *http://blog.csdn.net/nuptboyzhb/clear all;clc;close all;x=[1.0 , 5.7 , 2.84.5 , 5.2 , -0.3-0.9 , 8.1 , 1.40.5 , 6.6 , 2.33.5 , 4.7 , 0.24.7 , 5.9 , -15.1 , 6.2 , 0.92.1 , 7.4 , 3.0];subplot(1,2,1);plot3(x(:,1),x(:,2),x(:,3),'o');hold on;U1=[4.5,8.1,2.8];U2=[-0.9,4.7,-0.3];plot3(U1(1),U1(2),U1(3),'+');hold on;plot3(U2(1),U2(2),U2(3),'r+');hold on;title('o--原始数据,+初始的迭代点');x1=[1.0 , 5.7 , 2.8-0.9 , 8.1 , 1.40.5 , 6.6 , 2.32.1 , 7.4 , 3.0];x2=[4.5 , 5.2 , -0.33.5 , 4.7 , 0.24.7 , 5.9 , -15.1 , 6.2 , 0.9];subplot(1,2,2);plot3(x1(:,1),x1(:,2),x1(:,3),'o');hold on;plot3(x2(:,1),x2(:,2),x2(:,3),'ro');hold on;U1=[4.45 5.5 -0.05];U2=[0.675 6.95 2.125];plot3(U1(1),U1(2),U1(3),'r*');hold on;plot3(U2(1),U2(2),U2(3),'*');hold on;title('迭代结果,*为聚类点的中心');未经允许不得用于商业目的