X-means follows a split-and-test strategy: start with coarse clusters, then split only when a statistical criterion indicates improvement. This reduces manual trial-and-error over k while keeping the optimization tractable.
<p>X-means is a type of clustering algorithm that automatically determines the number of clusters as the clustering proceeds. This page compares the results of k-means++ and X-means.</p>
frompyclustering.cluster.xmeansimportxmeansfrompyclustering.cluster.center_initializerimportkmeans_plusplus_initializerBAYESIAN_INFORMATION_CRITERION=0MINIMUM_NOISELESS_DESCRIPTION_LENGTH=1defplot_by_xmeans(X,c_min=3,c_max=10,criterion=BAYESIAN_INFORMATION_CRITERION,tolerance=0.025):initial_centers=kmeans_plusplus_initializer(X,c_min).initialize()xmeans_instance=xmeans(X,initial_centers,c_max,criterion=criterion,tolerance=tolerance)xmeans_instance.process()# Create data for plotsclusters=xmeans_instance.get_clusters()n_samples=X.shape[0]c=[]fori,cluster_iinenumerate(clusters):X_ci=X[cluster_i]color_ci=[ifor_incluster_i]plt.scatter(X_ci[:,0],X_ci[:,1],marker="x")plt.title("x-means")# Run x-meansplot_by_xmeans(X,c_min=3,c_max=10,criterion=BAYESIAN_INFORMATION_CRITERION)