Using contour coefficient to judge the number of K-means clustering
1, kmeans cluster processing data must be numeric numeric, and must be matrxi or data frame
2, profile coefficient can use FPC package
3, clustering effect does not have a special evaluation criteria, the number of clustering can be judged by this.
4, the number of clustering of other methods can also adopt hierarchical clustering method, from the graphic results of the classification of categories Hclust (see last)
Library (FPC)
Data <-Iris[,1:4]
# defines the vector that holds the profile coefficients
Re<-c ()
for (i in2:5) {
result<-Kmeans (data, i)
stats<-cluster.stats (Dist (data), Result$cluster)
re[i]<-Stats$avg.silwidth
};
Re
Final <-Which.max (re) # divided into 2 clustering
Print (re[final]) # profile Factor
The result reads:
From the results, it seems that should be divided into 2 categories more appropriate (the second largest) the actual data is divided into 3 categories
# One More Practice
Re<-c ()
for (Iin4:6) {
Result<-kmeans (Data,i)
Stats<-cluster.stats (Dist (data), Result$cluster)
Re[i]<-stats$avg.silwidth
}
Re
Result Resolution:
If the cluster specifies to start with the 4 class, the result returned is:
# [1] na-na-na 0.4965169 0.3711254 0.3599431
From the results, the first three NA represent the first three clustering (because the result is NA without participating in the calculation)
The key function in FPC package
Cluster.stats (d =null, clustering, alt.clustering =null,
Noisecluster=false,
Silhouette =true, G2 =false, G3 =false,
Wgap=true, Sepindex=true, sepprob=0.1,
Sepwithnoise=true,
Compareonly =false,
Aggregateonly =false) One, input parameter D: Distance object (using dist (data[, numeric property))
Clustering: Using the cluster (clustering results) of Kmeans objects (such as result), numeric vectors (such as 1 1 1 2 2 3 4 4) Two, return object avg.silwidth: Average contour value. (That is, the average of the difference in the center distance of each cluster)
# Based on hierarchical clustering method
HC <-Hclust (Dist (data), method= "CEN") # Class center distance can also have many methods such as ave,median,complete, etc.
# Draw Clustering results (looks like three effects better)
Plot (HC);
Plot (hc, hang=-1) # Hang: Negative numbers are below ordinate 0, positive is above, default is 0.1
# and then just take three clustering ' pruning '
MEMB <-Cutree (hc, K =3)
Memb; Table (MEMB, iris[,5]) # to judge (the effect is not good)