K-means聚类 —— matlab
目錄
1.簡介
2.算法原理
3.實例分析
3.1 讀取數據
3.2?原理推導K均值過程
3.3 自帶kmeans函數求解過程
完整代碼
1.簡介
????????聚類是一個將數據集中在某些方面相似的數據成員進行分類組織的過程,聚類就是一種發現這種內在結構的技術,聚類技術經常被稱為無監督學習。
????????K均值聚類是最著名的劃分聚類算法,由于簡潔和效率使得他成為所有聚類算法中最廣泛使用的。給定一個數據點集合和需要的聚類數目K,K由用戶指定,K均值算法根據某個距離函數反復把數據分入K個聚類中。
2.算法原理
????????K-means算法是典型的基于距離的聚類算法,采用距離作為相似性的評價指標,即認為兩個對象的距離越近,其相似度就越大。該算法認為簇是由距離靠近的對象組成的,因此把得到緊湊且獨立的簇作為最終目標。
K-mean算法步驟如下:
(1)隨機選取K個樣本為中?
(2)分別計算所有樣本到隨機選取的K個中?的距離
(3)樣本離哪個中?近就被分到哪個中?
(4)計算各個中?樣本的均值(最簡單的?法就是求樣本每個維度的平均值)作為新的中心
(5)重復(2)(3)(4)直到新的中?和原來的中?基本不變化的時候,算法結束
3.實例分析
數據來源于:統計年鑒
從數據中,我們可以看到,實際數據是被分為三類的。
3.1 讀取數據
data=xlsread('D:\桌面\kmeans.xlsx')返回:
在這里我們看到,xlsread讀取數據時沒有讀取變量名,但序號也被加進去了,接下來我們需要將其剔除
data=data(:,2:7)返回:
3.2?原理推導K均值過程
%% 原理推導K均值 [m,n]=size(data); %讀取數據的行數與列數 cluster_num=3; %自定義分類數 cluster=data(randperm(m,cluster_num),:); epoch_max=1000;%最大次數 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); %找出分類出的個數 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=['原理推導K均值聚類',' 聚類數為:',num2str(cluster_num),' SC輪廓系數:',num2str(sc_t)]; title(title_str)返回:
3.3 自帶kmeans函數求解過程
%% MATLAB自帶kmeans函數 subplot(2,1,2) %畫子圖,在這里是一圖上可畫兩張子圖 cluster_num=3; %自定義分類數 [index_km,center_km]=kmeans(data,cluster_num) ;%MATLAB自帶kmeans函數 a=unique(index_km); %找出分類出的個數 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函數',' 聚類數為:',num2str(cluster_num),' SC輪廓系數:',num2str(sc_k)]; title(title_str1)返回結果如下:
完整代碼
clear;clc; data=xlsread('D:\桌面\kmeans.xlsx') data=data(:,2:7) %% 原理推導K均值 [m,n]=size(data); %讀取數據的行數與列數 cluster_num=3; %自定義分類數 cluster=data(randperm(m,cluster_num),:); epoch_max=1000;%最大次數 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); %找出分類出的個數 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=['原理推導K均值聚類',' 聚類數為:',num2str(cluster_num),' SC輪廓系數:',num2str(sc_t)]; title(title_str)%% MATLAB自帶kmeans函數 subplot(2,1,2) %畫子圖,在這里是一圖上可畫兩張子圖 cluster_num=3; %自定義分類數 [index_km,center_km]=kmeans(data,cluster_num) ;%MATLAB自帶kmeans函數 a=unique(index_km); %找出分類出的個數 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函數',' 聚類數為:',num2str(cluster_num),' SC輪廓系數:',num2str(sc_k)]; title(title_str1)返回:
每次返回結果也不盡相同,原理推導的和自帶的函數的求解結果也相差不是很大,但與原始數據的分類相比較,還是有一定差距
總結
以上是生活随笔為你收集整理的K-means聚类 —— matlab的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7配置mysql其他机器访问
- 下一篇: matlab人脸追踪,求大神帮助我这个菜