subgroup = X[allocation == 0] plt.plot(subgroup[:, 0], subgroup[:, 1], color='r', marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.plot(subgroup2[:, 0], subgroup2[:, 1], color='b', marker='.', linestyle='None', fillstyle='none') plt.title('Spectral Clustering \n nmi against label : ' + str(spectral_nmi)) result = drc(X, 2, 0.5) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, labels) print 'DRC :\n\t', result['allocation'] print 'Dimension :\n', result['L'] plt.subplot(224) subgroup = X[allocation == 0] plt.plot(subgroup[:, 0], subgroup[:, 1], color='r', marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.text(3, 1, str(result['L']),
spectral_nmi = normalized_mutual_info_score(allocation, univ_label) print 'Spectral Clustering : ' , spectral_nmi gmm = GMM(n_components=k) gmm.fit(X) allocation = gmm.predict(X) gmm_nmi = normalized_mutual_info_score(allocation, univ_label) print 'GMM : ' , gmm_nmi ##import pdb; pdb.set_trace() #p = [0.3,0.4,0.5,0.6,0.7] #,0.8,0.9,1,1.1,1.2,1.3,1.4 #drc_nmi = 0 #for m in p: result = drc(X, k, 1*Gamma, 1.1) # 1.1 turned out to be the best ratio allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, univ_label) topic_nmi_new = normalized_mutual_info_score(allocation, topic_label) print 'DRC : ' print '\tUniv : ' , drc_nmi print '\tTopic: ' , topic_nmi_new #print 'DRC : ' , m, ' , ' , drc_nmi_new #if(drc_nmi < drc_nmi_new): # drc_nmi = drc_nmi_new #print 'Dimension :\n' , result['L'] objects = ('K means', 'Spectral', 'GMM', 'DRC') y_pos = np.arange(len(objects))
clf = SpectralClustering(n_clusters=k, gamma=0.5) allocation = clf.fit_predict(X) spectral_nmi = normalized_mutual_info_score(allocation, labels) print 'Pure Spectral Clustering :\n\t' ,allocation plt.subplot(223) subgroup = X[allocation == 0] plt.plot(subgroup[:,0], subgroup[:,1], color='r' , marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.plot(subgroup2[:,0], subgroup2[:,1], color='b' , marker='.', linestyle='None', fillstyle='none') plt.title('Spectral Clustering \n nmi against label : ' + str(spectral_nmi)) result = drc(X, 2, 0.5) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, labels) print 'DRC :\n\t' , result['allocation'] print 'Dimension :\n' , result['L'] plt.subplot(224) subgroup = X[allocation == 0] plt.plot(subgroup[:,0], subgroup[:,1], color='r' , marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.text(3, 1, str(result['L']), style='italic', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10}) plt.plot(subgroup2[:,0], subgroup2[:,1], color='b' , marker='.', linestyle='None', fillstyle='none' ) plt.title('DRC / nmi against label : ' + str(drc_nmi)) plt.show()
clf = SpectralClustering(n_clusters=k, gamma=0.003) allocation = clf.fit_predict(X) spectral_nmi = normalized_mutual_info_score(allocation, label) print 'Spectral Clustering : ', spectral_nmi gmm = GMM(n_components=k) gmm.fit(X) allocation = gmm.predict(X) gmm_nmi = normalized_mutual_info_score(allocation, label) print 'GMM : ', gmm_nmi #import pdb; pdb.set_trace() #p = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,0.008,0.009,0.010,0.011,0.012,0.013, 0.1, 0.2] result = drc(X, 2, 0.8) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, label) print 'DRC : ', drc_nmi #print 'Dimension :\n' , result['L'] # # #objects = ('K means', 'Spectral', 'DRC') #y_pos = np.arange(len(objects)) #performance = [kmeans_nmi, spectral_nmi, drc_nmi] # #plt.bar(y_pos, performance, align='center', alpha=0.5) #matplotlib.rc('xtick', labelsize=10) #plt.xticks(y_pos, objects, rotation='vertical') #plt.ylabel('NMI')
plt.subplot(223) subgroup = X[allocation == 0] plt.plot(subgroup[:, 0], subgroup[:, 1], color='r', marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.plot(subgroup2[:, 0], subgroup2[:, 1], color='b', marker='.', linestyle='None') plt.title('Spectral Clustering \n nmi against label : ' + str(spectral_nmi)) result = drc(X, 2, 0.06) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, labels) #print 'DRC :\n\t' , result['allocation'] #print 'Dimension :\n' , result['L'] plt.subplot(224) subgroup = X[allocation == 0] plt.plot(subgroup[:, 0], subgroup[:, 1], color='r', marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.text(2, 0,
plt.plot(subgroup2[:,0], subgroup2[:,1], color='b' , marker='.', linestyle='None') plt.title('Kmeans \n nmi against label : ' + str(kmeans_nmi)) clf = SpectralClustering(n_clusters=k, gamma=0.08) allocation = clf.fit_predict(X) spectral_nmi = normalized_mutual_info_score(allocation, labels) plt.subplot(223) subgroup = X[allocation == 0] plt.plot(subgroup[:,0], subgroup[:,1], color='r' , marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.plot(subgroup2[:,0], subgroup2[:,1], color='b' , marker='.', linestyle='None') plt.title('Spectral Clustering \n nmi against label : ' + str(spectral_nmi)) result = drc(X, 2, 0.06) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, labels) #print 'DRC :\n\t' , result['allocation'] #print 'Dimension :\n' , result['L'] plt.subplot(224) subgroup = X[allocation == 0] plt.plot(subgroup[:,0], subgroup[:,1], color='r' , marker='x', linestyle='None') subgroup2 = X[allocation == 1] plt.text(2, 0, str(np.round(result['L'],2)), style='italic', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10}) plt.plot(subgroup2[:,0], subgroup2[:,1], color='b' , marker='.', linestyle='None') plt.title('DRC \n nmi against label : ' + str(drc_nmi)) plt.show()
spectral_nmi = normalized_mutual_info_score(allocation, label) print 'Spectral Clustering : ' , spectral_nmi gmm = GMM(n_components=k) gmm.fit(X) allocation = gmm.predict(X) gmm_nmi = normalized_mutual_info_score(allocation, label) print 'GMM : ' , gmm_nmi #import pdb; pdb.set_trace() #p = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,0.008,0.009,0.010,0.011,0.012,0.013, 0.1, 0.2] result = drc(X, 2, 0.8) allocation = result['allocation'] drc_nmi = normalized_mutual_info_score(allocation, label) print 'DRC : ' , drc_nmi #print 'Dimension :\n' , result['L'] # # #objects = ('K means', 'Spectral', 'DRC') #y_pos = np.arange(len(objects)) #performance = [kmeans_nmi, spectral_nmi, drc_nmi] # #plt.bar(y_pos, performance, align='center', alpha=0.5) #matplotlib.rc('xtick', labelsize=10) #plt.xticks(y_pos, objects, rotation='vertical')
d_matrix = sklearn.metrics.pairwise.pairwise_distances(X, Y=None, metric='euclidean') sigma = np.median(d_matrix) Gamma = 1 / (2 * np.power(sigma, 2)) #clf = SpectralClustering(n_clusters=k, gamma=Gamma) #allocation = clf.fit_predict(X) #spectral_nmi = normalized_mutual_info_score(allocation, univ_label) #print 'Spectral Clustering : ' , spectral_nmi #gmm = GMM(n_components=k) #gmm.fit(X) #allocation = gmm.predict(X) result = drc(X, k, 0.07 * Gamma, Const=50.1) # 1.1 turned out to be the best ratio allocation = result['allocation'] if True: # Plot clustering results fig = plt.figure() ax = fig.add_subplot(111, projection='3d') Uq_a = np.unique(allocation) group1 = X[allocation == Uq_a[0]] group2 = X[allocation == Uq_a[1]] group3 = X[allocation == Uq_a[2]] ax.scatter(group1[:, 0], group1[:, 1], group1[:, 2], c='b', marker='o') ax.scatter(group2[:, 0], group2[:, 1], group2[:, 2], c='r', marker='x') ax.scatter(group3[:, 0], group3[:, 1], group3[:, 2], c='g', marker='x') ax.set_xlabel('Feature 1')
sigma = np.median(d_matrix) Gamma = 1/(2*np.power(sigma,2)) #clf = SpectralClustering(n_clusters=k, gamma=Gamma) #allocation = clf.fit_predict(X) #spectral_nmi = normalized_mutual_info_score(allocation, univ_label) #print 'Spectral Clustering : ' , spectral_nmi #gmm = GMM(n_components=k) #gmm.fit(X) #allocation = gmm.predict(X) result = drc(X, k, 0.07*Gamma, Const=50.1) # 1.1 turned out to be the best ratio allocation = result['allocation'] if True: # Plot clustering results fig = plt.figure() ax = fig.add_subplot(111, projection='3d') Uq_a = np.unique(allocation) group1 = X[allocation == Uq_a[0]] group2 = X[allocation == Uq_a[1]] group3 = X[allocation == Uq_a[2]] ax.scatter(group1[:,0], group1[:,1], group1[:,2], c='b', marker='o') ax.scatter(group2[:,0], group2[:,1], group2[:,2], c='r', marker='x') ax.scatter(group3[:,0], group3[:,1], group3[:,2], c='g', marker='x')