'K-means clustering. http://en.wikipedia.org/wiki/K-means_clustering
Clusterizes the (cols) values of a given row, or viceversa
:param id: row (or col) id to cluster its values
:param k: number of clusters
:param is_row: is param *id* a row (or a col)?
:type is_row: Boolean'

def kmeans(self, id, k=5, is_row=True):

    if VERBOSE:
        sys.stdout.write(('Computing    k-means,    k=%s,    for    id    %s\n' % (k, id)))
    point = None
    if is_row:
        point = self.get_matrix().get_row(id)
    else:
        point = self.get_matrix().get_col(id)
    points = []
    points_id = []
    for i in point.nonzero_entries():
        label = point.label(i)
        points_id.append(label)
        if (not is_row):
            points.append(self.get_matrix().get_row(label))
        else:
            points.append(self.get_matrix().get_col(label))
    if VERBOSE:
        sys.stdout.write(('id    %s    has    %s    points\n' % (id, len(points))))
    M = array(points)
    MAX_POINTS = 150
    if (len(points) <= MAX_POINTS):
        centers = self._kinit(array(points), k)
        (centroids, labels) = kmeans2(M, centers, minit='matrix')
    else:
        (centroids, labels) = kmeans2(M, k, minit='random')
    i = 0
    clusters = dict()
    for cluster in labels:
        if (not clusters.has_key(cluster)):
            clusters[cluster] = dict()
            clusters[cluster]['centroid'] = centroids[cluster]
            clusters[cluster]['points'] = []
        clusters[cluster]['points'].append(points_id[i])
        i += 1
    return clusters
