我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用random.sample()。
def draw(path, srv): filename = os.path.join(path, srv["preprocessed_filename"]) df = pd.read_csv(filename, sep="\t", index_col='time', parse_dates=True) bins = defaultdict(list) for i, col in enumerate(df.columns): serie = df[col].dropna() if pd.algos.is_monotonic_float64(serie.values, False)[0]: serie = serie.diff()[1:] p_value = adfuller(serie, autolag='AIC')[1] if math.isnan(p_value): continue nearest = 0.05 * round(p_value/0.05) bins[nearest].append(serie) for bin, members in bins.items(): series = [serie.name for serie in members] if len(members) <= 10: columns = series else: columns = random.sample(series, 10) subset = df[columns] name = "%s_adf_confidence_%.2f.png" % (srv["name"], bin) print(name) axes = subset.plot(subplots=True) plt.savefig(os.path.join(path, name)) plt.close("all")
def reorder_bpr_loss(re_x, his_x, dynamic_user, item_embedding, config): ''' loss function for reorder prediction re_x padded reorder baskets his_x padded history bought items ''' nll = 0 ub_seqs = [] for u, h, du in zip(re_x, his_x, dynamic_user): du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item nll_u = [] # nll for user for t, basket_t in enumerate(u): if basket_t[0] != 0: pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t) # Sample negative products neg = [random.choice(h[t]) for _ in range(len(basket_t))] # replacement # neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg) # Score p(u, t, v > v') score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx] # Average Negative log likelihood for basket_t nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score))) nll += torch.mean(torch.cat(nll_u)) return nll
def test_uint_multi_port(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) do_ports = random.sample( [d for d in x_series_device.do_ports if d.do_port_width <= 16], 2) total_port_width = sum([d.do_port_width for d in do_ports]) with nidaqmx.Task() as task: task.do_channels.add_do_chan( flatten_channel_string([d.name for d in do_ports]), line_grouping=LineGrouping.CHAN_FOR_ALL_LINES) # Generate random values to test. values_to_test = [int(random.getrandbits(total_port_width)) for _ in range(10)] values_read = [] for value_to_test in values_to_test: task.write(value_to_test) time.sleep(0.001) values_read.append(task.read()) assert values_read == values_to_test
def test_insufficient_numpy_write_data(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) # Randomly select physical channels to test. number_of_channels = random.randint( 2, len(x_series_device.ao_physical_chans)) channels_to_test = random.sample( x_series_device.ao_physical_chans, number_of_channels) with nidaqmx.Task() as task: task.ao_channels.add_ao_voltage_chan( flatten_channel_string([c.name for c in channels_to_test]), max_val=10, min_val=-10) number_of_samples = random.randint(1, number_of_channels - 1) values_to_test = numpy.float64([ random.uniform(-10, 10) for _ in range(number_of_samples)]) with pytest.raises(DaqError) as e: task.write(values_to_test, auto_start=True) assert e.value.error_code == -200524
def __init__(self, blanca_arriba = True): """TODO: to be defined1. """ # Creo una cara de cada color self.caras = [] if blanca_arriba: self.caras.append(Cara(COLOR_WHITE)) self.caras.append(Cara(COLOR_BLUE)) self.caras.append(Cara(COLOR_RED)) self.caras.append(Cara(COLOR_GREEN)) self.caras.append(Cara(COLOR_ORANGE)) self.caras.append(Cara(COLOR_YELLOW)) else: caras = random.sample(range(6),6) #print caras for i in xrange(6): self.caras.append(Cara(caras[i])) self.configAdyacentes()
def test_bfill(self): # test ndim=1 N = 100 s = pd.Series(np.random.randn(N)) mask = random.sample(range(N), 10) s.iloc[mask] = np.nan correct = s.bfill().values test = bfill(s.values) assert_almost_equal(correct, test) # test ndim=2 df = pd.DataFrame(np.random.randn(N, N)) df.iloc[mask] = np.nan correct = df.bfill().values test = bfill(df.values) assert_almost_equal(correct, test)
def test_ffill(self): # test ndim=1 N = 100 s = pd.Series(np.random.randn(N)) mask = random.sample(range(N), 10) s.iloc[mask] = np.nan correct = s.ffill().values test = ffill(s.values) assert_almost_equal(correct, test) # test ndim=2 df = pd.DataFrame(np.random.randn(N, N)) df.iloc[mask] = np.nan correct = df.ffill().values test = ffill(df.values) assert_almost_equal(correct, test)
def loadLogoSet(path, rows,cols,test_data_rate=0.15): random.seed(612) _, imgID = readItems('data.txt') y, _ = modelDict(path) nPics = len(y) faceassset = np.zeros((nPics,rows,cols), dtype = np.uint8) ### gray images noImg = [] for i in range(nPics): temp = cv2.imread(path +'logo/'+imgID[i]+'.jpg', 0) if temp == None: noImg.append(i) elif temp.size < 1000: noImg.append(i) else: temp = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC) faceassset[i,:,:] = temp y = np.delete(y, noImg,0); faceassset = np.delete(faceassset, noImg, 0) nPics = len(y) index = random.sample(np.arange(nPics), int(nPics*test_data_rate)) x_test = faceassset[index,:,:]; x_train = np.delete(faceassset, index, 0) y_test = y[index]; y_train = np.delete(y, index, 0) return (x_train, y_train), (x_test, y_test)
def perturb(self): ''' Peturb the paramter by choosing a random value between val_min and val_max. Will choose a random number with precision specified by decimals. Will optionally pick the min or the max value after a specified number of perturb calls ''' if self.n == self.n_max - 1: # Choose and extreme value self.val = random.sample([self.val_min, self.val_max], 1)[0] self.n = 0 else: if self.decimals == 0: self.val = random.randint(self.val_min,self.val_max) else: self.val = random.random() * (self.val_max - self.val_min + 10**-self.decimals) + (self.val_min - 0.5 * 10**-self.decimals) self.val = round(self.val, ndigits=self.decimals) if self.n_max > 0: self.n += 1
def plot_labeled_images_random(image_list, label_list, categories, n, title_str, ypixels, xpixels, seed, filename): random.seed(seed) index_sample = random.sample(range(len(image_list)), n) plt.figure(figsize=(2*n, 2)) #plt.suptitle(title_str) for i, ind in enumerate(index_sample): ax = plt.subplot(1, n, i + 1) plt.imshow(image_list[ind].reshape(ypixels, xpixels)) plt.gray() ax.set_title(categories[label_list[ind]], fontsize=20) ax.get_xaxis().set_visible(False); ax.get_yaxis().set_visible(False) if 1: pylab.savefig(filename, bbox_inches='tight') else: plt.show() # plot_unlabeled_images_random: plots unlabeled images at random
def plot_unlabeled_images_random(image_list, n, title_str, ypixels, xpixels, seed, filename): random.seed(seed) index_sample = random.sample(range(len(image_list)), n) plt.figure(figsize=(2*n, 2)) plt.suptitle(title_str) for i, ind in enumerate(index_sample): ax = plt.subplot(1, n, i + 1) plt.imshow(image_list[ind].reshape(ypixels, xpixels)) plt.gray() ax.get_xaxis().set_visible(False); ax.get_yaxis().set_visible(False) if 1: pylab.savefig(filename, bbox_inches='tight') else: plt.show() # plot_compare: given test images and their reconstruction, we plot them for visual comparison
def startReplay(warcFilename): global p pathOfWARC = os.path.join(os.path.dirname(moduleLocation) + '/samples/warcs/' + warcFilename) tempFilePath = '/tmp/' + ''.join(random.sample( string.ascii_uppercase + string.digits * 6, 6)) + '.cdxj' print('B2' + tempFilePath) p = Process(target=replay.start, args=[tempFilePath]) p.start() sleep(5) cdxjList = indexer.indexFileAt(pathOfWARC, quiet=True) cdxj = '\n'.join(cdxjList) with open(tempFilePath, 'w') as f: f.write(cdxj)
def generate_article_url(self, response): as_id = ''.join(random.sample(string.ascii_letters + string.digits, 15)) cp_id = ''.join(random.sample(string.ascii_letters + string.digits, 15)) yield scrapy.Request( "http://www.toutiao.com/api/pc/feed/?category=news_tech&utm_source=toutiao&widen=1&max_behot_time=0" + "max_behot_time_tmp=" + str(int(time.time())) + "tadrequire=true&as=" + as_id + "&cp=" + cp_id + "&t=" + str(time.time()), callback=self.generate_article_url ) article_list = json.loads(response.body) if article_list.get("message") != "success": return for article_detail in article_list.get('data'): # wenda gallery ad ? # news_tech and news_finance tag_url = article_detail.get('tag_url') if article_detail.get('article_genre') == 'article'\ and (tag_url == 'news_tech' or tag_url == 'news_finance'): yield scrapy.Request( self.toutiao_url_pre + article_detail.get('source_url'), callback=self.generate_article_content )
def end_of_episode(self): ''' Summary: Performs miscellaneous end of episode tasks (#printing out useful information, saving stuff, etc.) ''' # self.model = self.weak_learners self.add_new_weak_learner() self.most_recent_episode = [] if self.markov_window > 0: # num_sampled_trees = int(math.ceil(len(self.weak_learners) / 10.0)) # self.model = random.sample(self.weak_learners, num_sampled_trees) self.model = self.weak_learners[-self.markov_window:] else: self.model = self.weak_learners Agent.end_of_episode(self)
def upload_file(upload_file_name, temp): # upload_file_name????? # ??? saveas??? # ?????????,??git???saveas #key = md5(str(time.time())+''.join(random.sample(string.letters, 12))).hexdigest() # key ?????? print u"??????: ", pic_name = raw_input() uuid_6 = uuid.uuid4().get_hex()[:8] #????? key = pic_name+"_"+uuid_6+".png" copyfile(upload_file_name,join(saveas,key)) mime_type = 'image/png' token = q.upload_token(bucket, key) ret, info = put_file(token, key, upload_file_name, mime_type=mime_type, check_crc=True) print 'upload qiniu result:', info assert ret['key'] == key assert ret['hash'] == etag(upload_file_name) os.rename(upload_file_name, upload_file_name+'.old') return domain+'/'+key
def __init__(self, config): super(CandidateDataLoader, self).__init__(config) self.resize_to = config.size[0] self.diameter_mm = config.diameter_mm self.batch_size = config.batch self.validation_rate =config.validation_ratio self.k = config.top_k # set data dir and file self.data_dir = 'data/' self.pkl_dir = self.data_dir + 'pkl/' self.stage1_dir = self.data_dir + 'stage1/' self.sample_dir = self.data_dir + 'sample/' self.patch_dir = self.data_dir + 'patch/' self.data_file_name = 'kaggle_stage1.pkl' self.feature_file_name = 'features_detect_kaggle.pkl' self.train_ids, self.valid_ids, self.test_ids = self.split_dataset() # self.train_ids, self.valid_ids, self.test_ids = self.get_ids_from_sample_dataset() self.current_ids = self.train_ids self.current_pointer = 0 self.current_set = None self.random_for_negative_samples = True self.data = self.build_data_dict(layer_features=['67', '77'], k=self.k)
def reseed(self, netdb): """Compress netdb entries and set content""" zip_file = io.BytesIO() dat_files = [] for root, dirs, files in os.walk(netdb): for f in files: if f.endswith(".dat"): # TODO check modified time # may be not older than 10h dat_files.append(os.path.join(root, f)) if len(dat_files) == 0: raise PyseederException("Can't get enough netDb entries") elif len(dat_files) > 75: dat_files = random.sample(dat_files, 75) with ZipFile(zip_file, "w", compression=ZIP_DEFLATED) as zf: for f in dat_files: zf.write(f, arcname=os.path.split(f)[1]) self.FILE_TYPE = 0x00 self.CONTENT_TYPE = 0x03 self.CONTENT = zip_file.getvalue() self.CONTENT_LENGTH = len(self.CONTENT)
def genplot(x, y, fit, xdata=None, ydata=None, maxpts=10000): bin_range = (0, 360) a = (np.arange(*bin_range)) f_a = nuth_func(a, fit[0], fit[1], fit[2]) nuth_func_str = r'$y=%0.2f*cos(%0.2f-x)+%0.2f$' % tuple(fit) if xdata.size > maxpts: import random idx = random.sample(list(range(xdata.size)), 10000) else: idx = np.arange(xdata.size) f, ax = plt.subplots() ax.set_xlabel('Aspect (deg)') ax.set_ylabel('dh/tan(slope) (m)') ax.plot(xdata[idx], ydata[idx], 'k.', label='Orig pixels') ax.plot(x, y, 'ro', label='Bin median') ax.axhline(color='k') ax.plot(a, f_a, 'b', label=nuth_func_str) ax.set_xlim(*bin_range) pad = 0.2 * np.max([np.abs(y.min()), np.abs(y.max())]) ax.set_ylim(y.min() - pad, y.max() + pad) ax.legend(prop={'size':8}) return f #Function copied from from openPIV pyprocess
def attack(): ip = socket.gethostbyname( host ) global n msg=str(string.letters+string.digits+string.punctuation) data="".join(random.sample(msg,5)) dos = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: n+=1 dos.connect((ip, port)) dos.send( "GET /%s HTTP/1.1\r\n" % data ) print "\n "+time.ctime().split(" ")[3]+" "+"["+str(n)+"] #-#-# Hold Your Tears #-#-#" except socket.error: print "\n [ No connection! Server maybe down ] " dos.close()
def _batch_gen(self): from random import sample,randint self.DB.get_split() epochs = self.flags.epochs fold = self.flags.fold if fold>=0: docs_ids = list(self.DB.split[fold][0]) else: docs_ids = list(range(self.DB.data['training_text'].shape[0])) B = min(self.flags.batch_size,len(docs_ids)) batches_per_epoch = len(docs_ids)//B y = self.DB.y #print(batches_per_epoch) for epoch in range(epochs): for batch in range(batches_per_epoch): inputs = [] labels = [] # 0 or 1 for idx in sample(docs_ids,B): inputs.append(idx+1) labels.append(y[idx]) yield inputs, labels, epoch
def ex3(argv): password = '' for i in range(len(argv)): for j in range(int(argv[i])): if i == 0: password += string.uppercase[random.randint(0,len(string.uppercase)-1)] elif i == 1: password += string.lowercase[random.randint(0,len(string.lowercase)-1)] elif i == 2: password += string.digits[random.randint(0,len(string.digits)-1)] elif i == 3: password += string.punctuation[random.randint(0,len(string.punctuation)-1)] return ''.join(random.sample(password,len(password)))
def compress(string): # Get the unique characters and numeric base. unique = set(string) base = len(unique) # Create a key that will encode data properly. key = random.sample(unique, base) mapping = dict(map(reversed, enumerate(key))) while not mapping[string[-1]]: key = random.sample(unique, base) mapping = dict(map(reversed, enumerate(key))) # Create a compressed numeric representation. value = 0 for place, char in enumerate(string): value += mapping[char] * base ** place # Return the number as a string with the table. return decode(value), bytes(key)
def randomly_grouped_by(key_from_example: Callable[[LabeledExample], Any], training_share: float = .9) -> Callable[ [List[LabeledExample]], Tuple[List[LabeledExample], List[LabeledExample]]]: def split(examples: List[LabeledExample]) -> Tuple[List[LabeledExample], List[LabeledExample]]: examples_by_directory = group(examples, key=key_from_example) directories = examples_by_directory.keys() # split must be the same every time: random.seed(42) keys = set(random.sample(directories, int(training_share * len(directories)))) training_examples = [example for example in examples if key_from_example(example) in keys] test_examples = [example for example in examples if key_from_example(example) not in keys] return training_examples, test_examples return split
def handler(event, context): num_of_players = int(os.environ['NUMBER_OF_PLAYERS']) num_of_mafia = int(os.environ['NUMBER_OF_MAFIA']) clear_all() names = random.sample(all_names, num_of_players) mafia = random.sample(range(0, num_of_players), num_of_mafia) for i in range(num_of_players): player = { 'Name': names[i], 'TrueIdentity': 'Mafia' if i in mafia else 'Innocent', 'Identity': 'Uncovered' } save(player) message = "New game started with {}".format(', '.join(names)) return response( {"message": message}, event ) # deleate all records in the table
def makeQ(self): shapes = ["Square", "Rectangle", "Triangle", "Octagon", "Pentagon", ] shape = random.sample(shapes, 1)[0] q1 = ("How many sides does a {0} have?").format(shape) options = [3,4,8,5] if shape == "Square" or shape =="Rectangle": a1 = 4 elif shape == "Triangle": a1 = 3 elif shape == "Octagon": a1=8 elif shape =="Pentagon": a1=5 print("Choose the correct answer: {0}").format(options) return q1, a1, options
def gen_pseudo_word(self, L=None): if not L: L = random.randint(1, 8) # generate one word that we hadn't used before while True: if self.readable: # alternating between vowels and consonants, sampled with repl. _choice, _range = random.choice, range(int(math.ceil(L / 2))) v = [_choice(self.V) for i in _range] c = [_choice(self.C) for i in _range] zipped = zip(v, c) if random.getrandbits(1) else zip(c, v) pseudo_word = ''.join([a for b in zipped for a in b])[:L] else: pseudo_word = ''.join(random.sample( string.ascii_lowercase, L)) if pseudo_word not in self.inv_word_mapping: return pseudo_word
def get_task_generator(self): content = load_dictionary(DICTIONARY_FILE) vocabulary = content[:200] mapping = dict(zip(random.sample(vocabulary, self.MAPPING_SIZE), random.sample(vocabulary, self.MAPPING_SIZE))) keys = list(mapping.keys()) self.mapping_check = {key: False for key in keys} def micro6_question(self): def micro6_feedback(is_correct, question): reaction = "good job" if is_correct else "wrong" if not is_correct: return reaction + '! ' + sentence else: return reaction + '! ' self.key_idx = random.randint(0, len(keys) - 1) word1 = keys[self.key_idx] word2 = mapping[word1] question = 'random_map: ' + word1 + '.' sentence = word2 + '.' return question, [sentence], micro6_feedback return TaskGenerator(micro6_question, '', None, ';')
def on_start(self, event): # choose some random object self.target_obj, = random.sample(objects, 1) # find the cell in front of the learner ws = self.get_world().state ld = self.get_world().valid_directions[ws.learner_direction] lp = ws.learner_pos self.state.initial_count = ws.learner_inventory[self.target_obj] # place the object there self.get_world().put_entity(lp + ld, self.target_obj, True, True) self.add_handler( on_state_changed(lambda ws, ts: ws.learner_inventory[self.target_obj] == ts.initial_count + 1) (self.on_object_picked_up)) self.set_message("You have {indef_object} in front of you. " "Pick up the {object}.".format( indef_object=msg.indef_article(self.target_obj), object=self.target_obj))
def on_start(self, event): # choose a random object self.target_obj, = random.sample(objects, 1) # find a random cell around the learner self.direction = random.choice(list(self.get_world().valid_directions .keys())) ws = self.get_world().state p = ws.learner_pos + self.get_world().valid_directions[self.direction] self.state.initial_count = ws.learner_inventory[self.target_obj] # place the object there self.get_world().put_entity(p, self.target_obj, True, True) self.add_handler( on_state_changed(lambda ws, ts: ws.learner_inventory[self.target_obj] == ts.initial_count + 1) (self.on_object_picked_up)) self.set_message("There is {indef_object} {direction} from you, " "pick up the {object}.".format( indef_object=msg.indef_article(self.target_obj), direction=self.direction, object=self.target_obj))
def on_start(self, event): # choose a random object self.target_obj, = random.sample(objects, 1) ws = self.get_world().state # select a random number of steps self.n = random.randint(1, PickUpInFrontTask.max_steps_forward) # place the object that number of steps in front of the learner p = ws.learner_pos + self.n * self.get_world().valid_directions[ ws.learner_direction] self.state.initial_count = ws.learner_inventory[self.target_obj] self.get_world().put_entity(p, self.target_obj, True, True) self.add_handler( on_state_changed(lambda ws, ts: ws.learner_inventory[self.target_obj] == ts.initial_count + 1) (self.on_object_picked_up)) self.set_message("There is {indef_object} {n} steps forward, " "pick up the {object}." .format( indef_object=msg.indef_article(self.target_obj), n=msg.number_to_string(self.n), object=self.target_obj))
def on_start(self, event): ws = self.get_world().state # pick a random object self.state.target_obj, = random.sample(objects, 1) # give one of it to the learner ws.learner_inventory[self.state.target_obj] += 1 # save how many objects of this we have self.state.initial_count = ws.teacher_inventory[self.state.target_obj] # inform the world that we can expect to receive such an object ws.teacher_accepts.add(self.state.target_obj) self.set_message("I gave you {indef_object}. Give it back to me " "by saying \"I give you {indef_object}\"." .format(indef_object=msg.indef_article( self.state.target_obj))) # if I have one more of the target object, the learner solved the task.
def getword(): lock = threading.Lock() lock.acquire() if len(words) != 0: value = random.sample(words, 1) words.remove(value[0]) else: print "\nReloading Wordlist - Changing User\n" reloader() value = random.sample(words, 1) users.remove(users[0]) lock.release() if len(users) ==1: return value[0][:-1], users[0] else: return value[0][:-1], users[0][:-1]
def getword(): lock = threading.Lock() lock.acquire() if len(words) != 0: value = random.sample(words, 1) words.remove(value[0]) else: print "\nReloading Wordlist - Changing User\n" reloader() value = random.sample(words, 1) users.remove(users[0]) lock.release() if len(users) ==1: return users[0], value[0][:-1] else: return users[0][:-1], value[0][:-1]
def getword(): lock = threading.Lock() lock.acquire() if len(words) != 0: value = random.sample(words, 1) words.remove(value[0]) else: print "[-] Reloading Wordlist - Changing User\n" reloader() value = random.sample(words, 1) users.remove(users[0]) lock.release() if len(users) ==1: return value[0], users[0] else: return value[0], users[0]
def _random_curve(self, nr_curves): curves = [] for i in range(nr_curves-1): curve = [(0,0)] # exlcude the 0 and 255 _x = numpy.sort(random.sample(range(1, 255), 32)) _y = numpy.sort(random.sample(range(1, 255), 32)) #_x = numpy.sort(numpy.random.randint(1, 255, 2)) #_y = numpy.sort(numpy.random.randint(1, 255, 2)) # _x[0] and _x[1] can't be the same curve.append((_x[0], _y[0])) curve.append((_x[1], _y[1])) curve.append((255,255)) curves.append(curve) curves.append([(255,255)]) return curves
def sample(self, nb_samples): sampled_character_folders = random.sample(self.character_folders, nb_samples) random.shuffle(sampled_character_folders) example_inputs = np.zeros((self.batch_size, nb_samples * self.nb_samples_per_class, np.prod(self.img_size)), dtype=np.float32) example_outputs = np.zeros((self.batch_size, nb_samples * self.nb_samples_per_class), dtype=np.float32) #notice hardcoded np.float32 here and above, change it to something else in tf for i in range(self.batch_size): labels_and_images = get_shuffled_images(sampled_character_folders, range(nb_samples), nb_samples=self.nb_samples_per_class) sequence_length = len(labels_and_images) labels, image_files = zip(*labels_and_images) angles = np.random.uniform(-self.max_rotation, self.max_rotation, size=sequence_length) shifts = np.random.uniform(-self.max_shift, self.max_shift, size=sequence_length) example_inputs[i] = np.asarray([load_transform(filename, angle=angle, s=shift, size=self.img_size).flatten() \ for (filename, angle, shift) in zip(image_files, angles, shifts)], dtype=np.float32) example_outputs[i] = np.asarray(labels, dtype=np.int32) return example_inputs, example_outputs
def make_pair_iter(self, rel): rel_set = {} pair_list = [] for label, d1, d2 in rel: if d1 not in rel_set: rel_set[d1] = {} if label not in rel_set[d1]: rel_set[d1][label] = [] rel_set[d1][label].append(d2) while True: rel_set_sample = random.sample(rel_set.keys(), self.config['query_per_iter']) for d1 in rel_set_sample: label_list = sorted(rel_set[d1].keys(), reverse = True) for hidx, high_label in enumerate(label_list[:-1]): for low_label in label_list[hidx+1:]: for high_d2 in rel_set[d1][high_label]: for low_d2 in rel_set[d1][low_label]: pair_list.append( (d1, high_d2, low_d2) ) yield pair_list
def test_get_content_ids__fills_in_non_cached_content_up_to_pagination_amount(self): with patch.object(self.stream, "get_cached_content_ids") as mock_cached: cached_ids = random.sample(range(10000, 100000), self.stream.paginate_by - 1) throughs = dict(zip(cached_ids, cached_ids)) mock_cached.return_value = cached_ids, throughs # Fills up with one of the two that are available all_ids = set(cached_ids + [self.site_content.id]) self.assertEqual(set(self.stream.get_content_ids()[0]), all_ids)
def add_random_feature_points(self, region_x, region_y, num_points): """Add a set number of randomly placed feature points in a region. Args: region_x (int): X coordinate of center point of desired region. region_y (int): Y coordinate of center point of desired region. num_points (int): Number of feature points to create. """ pnts = self.get_region(region_x, region_y) chosen_indices = random.sample(range(0, len(pnts)), num_points) chosen_feat_points = [pnts[i] for i in chosen_indices] for x, y in chosen_feat_points: self.add_feature_point(region_x, region_y, x, y)
def create_state(board_file, brains): lines = [line.rstrip('\n') for line in board_file] size = max(len(lines), max(len(line) for line in lines)) state = RoboRallyGameState(size) for row in range(len(lines)): for col in range(len(lines[row])): char = lines[row][col] cell = state.board.get_item((row, col)) if char == '#': cell.content = make_wall() elif char == '<': cell.content = make_mounted(WEST) elif char == '>': cell.content = make_mounted(EAST) elif char == 'v': cell.content = make_mounted(SOUTH) elif char == '^': cell.content = make_mounted(NORTH) elif char == '&': cell.floor = None elif char == '{': cell.floor = LEFT_SPINNER elif char == '}': cell.floor = RIGHT_SPINNER elif char.isdecimal(): cell.floor = FLAG + char state.flags[int(char) - 1] = (row, col) state.brains = brains empty_cells = [cell for cell, pos in state.board.traverse() if not cell.content and cell.floor == EMPTY] for cell, brain in zip(random.sample(empty_cells, len(brains) * NUM_ROBOTS_PER_BRAIN), brains * NUM_ROBOTS_PER_BRAIN): cell.content = make_robot(brain, random.choice([NORTH, WEST, EAST, SOUTH])) state.calculate_statistics() return state
def bpr_loss(x, dynamic_user, item_embedding, config): ''' bayesian personalized ranking loss for implicit feedback parameters: - x: batch of users' baskets - dynamic_user: batch of users' dynamic representations - item_embedding: item_embedding matrix - config: model configuration ''' nll = 0 ub_seqs = [] for u,du in zip(x, dynamic_user): du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item nll_u = [] # nll for user for t, basket_t in enumerate(u): if basket_t[0] != 0 and t != 0: pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t) # Sample negative products neg = [random.choice(range(1, config.num_product)) for _ in range(len(basket_t))] # replacement # neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg) # Score p(u, t, v > v') score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx] #Average Negative log likelihood for basket_t nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score))) nll += torch.mean(torch.cat(nll_u)) return nll
def _populate_mines(self): """ Method _populate_mines populates the cells of this minefield with mines. Applies a random selection of simple constraints to where mines may be placed, though about half of the mines are purely randomly placed. """ if self.mine_count is None: self.mine_count = int(0.15 * (self.height * self.width)) count = self.mine_count selectionfuncs = [ lambda y: not (y % 2), lambda y: bool(y % 2), lambda y: not (y % 3), ] yconstraint, xconstraint = random.sample(selectionfuncs * 2, 2) for x in range(count): while True: rx, ry = random.randint(0, self.width - 1), random.randint( 0, self.height - 1) if random.randint(0, 1): if yconstraint(ry): continue if xconstraint(rx): continue c = self.board[rx][ry] if c.contents == Contents.empty: c.contents = Contents.mine break