日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

K-means聚类 —— matlab

發布時間:2025/3/15 循环神经网络 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。