Main Content

Cluster Evaluation

This example shows how to identify clusters in Fisher's iris data.

Load Fisher's iris data set.

loadfisheririsX = meas; y = categorical(species);

Xis a numeric matrix that contains two petal measurements for 150 irises.Yis a cell array of character vectors that contains the corresponding iris species.

Evaluate multiple clusters from 1 to 10.

eva = evalclusters(X,'kmeans','CalinskiHarabasz','KList',1:10)
eva = CalinskiHarabaszEvaluation with properties: NumObservations: 150 InspectedK: [1 2 3 4 5 6 7 8 9 10] CriterionValues: [NaN 513.9245 561.6278 530.4871 456.1279 469.5068 ... ] OptimalK: 3

TheOptimalKvalue indicates that, based on the Calinski-Harabasz criterion, the optimal number of clusters is three.

Visualizeevato see the results for each number of clusters.

plot(eva)

Figure contains an axes object. The axes object contains 2 objects of type line.

Most clustering algorithms need prior knowledge of the number of clusters. When this information is not available, use cluster evaluation techniques to determine the number of clusters present in the data based on a specified metric.

Three clusters is consistent with the three species in the data.

categories(y)
ans =3x1 cell{' setosa}{“癣”}{' virginica '}

Compute a nonnegative rank-two approximation of the data for visualization purposes.

Xred = nnmf(X,2);

The original features are reduced to two features. Since none of the features are negative,nnmfalso guarantees that the features are nonnegative.

Confirm the three clusters visually using a scatter plot.

gscatter(Xred(:,1),Xred(:,2),y) xlabel('Column 1') ylabel('Column 2') gridon

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent setosa, versicolor, virginica.

See Also

|