導航:首頁 > 源碼編譯 > kmeans聚類演算法matlab

kmeans聚類演算法matlab

發布時間:2023-03-15 11:03:39

① matlab 聚類演算法silhouette

~的意思的無視這個項,僅生成h。

snapnaw,拍攝圖像快照以包括在發布文檔中。代碼中沒有涉及發布文檔,所以沒有顯示。

參考網頁網頁鏈接

② 怎麼用Matlab計算聚類演算法的正確率問題

我把K-mediods的matlab代碼貼出來,你好好學習一下
function label = kmedoids( data,k,start_data )
% kmedoids k中心點演算法函數
% data 待聚類的數據集,每一行是一個樣本數據點
% k 聚類個數
% start_data 聚類初始中心值,每一行為一個中心點,有cluster_n行
% class_idx 聚類結果,每個樣本點標記的類別
% 初始化變數
n = length(data);
dist_temp1 = zeros(n,k);
dist_temp2 = zeros(n,k);
last = zeros(n,1);
a = 0;
b = 0;
if nargin==3
centroid = start_data;
else
centroid = data(randsample(n,k),:);
end
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
while any(label~=last)
for a = 1:k
temp2 = ones(numel(data(label==a)),1);
temp3 = data(label==a);
for b = 1:n
temp4 = temp2*data(b,:);
temp5 = sum((temp3-temp4).^2,2);
dist_temp2(b,a) = sum(temp5,1);
end
end
[~,centry_indx] = min(dist_temp2,[],1);
last = label;
centroid = data(centry_indx,:);
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
end
end

③ 怎樣用matlab作聚類分析啊求操作T_T T_T

展示如何使用MATLAB進行聚類分析
分別運用分層聚類、K均值聚類以及高斯混合模型來進行分析,然後比較三者的結果
生成隨機二維分布圖形,三個中心
% 使用高斯分布(正態分布)
% 隨機生成3個中心以及標准差
s = rng(5,'v5normal');
mu = round((rand(3,2)-0.5)*19)+1;
sigma = round(rand(3,2)*40)/10+1;
X = [mvnrnd(mu(1,:),sigma(1,:),200); ...
mvnrnd(mu(2,:),sigma(2,:),300); ...
mvnrnd(mu(3,:),sigma(3,:),400)];
% 作圖
P1 = figure;clf;
scatter(X(:,1),X(:,2),10,'ro');
title('研究樣本散點分布圖')

K均值聚類
% 距離用傳統歐式距離,分成兩類
[cidx2,cmeans2,sumd2,D2] = kmeans(X,2,'dist','sqEuclidean');
P2 = figure;clf;
[silh2,h2] = silhouette(X,cidx2,'sqeuclidean');
從輪廓圖上面看,第二類結果比較好,但是第一類有部分數據表現不佳。有相當部分的點落在0.8以下。

分層聚類

eucD = pdist(X,'euclidean');
clustTreeEuc = linkage(eucD,'average');
cophenet(clustTreeEuc,eucD);
P3 = figure;clf;
[h,nodes] = dendrogram(clustTreeEuc,20);
set(gca,'TickDir','out','TickLength',[.002 0],'XTickLabel',[]);

可以選擇dendrogram顯示的結點數目,這里選擇20 。結果顯示可能可以分成三類

重新調用K均值法
改為分成三類
[cidx3,cmeans3,sumd3,D3] = kmeans(X,3,'dist','sqEuclidean');
P4 = figure;clf;
[silh3,h3] = silhouette(X,cidx3,'sqeuclidean');

圖上看,比前面的結果略有改善。

將分類的結果展示出來
P5 = figure;clf
ptsymb = {'bo','ro','go',',mo','c+'};
MarkFace = {[0 0 1],[.8 0 0],[0 .5 0]};
hold on
for i =1:3
clust = find(cidx3 == i);
plot(X(clust,1),X(clust,2),ptsymb{i},'MarkerSize',3,'MarkerFace',MarkFace{i},'MarkerEdgeColor','black');
plot(cmeans3(i,1),cmeans3(i,2),ptsymb{i},'MarkerSize',10,'MarkerFace',MarkFace{i});
end
hold off

運用高斯混合分布模型進行聚類分析
分別用分布圖、熱能圖和概率圖展示結果 等高線

% 等高線
options = statset('Display','off');
gm = gmdistribution.fit(X,3,'Options',options);
P6 = figure;clf
scatter(X(:,1),X(:,2),10,'ro');
hold on
ezcontour(@(x,y) pdf(gm,[x,y]),[-15 15],[-15 10]);
hold off
P7 = figure;clf
scatter(X(:,1),X(:,2),10,'ro');
hold on
ezsurf(@(x,y) pdf(gm,[x,y]),[-15 15],[-15 10]);
hold off
view(33,24)

熱能圖
cluster1 = (cidx3 == 1);
cluster3 = (cidx3 == 2);
% 通過觀察,K均值方法的第二類是gm的第三類
cluster2 = (cidx3 == 3);
% 計算分類概率
P = posterior(gm,X);
P8 = figure;clf
plot3(X(cluster1,1),X(cluster1,2),P(cluster1,1),'r.')
grid on;hold on
plot3(X(cluster2,1),X(cluster2,2),P(cluster2,2),'bo')
plot3(X(cluster3,1),X(cluster3,2),P(cluster3,3),'g*')
legend('第 1 類','第 2 類','第 3 類','Location','NW')
clrmap = jet(80); colormap(clrmap(9:72,:))
ylabel(colorbar,'Component 1 Posterior Probability')
view(-45,20);
% 第三類點部分概率值較低,可能需要其他數據來進行分析。

% 概率圖
P9 = figure;clf
[~,order] = sort(P(:,1));
plot(1:size(X,1),P(order,1),'r-',1:size(X,1),P(order,2),'b-',1:size(X,1),P(order,3),'y-');
legend({'Cluster 1 Score' 'Cluster 2 Score' 'Cluster 3 Score'},'location','NW');
ylabel('Cluster Membership Score');
xlabel('Point Ranking');

通過AIC准則尋找最優的分類數
高斯混合模型法的最大好處是給出分類好壞的標准
AIC = zeros(1,4);
NlogL = AIC;
GM = cell(1,4);
for k = 1:4
GM{k} = gmdistribution.fit(X,k);
AIC(k)= GM{k}.AIC;
NlogL(k) = GM{k}.NlogL;
end
[minAIC,numComponents] = min(AIC);
按AIC准則給出的最優分類數為: 3 對應的AIC值為: 8647.63

後記
(1)pluskid指出K均值演算法的初值對結果很重要,但是在運行時還沒有發現類似的結果。也許Mathworks對該演算法進行過優化。有時間會仔細研究下代碼,將結果放上來。
分享:

56
喜歡
4
贈金筆
閱讀(21209)┊ 評論 (4)┊ 收藏(1) ┊轉載原文 ┊ 喜歡▼ ┊列印┊舉報

前一篇:[轉載]拉普拉斯矩陣
後一篇:[轉載]用matlab做聚類分析

④ 怎樣用matlab實現多維K-means聚類演算法

直接用kmeans函數。。。
idx = kmeans(X,k)
idx = kmeans(X,k,Name,Value)
[idx,C] = kmeans(___)
[idx,C,sumd] = kmeans(___)
[idx,C,sumd,D] = kmeans(___)
idx = kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, and returns an n-by-1 vector (idx) containing cluster indices of each observation. Rows of X correspond to points and columns correspond to variables.
By default, kmeans uses the squared Euclidean distance measure and the k-means++ algorithm for cluster center initialization.
example
idx = kmeans(X,k,Name,Value) returns the cluster indices with additional options specified by one or more Name,Value pair arguments.
For example, specify the cosine distance, the number of times to repeat the clustering using new initial values, or to use parallel computing.
example
[idx,C] = kmeans(___) returns the k cluster centroid locations in the k-by-p matrix C.
example
[idx,C,sumd] = kmeans(___) returns the within-cluster sums of point-to-centroid distances in the k-by-1 vector sumd.
example
[idx,C,sumd,D] = kmeans(___) returns distances from each point to every centroid in the n-by-k matrix D.

⑤ Matlab FCM聚類和kmeans聚類有什麼區別

K均值聚類演算法即是HCM(普通硬-C均值聚類演算法),它是一種硬性劃分的方法,結果要麼是1要麼是0,沒有其他情況,具有「非此即彼」的性質。裡面的隸屬度矩陣是U。
FCM是把HCM演算法推廣到模糊情形,用在模糊性的分類問題上,給了隸屬度一個權重。隸屬度矩陣用U的m次方表示。

閱讀全文

與kmeans聚類演算法matlab相關的資料

熱點內容
如何改變appstore的語言 瀏覽:458
javahtmlxml 瀏覽:26
單片機啟動文件 瀏覽:805
橙app如何開啟聊天 瀏覽:899
訪問伺服器公網地址 瀏覽:666
pdf列印底色去掉 瀏覽:463
java快遞介面 瀏覽:397
哪個app可以教新爸爸 瀏覽:210
如何查看伺服器系統版本信息 瀏覽:524
成都市土地出讓金演算法 瀏覽:702
鋼筋加密標記 瀏覽:576
ps中擴展功能在文件夾的什麼位置 瀏覽:904
雙極壓縮機為什麼要先高壓 瀏覽:527
蘋果手機伺服器填什麼 瀏覽:832
android移動動畫效果 瀏覽:691
電子和伺服器是什麼意思 瀏覽:692
phpurl中文亂碼問題 瀏覽:893
程序員那麼可愛大結局陸漓產子 瀏覽:538
java如何從雲伺服器讀取本地文件 瀏覽:924
壓縮空氣軟管製作方法 瀏覽:913