本文整理汇总了Python中random.shuffle函数的典型用法代码示例。如果您正苦于以下问题:Python shuffle函数的具体用法?Python shuffle怎么用?Python shuffle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shuffle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: loadData
def loadData():
#filenames = os.listdir(os.getcwd())
filenames = [dataFile]
for filename in filenames:
if 'txt' in filename and 'sum' not in filename:
f = open(filename)
lines = f.readlines()
f.close()
random.shuffle(lines)
data = []
label = []
for i in range(len(lines)):
line = lines[i][:]
lines[i] = ''
pos = line.find(' ')
if pos < 0:
continue
line = line[pos+1 :].strip()
spLine = line.split(' ')
if int(spLine[-1]) < 100:
spLine[-1] = 0
elif int(spLine[-1]) > 100:
spLine[-1] = 1
else:
continue
data.append(spLine[:-1])
label.append(spLine[-1])
print 'array...'
data = np.array(data, dtype = float)
label = np.array(label, dtype = int)
print 'score...'
weight = getWeight(label)
return (data, label, weight)
示例2: SGD
def SGD(self, training_data, epochs, mini_batch_size, eta,
test_data=None, weight_decay = 0.0):
"""Train the neural network using mini-batch stochastic
gradient descent. The ``training_data`` is a list of tuples
``(x, y)`` representing the training inputs and the desired
outputs. The other non-optional parameters are
self-explanatory. If ``test_data`` is provided then the
network will be evaluated against the test data after each
epoch, and partial progress printed out. This is useful for
tracking progress, but slows things down substantially."""
if test_data: n_test = len(test_data)
n = len(training_data)
for j in xrange(epochs):
random.shuffle(training_data)
mini_batches = [
training_data[k:k+mini_batch_size]
for k in xrange(0, n, mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta , weight_decay)
if test_data:
n_correct = float(self.evaluate(test_data))
print "Epoch {0}: {1} / {2}".format(
j,n_correct , n_test)
self.test_accuracy.append(float('%.4f'%(n_correct/n_test)))
else:
print "Epoch {0} complete".format(j)
self.train_costs.append(self.cost_val(training_data))
print "Epoch {0}: cost = ".format(j),self.train_costs[-1]
示例3: _shuffle_slides
def _shuffle_slides( self ):
# randomize the groups and create our play list
shuffle( self.tmp_slides )
# now create our final playlist
print "-----------------------------------------"
# loop thru slide groups and skip already watched groups
for slides in self.tmp_slides:
# has this group been watched
if ( not self.settings[ "trivia_unwatched_only" ] or ( slides[ 0 ] and xbmc.getCacheThumbName( slides[ 0 ] ) not in self.watched ) or
( slides[ 1 ] and xbmc.getCacheThumbName( slides[ 1 ] ) not in self.watched ) or
( slides[ 2 ] and xbmc.getCacheThumbName( slides[ 2 ] ) not in self.watched ) ):
# loop thru slide group only include non blank slides
for slide in slides:
# only add if non blank
if ( slide ):
# add slide
self.slide_playlist += [ slide ]
print "included - %s, %s, %s" % ( os.path.basename( slides[ 0 ] ), os.path.basename( slides[ 1 ] ), os.path.basename( slides[ 2 ] ), )
else:
print "----------------------------------------------------"
print "skipped - %s, %s, %s" % ( os.path.basename( slides[ 0 ] ), os.path.basename( slides[ 1 ] ), os.path.basename( slides[ 2 ] ), )
print "----------------------------------------------------"
print
print "total slides selected: %d" % len( self.slide_playlist )
print
示例4: test_sort_index_multicolumn
def test_sort_index_multicolumn(self):
import random
A = np.arange(5).repeat(20)
B = np.tile(np.arange(5), 20)
random.shuffle(A)
random.shuffle(B)
frame = DataFrame({'A': A, 'B': B,
'C': np.random.randn(100)})
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['A', 'B'])
result = frame.sort_values(by=['A', 'B'])
indexer = np.lexsort((frame['B'], frame['A']))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['A', 'B'], ascending=False)
result = frame.sort_values(by=['A', 'B'], ascending=False)
indexer = np.lexsort((frame['B'].rank(ascending=False),
frame['A'].rank(ascending=False)))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['B', 'A'])
result = frame.sort_values(by=['B', 'A'])
indexer = np.lexsort((frame['A'], frame['B']))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
示例5: loadArray
def loadArray(dirpath):
# pattern = regex = str variable = '.+\.label' (recommended)
pattern = '.+\.label'
# another = 'array' (recommended)
another = 'array'
names = os.listdir(dirpath)
random.shuffle(names)
for name in names:
if re.match(pattern,name) != None:
#print name
folder,prename,num,suffix = name.split('.')
target = folder + '.' + prename + '.' + num + '.' + another
targetpath = dirpath + '/' + target
# find another suffix data file
# meanwhile examine the num, length of spectrogram = length of label
if os.path.exists(targetpath):
# extract object from a file
with file(target,'rb') as f:
spectroArray = cPickle.load(f)
# GPU default type is float32
spectroArray = np.float32(spectroArray)
with file(name,'rb') as f:
labelArray = cPickle.load(f)
# label should be int type
labelArray = np.int32(labelArray)
yield spectroArray,labelArray,int(num)
示例6: shuffle_val
def shuffle_val(X,y,ratio):
data = []
data_size = X.shape[0]
feature_size = X.shape[1]
train_data_size = int(data_size * ratio)
for i in range(data_size):
tmp_X = X[i]
one_line = np.concatenate((tmp_X,y[i]))
data.append(one_line)
random.shuffle(data)
split_index = [0,int(data_size*ratio),data_size]
X = np.zeros((data_size,feature_size))
y = np.zeros((data_size,1))
for i in range(data_size):
X[i] = data[i][:feature_size]
y[i] = data[i][feature_size]
X_train = np.array(X[split_index[0]:split_index[1]])
X_val = np.array(X[split_index[1]:split_index[2]])
y_train = np.array(y[split_index[0]:split_index[1]])
y_val = np.array(y[split_index[1]:split_index[2]])
return X_train,y_train,X_val,y_val
示例7: shuffle
def shuffle(self):
'''
Shuffles the cards
Args: None
Returns: None
'''
random.shuffle(self.cards)
示例8: __init__
def __init__(self, cache_path, *, max_size=None):
self._cache_path = cache_path
self.max_size = max_size
# convert to bytes
if self.max_size is not None:
self.max_size *= 1048576
# TODO 2k compat
os.makedirs(cache_path, exist_ok=True)
self._fn_cache = dict()
self._sz_cache = dict()
# TODO replace this with a double linked list like boltons LRU
self._heap_map = dict()
self._heap = []
# put files in to heap in random order
files = glob(os.path.join(self._cache_path, '*feather'))
shuffle(files)
for fn in files:
key = self._key_from_filename(fn)
self._fn_cache[key] = fn
stat = os.stat(fn)
self._sz_cache[key] = stat.st_size
heap_entry = [time.time(), key]
heapq.heappush(self._heap, heap_entry)
self._heap_map[key] = heap_entry
# prune up front just in case
self.__prune_files()
示例9: choose_next_neighbour
def choose_next_neighbour(routes_choices, chosen, city):
neighbours=list(routes_choices[city]-chosen)
if len(neighbours):
random.shuffle(neighbours)
count, neighbour=min((len(routes_choices[n]), n) for n in neighbours)
return neighbour
return None
示例10: fping
def fping(ips):
# IP tomb betoltese
rv = loads(ips)
# IP cimek osszekeverese
shuffle(rv)
# tomeges pingeleshez hasznalt fping parancs parameterezese
array = ['fping', '-e']
# tomeges pingeleshez hasznalt ipcimek hozzaadasa a parancshoz
for x in rv:
array.append(x)
# tomeges pingeles lefuttatasa csovezetekkel visszaterve
p1 = subprocess.Popen(array, stdout=subprocess.PIPE)
(pings, err) = p1.communicate()
#output={}
output = []
pings_arr = pings.split('\n')
for i in range(len(rv)):
tdict={}
pings_line = pings_arr[i].split(' ')
tdict["ip"]=pings_line[0]
tdict["avg"]= (pings_line[3])[1:]
#output[pings_line[0]] = (pings_line[3])[1:]
output.append(tdict)
示例11: join
def join(self):
logger.log("We will try to join our seeds members", self.seeds, part='gossip')
tmp = self.seeds
others = []
if not len(self.seeds):
logger.log("No seeds nodes, I'm a bootstrap node?")
return
for e in tmp:
elts = e.split(':')
addr = elts[0]
port = self.port
if len(elts) > 1:
port = int(elts[1])
others.append( (addr, port) )
random.shuffle(others)
while True:
logger.log('JOINING myself %s is joining %s nodes' % (self.name, others), part='gossip')
nb = 0
for other in others:
nb += 1
r = self.do_push_pull(other)
# Do not merge with more than KGOSSIP distant nodes
if nb > KGOSSIP:
continue
# If we got enough nodes, we exit
if len(self.nodes) != 1 or self.interrupted or self.bootstrap:
return
# Do not hummer the cpu....
time.sleep(0.1)
示例12: get_batches_fn
def get_batches_fn(batch_size):
"""
Create batches of training data
:param batch_size: Batch Size
:return: Batches of training data
"""
image_paths = glob(os.path.join(data_folder, 'image_2', '*.png'))
label_paths = {
re.sub(r'_(lane|road)_', '_', os.path.basename(path)): path
for path in glob(os.path.join(data_folder, 'gt_image_2', '*_road_*.png'))}
background_color = np.array([255, 0, 0])
random.shuffle(image_paths)
for batch_i in range(0, len(image_paths), batch_size):
images = []
gt_images = []
for image_file in image_paths[batch_i:batch_i+batch_size]:
gt_image_file = label_paths[os.path.basename(image_file)]
image = scipy.misc.imresize(scipy.misc.imread(image_file), image_shape)
gt_image = scipy.misc.imresize(scipy.misc.imread(gt_image_file), image_shape)
gt_bg = np.all(gt_image == background_color, axis=2)
gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)
images.append(image)
gt_images.append(gt_image)
yield np.array(images), np.array(gt_images)
示例13: add_bias_to_fitness
def add_bias_to_fitness(rawfitness, bias):
'''
Derive new fitness values which incorporate codon bias.
'''
new_fitness = np.zeros(61)
for i in range(len(genetic_code)):
# Determine the new preferred, non-preferred frequencies
family = genetic_code[i]
aa_fit = rawfitness[ codons.index(genetic_code[i][0]) ]
k = len(family) - 1.
nonpref = abs(aa_fit) * bias * -1 # Reduce fitness by 50-100%
pref = deepcopy(aa_fit)
# Assign randomly
indices = [codons.index(x) for x in family]
shuffle(indices)
first = True
for ind in indices:
if first:
new_fitness[ind] = pref
first=False
else:
new_fitness[ind] = nonpref
return new_fitness
示例14: random_population
def random_population(self, k):
population = []
for i in xrange(0, k):
x = range(0, self.instance.solution_size())
random.shuffle(x)
population.append(x)
return population
示例15: get_context_data
def get_context_data(self, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
member = self.request.user
recommended_items = []
if member.is_authenticated():
for item in get_all_recommended(member, 12):
if isinstance(item, Movie):
item.type = 'movie'
else:
item.type = 'series'
size = 0
episodes = SeriesEpisode.objects.filter(series=item)
for episode in episodes:
size += episode.size
item.size = size
recommended_items.append(item)
if len(recommended_items) < Movie.MIN_RECOMMENDED:
additional = Movie.MIN_RECOMMENDED - len(recommended_items)
additional_items = Movie.objects.all().order_by('-release')[:additional]
recommended_items.append(additional_items)
context['items'] = recommended_items
context['recommended_items'] = as_matrix(recommended_items, 4)
recent_releases = list(Movie.objects.all().order_by('-release', '-id')[:Movie.MAX_RECENT])
shuffle(recent_releases)
sample_media = recent_releases[0]
context['fb_share_item'] = sample_media
return context