www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EvaluatetheClusteringSolutionUsingSilhouetteCriterionExample.m
%% Evaluate the Clustering Solution Using Silhouette Criterion % Evaluate the optimal number of clusters using the silhouette clustering % evaluation criterion. %% % Generate sample data containing random numbers from three multivariate % distributions with different parameter values. rng('default'); % For reproducibility mu1 = [2 2]; sigma1 = [0.9 -0.0255; -0.0255 0.9]; mu2 = [5 5]; sigma2 = [0.5 0 ; 0 0.3]; mu3 = [-2, -2]; sigma3 = [1 0 ; 0 0.9]; N = 200; X = [mvnrnd(mu1,sigma1,N);... mvnrnd(mu2,sigma2,N);... mvnrnd(mu3,sigma3,N)]; %% % Evaluate the optimal number of clusters using the silhouette criterion. % Cluster the data using |kmeans|. E = evalclusters(X,'kmeans','silhouette','klist',[1:6]) %% % The |OptimalK| value indicates that, based on the silhouette criterion, % the optimal number of clusters is three. %% % Plot the silhouette criterion values for each number of clusters tested. figure; plot(E) %% % The plot shows that the highest silhouette value occurs at three clusters, % suggesting that the optimal number of clusters is three. %% % Create a grouped scatter plot to visually examine the suggested clusters. figure; gscatter(X(:,1),X(:,2),E.OptimalY,'rbg','xod') %% % The plot shows three distinct clusters within the data: Cluster 1 is in % the lower-left corner, cluster 2 is near the center of the plot, and cluster % 3 is in the upper-right corner.