plot
Plot clustering evaluation object criterion values
Description
plot(
displays a plot of the
criterion values versus the number of clusters, based on the values in the clustering
evaluation object evaluation
)evaluation
.
plot(
plots
into the axes specified by ax
,evaluation
)ax
instead of the current axes (gca
). (since R2024a)
returns a
h
= plot(___)Line
object using any of the input argument combinations in the previous
syntaxes. Use this object to inspect and adjust the properties of the plot line. For a list
of properties, see Line Properties.
Examples
Plot Clustering Evaluation Criterion Values
Plot the criterion values versus the number of clusters for each clustering solution stored in a clustering evaluation object.
Load the fisheriris
data set. The data contains length and width measurements from the sepals and petals of three species of iris flowers.
load fisheriris
Create a clustering evaluation object. Cluster the data using kmeans
, and evaluate the optimal number of clusters using the Calinski-Harabasz criterion.
rng("default") % For reproducibility evaluation = evalclusters(meas,"kmeans","CalinskiHarabasz","KList",1:6);
Plot the Calinski-Harabasz criterion values for each number of clusters tested.
plot(evaluation)
The plot shows that the highest Calinski-Harabasz value occurs at three clusters, suggesting that the optimal number of clusters is three.
Plot Optimal Number of Clusters Using Different Criteria
Cluster data using each of the four clustering evaluation criteria. For each criterion, create a plot of the criterion values and indicate the optimal number of clusters.
Generate sample data containing random numbers from three multivariate distributions with different parameter values.
rng("default") % For reproducibility n = 200; 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]; X = [mvnrnd(mu1,sigma1,n); ... mvnrnd(mu2,sigma2,n); ... mvnrnd(mu3,sigma3,n)];
Cluster the data using kmeans
, and evaluate the optimal number of clusters using the Calinski-Harabasz, Davies-Bouldin, gap, and silhouette criteria.
calinskiEvaluation = evalclusters(X,"kmeans","CalinskiHarabasz", ... "KList",1:6); daviesEvaluation = evalclusters(X,"kmeans","DaviesBouldin", ... "KList",1:6); gapEvaluation = evalclusters(X,"kmeans","gap","KList",1:6); silhouetteEvaluation = evalclusters(X,"kmeans","silhouette", ... "KList",1:6);
For each clustering evaluation object, plot the criterion values for the number of proposed clusters. In each plot, change the color of the plot line and add a vertical line indicating the optimal number of clusters.
t = tiledlayout(2,2); title(t,"Optimal Number of Clusters for Different Criteria") colors = lines(4); % Calinski-Harabasz Criterion Plot nexttile h1 = plot(calinskiEvaluation); h1.Color = colors(1,:); hold on xline(calinskiEvaluation.OptimalK,"--","Optimal K", ... "LabelVerticalAlignment","middle") hold off % Davies-Bouldin Criterion Plot nexttile h2 = plot(daviesEvaluation); h2.Color = colors(2,:); hold on xline(daviesEvaluation.OptimalK,"--","Optimal K", ... "LabelVerticalAlignment","middle") hold off % Gap Criterion Plot nexttile h3 = plot(gapEvaluation); h3.Color = colors(3,:); hold on xline(gapEvaluation.OptimalK,"--","Optimal K", ... "LabelVerticalAlignment","middle") hold off % Silhouette Criterion Plot nexttile h4 = plot(silhouetteEvaluation); h4.Color = colors(4,:); hold on xline(silhouetteEvaluation.OptimalK,"--","Optimal K", ... "LabelVerticalAlignment","middle") hold off
The four plots indicate that the optimal number of clusters is three, regardless of the clustering criterion.
Input Arguments
ax
— Target axes
Axes
object
Target axes, specified as an Axes object. If you do not specify the axes, then
plot
uses the current axes (gca
).
evaluation
— Clustering evaluation data
CalinskiHarabaszEvaluation
object | DaviesBouldinEvaluation
object | GapEvaluation
object | SilhouetteEvaluation
object
Clustering evaluation data, specified as a CalinskiHarabaszEvaluation
, DaviesBouldinEvaluation
, GapEvaluation
, or SilhouetteEvaluation
clustering evaluation
object. Create a clustering evaluation object by using evalclusters
.
Version History
Introduced in R2013bR2024a: Specify target axes
Specify the target axes for the plot by using the ax
input
argument.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)