① 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()