Package: cluster-crit
Cluster CriteriaDescriptionThe project is a python extension for thecluster critRpackage. The project also contains the ability to select the optimal results when running Cluster Criteria algorithms on any number of clusters.External DependenciesThe R programming language is a dependency of this project, and itmustbe installed prior to installing this project. Please visit theR Downloads Page.Internal CriteriaThe function intCriteria calculates internal clustering indices. The list of all internal criteria can be found incriteria.py.External CriteriaThe function extCriteria calculates external clustering indices in order to compare two partitions. The list of all external criteria can be found incriteria.py.Best CriterionGiven a vector of several clustering quality index values computed with a given criterion, the function bestCriterion returns the index of the "best" one in the sense of the specified criterion.
Typically, a set of data has been clusterized several times (using different algorithms or specifying a different number of clusters) and a clustering index has been calculated each
time. The bestCriterion function determines which value is considered the best according to the given clustering index. For instance, if one uses the Calinski_Harabasz index, the best
value is the largest one. A list of all the supported criteria can be obtained with the getCriteriaNames function. The criterion name (crit argument) is case insensitive and can be abbreviated.Get Criteria NamesGet a list of Criteria Names.The user can return Internal vs External Criteria Names by setting theinternalvariable toTruevsFalserespectively.When retrieving Internal Criteria, the user can setincludeGCItoFalseto skip returning any criteria with GDI-XXX as the name.The user can also control the return type by settingreturnEnumerationstoTrue(return Enumerations) orFalseto return the string representations of the criteria.ExamplesThe following sections are a set of brief/simple examples of this library. To setup/initialize these tests, you can use the following steps:Install All External Dependencies (see external dependencies above).Installkmeans1d:python -m pip install kmeans1dCreate the original set of data (this is a sample taken from a large data set).original=[-0.018,-0.03,0.025,-0.073,-0.007,0.052,-0.042,-0.025,-0.056,0.005,0.131,0.059,0.15,0.157,0.036,0.096,-0.027,-0.002,0.069,0.099,0.067,0.101,0.105,0.115,0.108,-0.036,-0.109,-0.133,-0.061,-0.045,-0.058,0.017,0.007,-0.093,0.077,0.085,0.1,-0.005,0.009,0.16]Note:It is advised that you convert this data when it is a 1-D data set like above.importnumpyasnporiginal=np.asarray([-0.018,-0.03,0.025,-0.073,-0.007,0.052,-0.042,-0.025,-0.056,0.005,0.131,0.059,0.15,0.157,0.036,0.096,-0.027,-0.002,0.069,0.099,0.067,0.101,0.105,0.115,0.108,-0.036,-0.109,-0.133,-0.061,-0.045,-0.058,0.017,0.007,-0.093,0.077,0.085,0.1,-0.005,0.009,0.16])original=original.reshape(-1,1)Create a wrapper forkmeansso that we can generate the clusters for the above data set.defk_means_wrapper(data_set,k):matching_clusters,centroids=kmeans1dc(data_set,k)# R uses values 1-N not 0-N-1, so let's update herematching_clusters=[x+1forxinmatching_clusters]returnmatching_clustersCluster the data using values of K = 2,3,4,5,6 ...clusters=[k_means_wrapper(original,2),k_means_wrapper(original,3),k_means_wrapper(original,4),k_means_wrapper(original,5),k_means_wrapper(original,6)]You should now have values similar to the following:clusters=[[1,1,1,1,1,2,1,1,1,1,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,2,2,2,1,1,2],[2,2,2,1,2,3,1,2,1,2,3,3,3,3,2,3,2,2,3,3,3,3,3,3,3,2,1,1,1,1,1,2,2,1,3,3,3,2,2,3],[2,2,2,1,2,3,1,2,1,2,4,3,4,4,2,3,2,2,3,3,3,3,3,3,3,2,1,1,1,1,1,2,2,1,3,3,3,2,2,4],[2,2,3,2,3,4,2,2,2,3,5,4,5,5,3,4,2,3,4,4,4,4,4,4,4,2,1,1,2,2,2,3,3,1,4,4,4,3,3,5],[3,2,3,2,3,4,2,2,2,3,6,4,6,6,4,5,2,3,4,5,4,5,5,5,5,2,1,1,2,2,2,3,3,1,4,5,5,3,3,6],]These clusters can be used as parameters to IntCriteria. Follow similar steps to produce data for ExtCriteria.Internal CriteriaThe following will receive the results of the clusters with theDunnCriteria on cluster numbers two through six.criteria=CriteriaInternal.Dunnvalues=[]forclusterinclusters:output=intCriteria(original,cluster,[criteria])values.append(output[criteria.name])External Criteriafromrandomimportrandint# generate two artificial partitionspart1=[randint(1,3)for_inrange(150)]part2=[randint(1,5)for_inrange(150)]output=extCriteria(part1,part2,[CriteriaExternal.Czekanowski_Dice])Best CriterionContinuing with the IntCriteria example above, the following will print the index of the best cluster size given the
outputs of the Internal Crtieria evaluation.crit=np.asarray(values)print(bestCriterion(crit,criteria.name))Get Criteria NamesThe example will get all InternalCriteria, excludingGDI-XXXcriteria, and the values will be returned as enumerations.criteria=getCriteriaNames(True,False,True)The provided parameters are defaults, and they donotneed to be specified.