'**SUMMARY**
This function clusters the blobs in the featureSet based on the properties. Properties can be "color", "shape" or "position" of blobs.
Clustering is done using K-Means or Hierarchical clustering(Ward) algorithm.
**PARAMETERS**
* *properties* - It should be a list with any combination of "color", "shape", "position". properties = ["color","position"]. properties = ["position","shape"]. properties = ["shape"]
* *method* - if method is "kmeans", it will cluster using K-Means algorithm, if the method is "hierarchical", no need to spicify the number of clusters
* *k* - The number of clusters(kmeans).
**RETURNS**
A list of featureset, each being a cluster itself.
**EXAMPLE**
>>> img = Image("lenna")
>>> blobs = img.findBlobs()
>>> clusters = blobs.cluster(method="kmeans",properties=["color"],k=5)
>>> for i in clusters:
>>>     i.draw(color=Color.getRandom(),width=5)
>>> img.show()'

def cluster(self, method='kmeans', properties=None, k=3):

    try:
        from sklearn.cluster import KMeans, Ward
        from sklearn import __version__
    except:
        logger.warning('install    scikits-learning    package')
        return
    X = []
    if (not properties):
        properties = ['color', 'shape', 'position']
    if (k > len(self)):
        logger.warning('Number    of    clusters    cannot    be    greater    then    the    number    of    blobs    in    the    featureset')
        return
    for i in self:
        featureVector = []
        if ('color' in properties):
            featureVector.extend(i.mAvgColor)
        if ('shape' in properties):
            featureVector.extend(i.mHu)
        if ('position' in properties):
            featureVector.extend(i.extents())
        if (not featureVector):
            logger.warning('properties    parameter    is    not    specified    properly')
            return
        X.append(featureVector)
    if (method == 'kmeans'):
        sklearn_version = re.search('\\d+\\.\\d+', __version__).group()
        if (float(sklearn_version) > 0.11):
            k_means = KMeans(init='random', n_clusters=k, n_init=10).fit(X)
        else:
            k_means = KMeans(init='random', k=k, n_init=10).fit(X)
        KClusters = [FeatureSet([]) for i in range(k)]
        for i in range(len(self)):
            KClusters[k_means.labels_[i]].append(self[i])
        return KClusters
    if (method == 'hierarchical'):
        ward = Ward(n_clusters=int(sqrt(len(self)))).fit(X)
        WClusters = [FeatureSet([]) for i in range(int(sqrt(len(self))))]
        for i in range(len(self)):
            WClusters[ward.labels_[i]].append(self[i])
        return WClusters
