def kmeans(features, projection, ite = 50, k = 4, threshold = 1e-5): """ perform k_keamns clustering and return a the result as a subsapce clustering object """ from scipy.cluster.vq import kmeans, vq import datetime from measures import spatial_coherence centroids, distance = kmeans(features, k, iter=ite, thresh=threshold) code, _ = vq(features, centroids) run_ = datetime.datetime.now().strftime("%y_%m_%d_%H_%M") params = "projection_size=%d, k=%d" %(len(projection), k) clusters = clusters_from_code(code, k, projection) clustering_id = "(%s)_(%s)_(%s)_(%s)" %("exhaustive_kmeans", params, run_, projection) #print clustering_id km_clt = KMClustering(algorithm ="exhaustive_kmeans", parameters = params, run = run_, clustering_id = clustering_id, clusters = clusters, ccontains_noise = False, cclustering_on_dimension = True) measures = {'spatial_coherence': spatial_coherence(km_clt, len(features))[0], 'distortion': distance} km_clt.update_measures(measures) return km_clt
def kmeans(features, projection, ite = 50, k = 4, threshold = 1e-5): """ perform k_keamns clustering and return a the result as a subsapce clustering object """ from scipy.cluster.vq import kmeans, vq import datetime from measures import spatial_coherence #features = dims_arg centroids, distance = kmeans(features, k, iter=ite, thresh=threshold) code, _ = vq(features, centroids) run_ = datetime.datetime.now().strftime("%y_%m_%d_%H_%M") params = "projection_size=%d, k=%d" %(len(projection), k) km_clt = KMClustering(algorithm ="exhaustive_kmeans", parameters = params, run = run_, clustering_id = None) clusters = clusters_from_code(code, k, projection) for cluster in clusters: km_clt.add_cluster(cluster) measures = {'spatial_coherence': spatial_coherence(km_clt, len(features))[0], 'distortion': distance} km_clt.update_measures(measures) return km_clt