① matlab里的kmeans算法使用案例不理解丘解释
[Idx,C,sumD,D]=Kmeans(data,3,’dist’,’sqEuclidean’,’rep’,4)
等号右边:
kmeans:K-均值聚类
data是你自己的输入数据
3 是你要聚成3类
dist sqEuclidean 这2个参数,表示距离函数为欧式距离。什么是欧式距离自己网络
’rep’,4聚类重复次数4次。因为要反复算直到选出最好的结果,至多反复算4次
等号左边:
Idx 是你聚类的标号
C 是聚类之后质心的位置
sumD是所有点到质心的距离之和
D是每个点与所有质心的距离
比如下面这幅图中,输入数据data就是所有的小点,K-均值聚类输出的结果就是所有的数据被聚为了3类,聚类的标号就是红绿蓝三种颜色,每一类有一个自己的质心(大的点)。
② 使用K-Means 算法进行聚类分析程序
高维求距离呗。自己定义一个距离的概念,一般都用和2维一样的2-norm.
③ 如何编写求K-均值聚类算法的Matlab程序
在聚类分析中,K-均值聚类算法(k-means
algorithm)是无监督分类中的一种基本方法,其也称为C-均值算法,其基本思想是:通过迭代的方法,逐次更新各聚类中心的值,直至得到最好的聚类结果。
假设要把样本集分为c个类别,算法如下:
(1)适当选择c个类的初始中心;
(2)在第k次迭代中,对任意一个样本,求其到c个中心的距离,将该样本归到距离最短的中心所在的类,
(3)利用均值等方法更新该类的中心值;
(4)对于所有的c个聚类中心,如果利用(2)(3)的迭代法更新后,值保持不变,则迭代结束,否则继续迭代。
下面介绍作者编写的一个分两类的程序,可以把其作为函数调用。
%%
function
[samp1,samp2]=kmeans(samp);
作为调用函数时去掉注释符
samp=[11.1506
6.7222
2.3139
5.9018
11.0827
5.7459
13.2174
13.8243
4.8005
0.9370
12.3576];
%样本集
[l0
l]=size(samp);
%%利用均值把样本分为两类,再将每类的均值作为聚类中心
th0=mean(samp);n1=0;n2=0;c1=0.0;c1=double(c1);c2=c1;for
i=1:lif
samp(i)<th0
c1=c1+samp(i);n1=n1+1;elsec2=c2+samp(i);n2=n2+1;endendc1=c1/n1;c2=c2/n2;
%初始聚类中心t=0;cl1=c1;cl2=c2;
c11=c1;c22=c2;
%聚类中心while
t==0samp1=zeros(1,l);
samp2=samp1;n1=1;n2=1;for
i=1:lif
abs(samp(i)-c11)<abs(samp(i)-c22)
samp1(n1)=samp(i);
cl1=cl1+samp(i);n1=n1+1;
c11=cl1/n1;elsesamp2(n2)=samp(i);
cl2=cl2+samp(i);n2=n2+1;
c22=cl2/n2;endendif
c11==c1
&&
c22==c2t=1;endcl1=c11;cl2=c22;
c1=c11;c2=c22;
end
%samp1,samp2为聚类的结果。
初始中心值这里采用均值的办法,也可以根据问题的性质,用经验的方法来确定,或者将样本集随机分成c类,计算每类的均值。
k-均值算法需要事先知道分类的数量,这是其不足之处。
④ 减法聚类如何用python实现
下面是一个k-means聚类算法在python2.7.5上面的具体实现,你需要先安装Numpy和Matplotlib:
from numpy import *
import time
import matplotlib.pyplot as plt
# calculate Euclidean distance
def euclDistance(vector1, vector2):
return sqrt(sum(power(vector2 - vector1, 2)))
# init centroids with random samples
def initCentroids(dataSet, k):
numSamples, dim = dataSet.shape
centroids = zeros((k, dim))
for i in range(k):
index = int(random.uniform(0, numSamples))
centroids[i, :] = dataSet[index, :]
return centroids
# k-means cluster
def kmeans(dataSet, k):
numSamples = dataSet.shape[0]
# first column stores which cluster this sample belongs to,
# second column stores the error between this sample and its centroid
clusterAssment = mat(zeros((numSamples, 2)))
clusterChanged = True
## step 1: init centroids
centroids = initCentroids(dataSet, k)
while clusterChanged:
clusterChanged = False
## for each sample
for i in xrange(numSamples):
minDist = 100000.0
minIndex = 0
## for each centroid
## step 2: find the centroid who is closest
for j in range(k):
distance = euclDistance(centroids[j, :], dataSet[i, :])
if distance < minDist:
minDist = distance
minIndex = j
## step 3: update its cluster
if clusterAssment[i, 0] != minIndex:
clusterChanged = True
clusterAssment[i, :] = minIndex, minDist**2
## step 4: update centroids
for j in range(k):
pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]]
centroids[j, :] = mean(pointsInCluster, axis = 0)
print 'Congratulations, cluster complete!'
return centroids, clusterAssment
# show your cluster only available with 2-D data
def showCluster(dataSet, k, centroids, clusterAssment):
numSamples, dim = dataSet.shape
if dim != 2:
print "Sorry! I can not draw because the dimension of your data is not 2!"
return 1
mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
if k > len(mark):
print "Sorry! Your k is too large! please contact Zouxy"
return 1
# draw all samples
for i in xrange(numSamples):
markIndex = int(clusterAssment[i, 0])
plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])
mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
# draw the centroids
for i in range(k):
plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12)
plt.show()