def al_sample_clusters(self, num_clusters, init): ''' active learning with clusters ''' strategy = self.strategy if not init else 'random' sample_ids = [] for i in range(self.num_clusters): if strategy == 'random': new_sample_ids = rand_sample(self.unseen_ids[i], num_clusters) for song_id in new_sample_ids: self.unseen_ids[i].remove(song_id) sample_ids += new_sample_ids elif strategy == 'uncertainty': unseen_x = get_x(self.train_data, ids=self.unseen_ids[i]) unseen_x_scores = self.learner.decision_function( self.transform(unseen_x)) # get index of smallest absolute value (probability or # distance from decision boundary) # from unseen_x_scores, pop the corresponding entry from # self.unseen_ids sample_ids.append(self.unseen_ids[i].pop( np.argmin(np.abs(unseen_x_scores)))) self.train_ids.update(sample_ids) return sample_ids
def G1DConnBiasedMutateWeights(genome, **args): ## args['mutation_conns'] takes the list of indices of connections to be mutated if not args['mutation_conns']: mutation_conn_indices=rand_sample(numpy.array(len(genome)-1),int(args['pmut']*len(genome))) # print "Indices of mutation weights:" # print mutation_conn_indices else: mutation_conn_indices = args['mutation_conns'] # print "Indices of mutation weights:" # print mutation_conn_indices mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") new_mutation_list=[] for it in mutation_conn_indices: final_value = genome['weight'][it] + rand_gauss(mu, sigma) #final_value = min(final_value, genome.getParam("rangemax", Consts.CdefRangeMax)) #final_value = max(final_value, genome.getParam("rangemin", Consts.CdefRangeMin)) new_mutation_list.append(final_value) numpy.put(genome['weight'],[mutation_conn_indices],[new_mutation_list]) return genome
def __get_used_samples(self): indexes = rand_sample(range(0, len(self.samples)), randint(1, self.max_data_in_combined)) used_samples = [k for i, k in enumerate(self.samples) if i in indexes] fully_used = [ self.usage[k] == self.max_data_usage for k in used_samples ] while all(fully_used): indexes = rand_sample(range(0, len(self.samples)), randint(1, self.max_data_in_combined)) used_samples = [ k for i, k in enumerate(self.samples) if i in indexes ] fully_used = [self.usage[k] == 20 for k in used_samples] return used_samples
def load_usps(filename, sample=-1): with open(filename, "r") as f: f.readline() data = [[float(x) for x in l.split()] for l in f if len(l.split()) > 2] tmp = np.array(data) if sample > 0: idx = rand_sample(range(len(tmp)), sample) tmp = tmp[idx, :] return tmp[:, 1:], tmp[:, 0].astype(int)
def spl(self,n,timing=False,nopref=False): if timing: tic = time.time() # Generate variates. if nopref: X=rand_sample(self.keys.values(),n) else: X =[] for nn in xrange(n): X.append(self.draw()) if timing: print('time elapsed:'+str(time.time()-tic)) return X
def al_sample(self, number, init): ''' active learning sample ''' strategy = self.strategy if not init else 'random' if strategy == 'random': # Initial samples must contain all labels for offline learners. # This ensures that happens. if init and not self.is_online(): sampled_classes = set() while len(sampled_classes) != len(CLASSES): sampled_classes = set() sample_ids = rand_sample(self.unseen_ids, number) sampled_classes = set() for id_ in sample_ids: sampled_classes.add(self.train_data[id_]['label']) else: sample_ids = rand_sample(self.unseen_ids, number) for song_id in sample_ids: self.unseen_ids.remove(song_id) elif strategy == 'uncertainty': unseen_x = get_x(self.train_data, ids=self.unseen_ids) unseen_x_scores = self.learner.decision_function( self.transform(unseen_x)) # get index of smallest absolute value (probability or distance from decision boundary) # from unseen_x_scores, pop the corresponding entry from # self.unseen_ids sample_ids = [ self.unseen_ids.pop(np.argmin(np.abs(unseen_x_scores))) ] self.train_ids.update(sample_ids) return sample_ids
def split_data(data, proportion): ''' Splits data into training and validation sets for simple classification. :param data: complete labeled data :param p: proportion of data to use for validation :return train_data, validation_data ''' validation_ids = rand_sample(list(data), int(len(data) * proportion)) validation_data = {k: data[k] for k in validation_ids} train_data = {k: v for k, v in data.items() if k not in validation_data} return FrozenMap(train_data), FrozenMap(validation_data)
def G1DConnUnbiasedMutateWeights(genome, **args): ## args['mutation_conns'] takes the list of indices of connections to be mutated if not args['mutation_conns']: mutation_conn_indices=rand_sample(numpy.arange(len(genome)),int(args['pmut']*len(genome))) # print "Indices of mutation weights:" # print mutation_conn_indices else: mutation_conn_indices = args['mutation_conns'] # print "Indices of mutation weights:" # print mutation_conn_indices new_mutation_list=[] for it in mutation_conn_indices: new_mutation_list.append(rand_uniform(genome.getParam("rangemin", Consts.CDefRangeMin),genome.getParam("rangemax", Consts.CDefRangeMax))) numpy.put(genome['weight'],mutation_conn_indices,new_mutation_list) return genome
def G1DConnUnbiasedMutateWeights(genome, **args): ## args['mutation_conns'] takes the list of indices of connections to be mutated if not args.get('mutation_conns',0): mutation_conn_indices=rand_sample(numpy.arange(len(genome)),int(args['pmut']*len(genome))) # print "Indices of mutation weights:" # print mutation_conn_indices else: mutation_conn_indices = args['mutation_conns'] # print "Indices of mutation weights:" # print mutation_conn_indices mu = genome.getParam("gauss_mu",0) sigma = genome.getParam("gauss_sigma",1) #new_mutation_list=[] for it in mutation_conn_indices: final_value=rand_gauss(mu, sigma) genome['weight'][it] = final_value # new_mutation_list.append(rand_uniform(genome.getParam("rangemin", Consts.CDefRangeMin),genome.getParam("rangemax", Consts.CDefRangeMax))) mutations = len(mutation_conn_indices) #numpy.put(genome['weight'],mutation_conn_indices,new_mutation_list) return int(mutations)
def G1DConnMutateNodes(genome, **args): ## args['mutation_nodes'] takes the list of node numbers to be mutated if not args['mutation_nodes']: mutations=int(args['pmut']*len(numpy.unique(genome['to']))) mutation_node_numbers = rand_sample(numpy.unique(genome['to']), mutations) # print "randomly chosen mutation nodes:" # print mutation_node_numbers else: mutation_node_numbers = list(args['mutation_nodes']) # print "selected mutation nodes:" # print mutation_node_numbers mutations = len(arg['mutation_nodes']) for it in mutation_node_numbers: mutation_nodes = genome.getnode(it) # print "connections of mutation nodes" # print mutation_nodes mutation_conn_indices=getMutateConnIndex(genome, mutation_nodes) G1DConnBiasedMutateWeights(genome, mutation_conns=mutation_conn_indices) return int(mutations)
def ChromoInitializator(genome, **args): genome.genomeList = rand_sample(xrange(genome.getListSize()), genome.getListSize())
def rand_max(arr, key=lambda x: x): return rand_sample(max_els(arr, key), 1)[0]