def kernelkmeans(fileData, epocs ,normalized_axis = 0 ,norm='l1', robust_gamma = 0.00015): #parameters epocs = sio.loadmat(epocs)['epocs'][0][0] #gamma_logscale = [1,2,3,4,5,6,7,8,9,10,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18] gamma_logscale = [-3,-4] print np.size(gamma_logscale) print epocs print epocs*np.size(gamma_logscale) clustering_accuracy = np.zeros(epocs*np.size(gamma_logscale)) calculate_purity = np.zeros(epocs*np.size(gamma_logscale)) calculate_nmi = np.zeros(epocs*np.size(gamma_logscale)) cont = 0 for gamma in gamma_logscale: for epocs in xrange(epocs): print epocs labels_true,labels_pred,features= KernelKMeans.robust_kernel(fileData, normalized_axis = normalized_axis,norm=norm,gamma=2**gamma, robust_gamma = robust_gamma) clustering_accuracy[cont] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[cont],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[cont] = Metrics.calculate_nmi(labels_true,labels_pred) cont +=1 sio.savemat('resultsRobust',{'clusteringAccuracy' : clustering_accuracy,'purityvec' : calculate_purity,'nmivec' : calculate_nmi})
def kernelkmeans(fileData, epocs ,normalized_axis = 0 ,norm='l1'): #parameters epocs = sio.loadmat(epocs)['epocs'][0][0] gamma_logscale = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18] print np.size(gamma_logscale) print epocs print epocs*np.size(gamma_logscale) labels_total = [] clustering_accuracy = np.zeros(epocs*np.size(gamma_logscale)) calculate_purity = np.zeros(epocs*np.size(gamma_logscale)) calculate_nmi = np.zeros(epocs*np.size(gamma_logscale)) clustering_accuracy_mean = np.zeros(np.size(gamma_logscale)) calculate_purity_mean = np.zeros(np.size(gamma_logscale)) calculate_nmi_mean = np.zeros(np.size(gamma_logscale)) clustering_accuracy_sd = np.zeros(np.size(gamma_logscale)) calculate_purity_sd = np.zeros(np.size(gamma_logscale)) calculate_nmi_sd = np.zeros(np.size(gamma_logscale)) cont = 0 contador = 0 for j,gamma in zip(range(34),gamma_logscale): aux=cont for epocs in xrange(30): print epocs labels_true,labels_pred,features= KernelKMeans.get_kernelKMeans(fileData, normalized_axis = normalized_axis,norm=norm,gamma=2**gamma) labels_total.append(labels_pred) if np.size(np.unique(labels_pred)) == np.size(np.unique(labels_true)): clustering_accuracy[cont] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[cont],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[cont] = Metrics.calculate_nmi(labels_true,labels_pred) cont+=1 contador+=1 clustering_accuracy_mean[j] = np.mean(clustering_accuracy[aux:cont-1]) calculate_purity_mean[j] = np.mean(calculate_purity[aux:cont-1]) calculate_nmi_mean[j] = np.mean(calculate_nmi[aux:cont-1]) clustering_accuracy_sd[j] = np.std(clustering_accuracy[aux:cont-1]) calculate_purity_sd[j] = np.std(calculate_purity[aux:cont-1]) calculate_nmi_sd[j] = np.std(calculate_nmi[aux:cont-1]) sio.savemat('G:/Dropbox/Universidad/Machine Learning/Robustes/AR/KernelKMeans/results',{'labels_total' : labels_total , 'clusteringAccuracyVec' : clustering_accuracy, 'purityVec' : calculate_purity, 'nmiVec' : calculate_nmi , 'clusteringAccuracyMeanVec' : clustering_accuracy_mean, 'purityMeanVec' : calculate_purity_mean, 'nmiMeanVec' : calculate_nmi_mean, 'clusteringAccuracyStdVec' : clustering_accuracy_sd , 'purityStdVec' :calculate_purity_sd , 'nmiStdVec':calculate_nmi_sd})
def RMNMF(path,labels_name): labels_pred_total = sio.loadmat(path+'RMNMF/results_aux')['labels_total'] labels_true = sio.loadmat(path+labels_name)['labels'] print labels_pred_total.shape print labels_true.shape num = 4 epocs = 30 clustering_accuracy = np.zeros(num*epocs) calculate_purity = np.zeros(num*epocs) calculate_nmi = np.zeros(num*epocs) clustering_accuracy_mean = np.zeros(num) calculate_purity_mean = np.zeros(num) calculate_nmi_mean = np.zeros(num) clustering_accuracy_sd = np.zeros(num) calculate_purity_sd = np.zeros(num) calculate_nmi_sd = np.zeros(num) cont = 0 contador = 0 for j in range(num): aux = cont for i in range(30): labels_pred = labels_pred_total[contador,:] if np.size(np.unique(labels_pred)) == np.size(np.unique(labels_true)): clustering_accuracy[cont] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[cont],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[cont] = Metrics.calculate_nmi(labels_true,labels_pred) cont+=1 contador+=1 clustering_accuracy_mean[j] = np.mean(clustering_accuracy[aux:cont-1]) calculate_purity_mean[j] = np.mean(calculate_purity[aux:cont-1]) calculate_nmi_mean[j] = np.mean(calculate_nmi[aux:cont-1]) clustering_accuracy_sd[j] = np.std(clustering_accuracy[aux:cont-1]) calculate_purity_sd[j] = np.std(calculate_purity[aux:cont-1]) calculate_nmi_sd[j] = np.std(calculate_nmi[aux:cont-1]) sio.savemat(path+'RMNMF/results',{'labels_total' : labels_pred_total , 'clusteringAccuracyVec' : clustering_accuracy, 'purityVec' : calculate_purity, 'nmiVec' : calculate_nmi , 'clusteringAccuracyMeanVec' : clustering_accuracy_mean, 'purityMeanVec' : calculate_purity_mean, 'nmiMeanVec' : calculate_nmi_mean, 'clusteringAccuracyStdVec' : clustering_accuracy_sd , 'purityStdVec' :calculate_purity_sd , 'nmiStdVec':calculate_nmi_sd})
def Kernelseminmfnnls(path,labels_name): labels_pred_total = sio.loadmat(path+'Kernelseminmfnnls/results_aux')['labels_total'] labels_true = sio.loadmat(path+labels_name)['labels'] print labels_pred_total.shape print labels_true.shape clustering_accuracy = np.zeros(21*30) calculate_purity = np.zeros(21*30) calculate_nmi = np.zeros(21*30) clustering_accuracy_mean = np.zeros(21) calculate_purity_mean = np.zeros(21) calculate_nmi_mean = np.zeros(21) clustering_accuracy_sd = np.zeros(21) calculate_purity_sd = np.zeros(21) calculate_nmi_sd = np.zeros(21) cont = 0 contador = 0 for j in range(21): aux = cont for i in range(30): labels_pred = labels_pred_total[contador,:] #if np.size(np.unique(labels_pred)) == np.size(np.unique(labels_true)): clustering_accuracy[(j)*21+i] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[(j)*21+i],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[(j)*21+i] = Metrics.calculate_nmi(labels_true,labels_pred) cont+=1 contador+=1 clustering_accuracy_mean[j] = np.mean(clustering_accuracy[(j)*21:(j)*21+30]) calculate_purity_mean[j] = np.mean(calculate_purity[(j)*21:(j)*21+30]) calculate_nmi_mean[j] = np.mean(calculate_nmi[(j)*21:(j)*21+30]) clustering_accuracy_sd[j] = np.std(clustering_accuracy[(j)*21:(j)*21+30]) calculate_purity_sd[j] = np.std(calculate_purity[(j)*21:(j)*21+30]) calculate_nmi_sd[j] = np.std(calculate_nmi[(j)*21:(j)*21+30]) sio.savemat(path+'Kernelseminmfnnls/results',{'labels_total' : labels_pred_total , 'clusteringAccuracyVec' : clustering_accuracy, 'purityVec' : calculate_purity, 'nmiVec' : calculate_nmi , 'clusteringAccuracyMeanVec' : clustering_accuracy_mean, 'purityMeanVec' : calculate_purity_mean, 'nmiMeanVec' : calculate_nmi_mean, 'clusteringAccuracyStdVec' : clustering_accuracy_sd , 'purityStdVec' :calculate_purity_sd , 'nmiStdVec':calculate_nmi_sd})
def kmeans(fileData, epocs ,normalized_axis = 0 ,norm='l1'): #parameters epocs = sio.loadmat(epocs)['epocs'][0][0] clustering_accuracy = np.zeros(epocs) calculate_purity = np.zeros(epocs) calculate_nmi = np.zeros(epocs) for epocs in xrange(epocs): print epocs labels_true,labels_pred,features= KMeans.get_kMeans(fileData,normalized_axis = normalized_axis ,norm=norm) clustering_accuracy[epocs] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[epocs],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[epocs] = Metrics.calculate_nmi(labels_true,labels_pred) sio.savemat('results',{'clusteringAccuracy' : clustering_accuracy,'purityvec' : calculate_purity,'nmivec' : calculate_nmi})
def kernelkmeans(fileData, epocs, normalized_axis=0, norm='l1', robust_gamma=0.00015): #parameters epocs = sio.loadmat(epocs)['epocs'][0][0] #gamma_logscale = [1,2,3,4,5,6,7,8,9,10,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18] gamma_logscale = [-3, -4] print np.size(gamma_logscale) print epocs print epocs * np.size(gamma_logscale) clustering_accuracy = np.zeros(epocs * np.size(gamma_logscale)) calculate_purity = np.zeros(epocs * np.size(gamma_logscale)) calculate_nmi = np.zeros(epocs * np.size(gamma_logscale)) cont = 0 for gamma in gamma_logscale: for epocs in xrange(epocs): print epocs labels_true, labels_pred, features = KernelKMeans.robust_kernel( fileData, normalized_axis=normalized_axis, norm=norm, gamma=2**gamma, robust_gamma=robust_gamma) clustering_accuracy[cont] = Metrics.calculate_clusteringAccuracy( labels_true, labels_pred) calculate_purity[cont], vector = Metrics.calculate_purity( labels_true, labels_pred) calculate_nmi[cont] = Metrics.calculate_nmi( labels_true, labels_pred) cont += 1 sio.savemat( 'resultsRobust', { 'clusteringAccuracy': clustering_accuracy, 'purityvec': calculate_purity, 'nmivec': calculate_nmi })
epocs = sio.loadmat('G:/Dropbox/Universidad/Machine Learning/Robustes/ATT/parameters.mat')['epocs'] data = np.load(fileData) X = data['data'] labelsReal = data['labels'] sigma = 2**3 nClusters = 10 X = normalizeByRange.normalizeByRange(X,0) X= np.transpose(X) K = gaussianKernel.gaussianKernel(X,sigma=sigma) H,labelsPred,t = kernel_k_means.KKmeans(K,nClusters,1000) print labelsPred print accuracy.accuracy(labelsReal,labelsPred) print metrics.calculate_clusteringAccuracy(labelsReal,labelsPred) model = KernelKMeans.KernelKMeans(n_clusters=nClusters, kernel="rbf", gamma=1./sigma, verbose=1) print model model.fit(np.transpose(X)) labelsPred = model.labels_ print accuracy.accuracy(labelsReal,labelsPred) print metrics.calculate_clusteringAccuracy(labelsReal,labelsPred)
import scipy.io as sio #parameters #fileData = 'G:/Dropbox/Universidad/Machine Learning/Robustes/Abalone/abalone.npz' #epocs = sio.loadmat('G:/Dropbox/Universidad/Machine Learning/Robustes/Abalone/parameters.mat')['epocs'] fileData = 'G:/Dropbox/Universidad/Machine Learning/Robustes/ATT/att.npz' epocs = sio.loadmat('G:/Dropbox/Universidad/Machine Learning/Robustes/ATT/parameters.mat')['epocs'] n_clusters = 3 clustering_accuracy = np.zeros(epocs) calculate_purity = np.zeros(epocs) calculate_nmi = np.zeros(epocs) labels_total = np.zeros((epocs,400)) for epocs in xrange(epocs): labels_true,labels_pred,features= KMeans.get_kMeans(fileData,normalized_axis = 0 ,norm='l1') labels_total[epocs,:] = labels_pred clustering_accuracy[epocs] = Metrics.calculate_clusteringAccuracy(labels_true,labels_pred) calculate_purity[epocs],vector = Metrics.calculate_purity(labels_true,labels_pred) calculate_nmi[epocs] = Metrics.calculate_nmi(labels_true,labels_pred) clustering_accuracy_mean = np.mean(clustering_accuracy) calculate_purity_mean = np.mean(calculate_purity) calculate_nmi_mean = np.mean(calculate_nmi) sio.savemat('results',{'labels_total' : labels_total ,'clusteringAccuracyVec' : clustering_accuracy,'purityVec' : calculate_purity,'nmiVec' : calculate_nmi , 'clusteringAccuracyMeanVec' : clustering_accuracy_mean, 'purityMeanVec' : calculate_purity_mean, 'nmiMeanVec' : calculate_nmi_mean})
)['epocs'] n_clusters = 3 clustering_accuracy = np.zeros(epocs) calculate_purity = np.zeros(epocs) calculate_nmi = np.zeros(epocs) labels_total = np.zeros((epocs, 400)) for epocs in xrange(epocs): labels_true, labels_pred, features = KMeans.get_kMeans(fileData, normalized_axis=0, norm='l1') labels_total[epocs, :] = labels_pred clustering_accuracy[epocs] = Metrics.calculate_clusteringAccuracy( labels_true, labels_pred) calculate_purity[epocs], vector = Metrics.calculate_purity( labels_true, labels_pred) calculate_nmi[epocs] = Metrics.calculate_nmi(labels_true, labels_pred) clustering_accuracy_mean = np.mean(clustering_accuracy) calculate_purity_mean = np.mean(calculate_purity) calculate_nmi_mean = np.mean(calculate_nmi) sio.savemat( 'results', { 'labels_total': labels_total, 'clusteringAccuracyVec': clustering_accuracy, 'purityVec': calculate_purity, 'nmiVec': calculate_nmi, 'clusteringAccuracyMeanVec': clustering_accuracy_mean,