def DBSCAN(data, clusters, dataNr = 0): D = distance.squareform(distance.pdist(data)) S = 1 - (D / np.max(D)) db = cluster.DBSCAN().fit(S, eps=0.95, min_samples=10) core_samples = db.core_sample_indices_ labels = db.labels_ x = np.linspace(min([x[0] for x in data]), max([x[0] for x in data])) y = np.linspace(min([y[1] for y in data]), max([y[1] for y in data])) colors = cycle('bgrcmy') for k, col in zip(set(labels), colors): if k == -1: col = 'k' markersize = 6 class_members = [index[0] for index in np.argwhere(labels == k)] cluster_core_samples = [index for index in core_samples if labels[index] == k] for index in class_members: x = data[index] if index in core_samples and k != -1: markersize = 14 else: markersize = 6 pl.plot(x[0], x[1], 'o', markerfacecolor = col, markeredgecolor = 'k', markersize = markersize) draw.setImgTitle('DBSCAN_test' + str(dataNr)) draw.showImage()
def ECMC(data, clusters, dataNr = 0): clusters = ECMCThrToClusters(data, clusters) x = np.linspace(min([x[0] for x in data]), max([x[0] for x in data])) y = np.linspace(min([y[1] for y in data]), max([y[1] for y in data])) colors = 'bgrcmyk' sign = 'o*.x+' for c in range(len(clusters)): #circ = pl.Circle((clusters[c].centre[0], clusters[c].centre[1]), radius = clusters[c].radius, facecolor = "none") #ax = pl.gca() #ax.add_patch(circ) for m in clusters[c].members: pl.plot(m[0], m[1], sign[c/len(colors)], markerfacecolor = colors[c%len(colors)], markersize = 3) draw.setImgTitle('ECMC_test' + str(dataNr)) draw.showImage()
## Data preprocessing data = parse_tab(sys.argv[1]) k = int(sys.argv[2]) classes = [example[-1] for example in data] examples = data_to_na(data) distances = euclidean_distances(examples, examples) # Apply gaussian kernel as suggested in the documentation: gamma = 0.5 # == 1 / num_features (heuristic) similarity_matrix = numpy.exp(-distances * gamma) ## Clustering sc = SpectralClustering(k=k, random_state=0) sc.fit(similarity_matrix) labels = sc.labels_ ## Performance evaluation ari = adjusted_rand_score(labels, classes) homogeneity, completeness, v_measure = homogeneity_completeness_v_measure(labels, classes) print('ARI: {0}'.format(ari)) print('Homogeneity: {0}'.format(homogeneity)) print('Completeness: {0}'.format(completeness)) print('V-measure: {0}'.format(v_measure)) addToResult('Spectral', ari, homogeneity, completeness, v_measure) draw.scatter(examples, labels) print(os.path.splitext(os.path.basename(sys.argv[1]))[0]) draw.setImgTitle('spectral_' + os.path.splitext(os.path.basename(sys.argv[1]))[0]) draw.showImage()