def accuracy(self, examples, descriptions): dst, ind = self.knn.query(examples, self.k) consensus_captions = [] # consensus captions actual_descriptions = [] # actual descriptions for all examples print('computing accuracy') for i, ids in enumerate(ind): captions = [] # candidate captions for j, id in enumerate(ids): # get consensus captions sentences = self.trains['descriptions'][id]['sentences'] for sentence in sentences: captions.append(sentence['raw']) consensus_captions.append(consensus(captions)) sentences = descriptions[i]["sentences"] actual_description = [] for sentence in sentences: actual_description.append(sentence['raw']) actual_descriptions.append(actual_description) if i % 10 == 0: print("{0:.0f}%".format(float(i) / len(examples) * 100)) print('100%') print(accuracy(consensus_captions, actual_descriptions))
def eval(self, examples, descriptions): ''' test knn model and display image :return: ''' dst, ind = self.knn.query(examples, self.k) consensus_captions = [] # consensus captions actual_descriptions = [] # actual descriptions for all examples for i, ids in enumerate(ind): print('{0} predict image {1} filename: {2} {0}'.format( banner1, i, descriptions[i]['filename'])) captions = [] # candidate captions for j, id in enumerate(ids): print('{0} closest image: {1} image id: {2} {0}'.format( banner2, j, self.trains['descriptions'][id]['filename'])) sentences = self.trains['descriptions'][id]['sentences'] for sentence in sentences: print sentence['raw'] #get consensus captions sentences = self.trains['descriptions'][id]['sentences'] for sentence in sentences: captions.append(sentence['raw']) print("Predictions: ", consensus(captions)) consensus_captions.append(consensus(captions)) sentences = descriptions[i]["sentences"] print('actual description:') actual_description = [] for sentence in sentences: actual_description.append(sentence['raw']) print(sentence['raw']) actual_descriptions.append(actual_description) img = mpimg.imread('data/Flicker8k_Dataset/' + descriptions[i]['filename']) plt.imshow(img) plt.show() return consensus_captions, actual_descriptions
def predict(self, examples): dst, ind = self.knn.query(examples, self.k) consensus_captions = [] #consensus captions for i, ids in enumerate(ind): print('predict image ', i) captions = [] # candidate captions for j, id in enumerate(ids): # print 'closed image:', j, 'image id:', id sentences = self.trains['descriptions'][id]['sentences'] for sentence in sentences: captions.append(sentence['raw']) consensus_captions.append(consensus(captions)) return consensus_captions
def predict(self, examples): ''' get the k nearest neighbors of given examples :param examples: images needed to be described :return: ''' dst, ind = self.knn.query(examples, self.k) consensus_captions = [] #consensus captions for i, ids in enumerate(ind): print('predict image ', i) captions = [] # candidate captions for j, id in enumerate(ids): # print 'closed image:', j, 'image id:', id sentences = self.trains['descriptions'][id]['sentences'] for sentence in sentences: captions.append(sentence['raw']) consensus_captions.append(consensus(captions)) return consensus_captions
samples = samples[minflag] mutations_temp = numpy.zeros((len(mutations),len(genes))) mutations_temp[:,tokeep_geneindex] = mutations[:,tokeep_thisindex] mutations = sp.csr_matrix(mutations_temp) mutation_smooth = utils.diffuse(mutations,data['adj'],alpha,diff_thresh) mutation_smooth_norm = sp.csr_matrix(utils.quantile_normalization(numpy.array(mutation_smooth.todense())),shape=mutation_smooth.shape) #U,V = utils.gnmf(mutation_smooth,data['knn'],nclust, gamma, maxiter, tolerance) #labels = numpy.array(V.todense().argmax(axis=1))[:,0] def gnmfsingle(X, W, nclust, gamma, maxiter, tolerance): U,V = utils.gnmf(X, W ,nclust, gamma, maxiter, tolerance) return numpy.array(V.todense().argmax(axis=1))[:,0] cons = utils.consensus(gnmfsingle,mutation_smooth_norm, [data['knn'],nclust, gamma, maxiter, tolerance], bootstrap = 0.8,rep = 100) ######take from stratipy modules zmatrix = linkage(cons,method='average') clusters = fcluster(zmatrix,1) dend = dendrogram(zmatrix,count_sort='ascending') idx=numpy.array(dend['leaves']) plt.imshow(cons[idx,:][:,idx]) plt.plot(range(1, len(zmatrix)+1), zmatrix[::-1, 2]) knee = numpy.diff(zmatrix[::-1, 2], 2) plt.plot(range(2, len(zmatrix)), knee) num_clust1 = knee.argmax() + 2 knee[knee.argmax()] = 0
return individual if __name__ == "__main__": instance = 'x60189_4' matrix = np.genfromtxt(instance + '/matrix_conservative.csv', delimiter=',') #print(matrix.shape) num_fragments = matrix.shape[0] aleatory_solution = np.arange(num_fragments) np.random.shuffle(aleatory_solution) print("initial solution: ", aleatory_solution) sol = PALS(num_fragments, aleatory_solution, matrix) fitness_temp = fitness(matrix, sol) contigs_temp = consensus(matrix, sol) print("fitness: ", fitness_temp, "contig: ", contigs_temp) print("final solution: ", sol) #################################################### pruebas ######################################### num_test = 30.0 fitness_acum = best_fitness = contig_acum = 0.0 best_contig = 100 """ print("TESTING 30 ITEARTIONS...") for i in range(int(num_test)): aleatory_solution = np.arange(num_fragments) np.random.shuffle(aleatory_solution) print("testing...", i)