K-means聚类 —— matlab
目錄
1.簡介
2.算法原理
3.實(shí)例分析
3.1 讀取數(shù)據(jù)
3.2?原理推導(dǎo)K均值過程
3.3 自帶kmeans函數(shù)求解過程
完整代碼
1.簡介
????????聚類是一個(gè)將數(shù)據(jù)集中在某些方面相似的數(shù)據(jù)成員進(jìn)行分類組織的過程,聚類就是一種發(fā)現(xiàn)這種內(nèi)在結(jié)構(gòu)的技術(shù),聚類技術(shù)經(jīng)常被稱為無監(jiān)督學(xué)習(xí)。
????????K均值聚類是最著名的劃分聚類算法,由于簡潔和效率使得他成為所有聚類算法中最廣泛使用的。給定一個(gè)數(shù)據(jù)點(diǎn)集合和需要的聚類數(shù)目K,K由用戶指定,K均值算法根據(jù)某個(gè)距離函數(shù)反復(fù)把數(shù)據(jù)分入K個(gè)聚類中。
2.算法原理
????????K-means算法是典型的基于距離的聚類算法,采用距離作為相似性的評(píng)價(jià)指標(biāo),即認(rèn)為兩個(gè)對象的距離越近,其相似度就越大。該算法認(rèn)為簇是由距離靠近的對象組成的,因此把得到緊湊且獨(dú)立的簇作為最終目標(biāo)。
K-mean算法步驟如下:
(1)隨機(jī)選取K個(gè)樣本為中?
(2)分別計(jì)算所有樣本到隨機(jī)選取的K個(gè)中?的距離
(3)樣本離哪個(gè)中?近就被分到哪個(gè)中?
(4)計(jì)算各個(gè)中?樣本的均值(最簡單的?法就是求樣本每個(gè)維度的平均值)作為新的中心
(5)重復(fù)(2)(3)(4)直到新的中?和原來的中?基本不變化的時(shí)候,算法結(jié)束
3.實(shí)例分析
數(shù)據(jù)來源于:統(tǒng)計(jì)年鑒
從數(shù)據(jù)中,我們可以看到,實(shí)際數(shù)據(jù)是被分為三類的。
3.1 讀取數(shù)據(jù)
data=xlsread('D:\桌面\kmeans.xlsx')返回:
在這里我們看到,xlsread讀取數(shù)據(jù)時(shí)沒有讀取變量名,但序號(hào)也被加進(jìn)去了,接下來我們需要將其剔除
data=data(:,2:7)返回:
3.2?原理推導(dǎo)K均值過程
%% 原理推導(dǎo)K均值 [m,n]=size(data); %讀取數(shù)據(jù)的行數(shù)與列數(shù) cluster_num=3; %自定義分類數(shù) cluster=data(randperm(m,cluster_num),:); epoch_max=1000;%最大次數(shù) therad_lim=0.001;%中心變化閾值 epoch_num=0; while(epoch_num<epoch_max)epoch_num=epoch_num+1;for i=1:cluster_numdistance=(data-repmat(cluster(i,:),m,1)).^2;distance1(:,i)=sqrt(sum(distance'));end[~,index_cluster]=min(distance1');for j=1:cluster_numcluster_new(j,:)=mean(data(find(index_cluster==j),:));endif (sqrt(sum((cluster_new-cluster).^2))>therad_lim)cluster=cluster_new;elsebreak;end end %% 畫出聚類效果 figure(2) subplot(2,1,1) a=unique(index_cluster); %找出分類出的個(gè)數(shù) C=cell(1,length(a)); for i=1:length(a)C(1,i)={find(index_cluster==a(i))}; end for j=1:cluster_numdata_get=data(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,'filled','MarkerFaceAlpha',.6,'MarkerEdgeAlpha',.9);hold on end plot(cluster(:,1),cluster(:,2),'kd','LineWidth',2); hold on sc_t=mean(silhouette(data,index_cluster')); title_str=['原理推導(dǎo)K均值聚類',' 聚類數(shù)為:',num2str(cluster_num),' SC輪廓系數(shù):',num2str(sc_t)]; title(title_str)返回:
3.3 自帶kmeans函數(shù)求解過程
%% MATLAB自帶kmeans函數(shù) subplot(2,1,2) %畫子圖,在這里是一圖上可畫兩張子圖 cluster_num=3; %自定義分類數(shù) [index_km,center_km]=kmeans(data,cluster_num) ;%MATLAB自帶kmeans函數(shù) a=unique(index_km); %找出分類出的個(gè)數(shù) C=cell(1,length(a)); for i=1:length(a)C(1,i)={find(index_km==a(i))}; end for j=1:cluster_numdata_get=data(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,'filled','MarkerFaceAlpha',.6,'MarkerEdgeAlpha',.9);hold on end plot(center_km(:,1),center_km(:,2),'kd','LineWidth',2); hold on sc_k=mean(silhouette(data,index_km)); title_str1=['MATLAB自帶kmeans函數(shù)',' 聚類數(shù)為:',num2str(cluster_num),' SC輪廓系數(shù):',num2str(sc_k)]; title(title_str1)返回結(jié)果如下:
完整代碼
clear;clc; data=xlsread('D:\桌面\kmeans.xlsx') data=data(:,2:7) %% 原理推導(dǎo)K均值 [m,n]=size(data); %讀取數(shù)據(jù)的行數(shù)與列數(shù) cluster_num=3; %自定義分類數(shù) cluster=data(randperm(m,cluster_num),:); epoch_max=1000;%最大次數(shù) therad_lim=0.001;%中心變化閾值 epoch_num=0; while(epoch_num<epoch_max)epoch_num=epoch_num+1;for i=1:cluster_numdistance=(data-repmat(cluster(i,:),m,1)).^2;distance1(:,i)=sqrt(sum(distance'));end[~,index_cluster]=min(distance1');for j=1:cluster_numcluster_new(j,:)=mean(data(find(index_cluster==j),:));endif (sqrt(sum((cluster_new-cluster).^2))>therad_lim)cluster=cluster_new;elsebreak;end end %% 畫出聚類效果 figure subplot(2,1,1) %畫子圖,在這里是一圖上可畫兩張子圖 a=unique(index_cluster); %找出分類出的個(gè)數(shù) C=cell(1,length(a)); for i=1:length(a)C(1,i)={find(index_cluster==a(i))}; end for j=1:cluster_numdata_get=data(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,'filled','MarkerFaceAlpha',.6,'MarkerEdgeAlpha',.9);hold on end plot(cluster(:,1),cluster(:,2),'kd','LineWidth',2); hold on sc_t=mean(silhouette(data,index_cluster')); title_str=['原理推導(dǎo)K均值聚類',' 聚類數(shù)為:',num2str(cluster_num),' SC輪廓系數(shù):',num2str(sc_t)]; title(title_str)%% MATLAB自帶kmeans函數(shù) subplot(2,1,2) %畫子圖,在這里是一圖上可畫兩張子圖 cluster_num=3; %自定義分類數(shù) [index_km,center_km]=kmeans(data,cluster_num) ;%MATLAB自帶kmeans函數(shù) a=unique(index_km); %找出分類出的個(gè)數(shù) C=cell(1,length(a)); for i=1:length(a)C(1,i)={find(index_km==a(i))}; end for j=1:cluster_numdata_get=data(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,'filled','MarkerFaceAlpha',.6,'MarkerEdgeAlpha',.9);hold on end plot(center_km(:,1),center_km(:,2),'kd','LineWidth',2); hold on sc_k=mean(silhouette(data,index_km)); title_str1=['MATLAB自帶kmeans函數(shù)',' 聚類數(shù)為:',num2str(cluster_num),' SC輪廓系數(shù):',num2str(sc_k)]; title(title_str1)返回:
每次返回結(jié)果也不盡相同,原理推導(dǎo)的和自帶的函數(shù)的求解結(jié)果也相差不是很大,但與原始數(shù)據(jù)的分類相比較,還是有一定差距
總結(jié)
以上是生活随笔為你收集整理的K-means聚类 —— matlab的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7配置mysql其他机器访问
- 下一篇: java注释的理解,java注解原理——