的質心不對應的圖像中的座標,但在功能空間座標。有兩種方法可以測試kmeans的表現。對於這兩種方式,你都希望將這些觀點與他們最接近的羣集聯繫起來。你從kmeans的第一個輸出中獲得這些信息。
(1)可以通過減少6維空間2或3維空間,然後在繪製不同顏色的不同分類的可視化座標的聚類結果。
假設特徵向量被收集在一個名爲featureArray
一個數組,你問nClusters
集羣,你會做的情節使用mdscale轉換數據,比方說如下,3D空間:
%# kmeans clustering
[idx,centroids6D] = kmeans(featureArray,nClusters);
%# find the dissimilarity between features in the array for mdscale.
%# Add the cluster centroids to the points, so that they get transformed by mdscale as well.
%# I assume that you use Euclidean distance.
dissimilarities = pdist([featureArray;centroids6D]);
%# transform onto 3D space
transformedCoords = mdscale(dissimilarities,3);
%# create colormap with nClusters colors
cmap = hsv(nClusters);
%# loop to plot
figure
hold on,
for c = 1:nClusters
%# plot the coordinates
currentIdx = find(idx==c);
plot3(transformedCoords(currentIdx,1),transformedCoords(currentIdx,2),...
transformedCoords(currentIdx,3),'.','Color',cmap(c,:));
%# plot the cluster centroid with a black-edged square
plot3(transformedCoords(1:end-nClusters+c,1),transformedCoords(1:end-nClusters+c,2),...
transformedCoords(1:end-nClusters+c,3),'s','MarkerFaceColor',cmap(c,:),...
MarkerEdgeColor','k');
end
(2)你可以,或者創建一個僞彩色圖像,顯示你,你寫這集羣
假設你有nRows
通過nCols
塊
什麼形象的一部分,屬於3210
%# kmeans clustering
[idx,centroids6D] = kmeans(featureArray,nClusters);
%# create image
img = reshape(idx,nRows,nCols);
%# create colormap
cmap = hsv(nClusters);
%# show the image and color according to clusters
figure
imshow(img,[])
colormap(cmap)