• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python random.shuffle函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中random.shuffle函数的典型用法代码示例。如果您正苦于以下问题:Python shuffle函数的具体用法?Python shuffle怎么用?Python shuffle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了shuffle函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:yizhikong,项目名称:Illustration_Classification,代码行数:33,代码来源:trainSVM.py


示例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]
开发者ID:ztq09290929,项目名称:AnnTest,代码行数:29,代码来源:network.py


示例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
开发者ID:ackbarr,项目名称:script.cinema.experience,代码行数:26,代码来源:xbmcscript_trivia.py


示例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)
开发者ID:AlexisMignon,项目名称:pandas,代码行数:33,代码来源:test_sorting.py


示例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)
开发者ID:star013,项目名称:timit,代码行数:26,代码来源:rc3e3.py


示例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
开发者ID:Plabo1028,项目名称:ML_NTU_HW,代码行数:27,代码来源:final_0119.py


示例7: shuffle

	def shuffle(self):
		'''
		Shuffles the cards
			Args: None
			Returns: None
		'''
		random.shuffle(self.cards)
开发者ID:Skrelan,项目名称:demoCode,代码行数:7,代码来源:black_jack.py


示例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()
开发者ID:tacaswell,项目名称:awj,代码行数:30,代码来源:awj.py


示例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
开发者ID:lavarini,项目名称:TPS-HILLCLIMB-PYTHON,代码行数:7,代码来源:tsp.py


示例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)
开发者ID:belaa007,项目名称:Spotter,代码行数:33,代码来源:spotter.py


示例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)
开发者ID:pombredanne,项目名称:kunai-1,代码行数:31,代码来源:gossip.py


示例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)
开发者ID:Forrest-Z,项目名称:self-driving-car,代码行数:30,代码来源:helper.py


示例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
开发者ID:sjspielman,项目名称:dnds_1rate_2rate,代码行数:27,代码来源:function_library.py


示例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
开发者ID:kzielonka,项目名称:evo_ca,代码行数:7,代码来源:sga.py


示例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
开发者ID:komsihon,项目名称:shavida,代码行数:27,代码来源:views.py


示例16: getDataCsv

    def getDataCsv(self, filename, header=True, yValue=False, shuffle=True):
        raw_data = open(filename, "r").read()

        data = []
        yData = []
        ids = []

        offset = (1 if header else 0)
        raw_data = raw_data.split('\n')[offset:]

        if shuffle:
            random.shuffle(raw_data)

        for row in raw_data:
            split_row = row.split(',')
            if len(split_row) < 2:
                continue
            yData.append(int(split_row[1]))
            ids.append(int(split_row[0]))
            curRow = []
            for item in split_row[(2 - yValue):]:
                if 'A' <= item <= 'Z':
                    curRow.append(float(ord(item) - 65))
                else:
                    curRow.append(float(item))
            data.append(curRow)

        return data, yData, ids
开发者ID:tpinetz,项目名称:KaggleContests,代码行数:28,代码来源:CleanDataClass.py


示例17: ProcessLines

def ProcessLines(lines):
	random.shuffle(DEV_NAME_COLORS)

	nlines = ""
	onames = {}

	for line in lines:
		match = re.search(DEV_NAME_PATTERN, line)

		if (match == None):
			continue

		oname = match.group(0)
		oname = oname[1: -1]

		if (not onames.has_key(oname)):
			nname = oname

			for n in DEV_NAME_LIST:
				if (oname.find(n) >= 0):
					nname = n; break

			onames[oname] = (len(onames), nname)

		npair = onames[oname]
		color = (npair[0] < len(DEV_NAME_COLORS) and DEV_NAME_COLORS[ npair[0] ]) or "F00F00"
		nname = npair[1]
		nline = (LOG_LINE_PREFIX % (color, nname)) + line[match.span()[1]: ] + '\n'

		nlines += nline

	return nlines
开发者ID:eXLabT,项目名称:spring,代码行数:32,代码来源:log-colorizer.py


示例18: gen

		def gen(gear):
			charm_list = []
			for (charm, num) in gear:
				charm_list.extend([charm] * num)
			random.shuffle(charm_list)
			for charm in charm_list:
				yield charm
开发者ID:wxyz202,项目名称:Estiah_Battle_Simulator,代码行数:7,代码来源:gear.py


示例19: buildMap

def buildMap(gridSize):
    cells = {}

    # generate a list of candidate coords for cells
    roomCoords = [(x, y) for x in range(gridSize) for y in range(gridSize)]
    random.shuffle(roomCoords)

    roomCount = min(10, int(gridSize * gridSize / 2))
    for i in range(roomCount):
        # search for candidate cell
        coord = roomCoords.pop()

        while not safeToPlace(cells, coord) and len(roomCoords) > 0:
            coord = roomCoords.pop()

        if not safeToPlace(cells, coord):
            break

        width = random.randint(3, CELL_SIZE)
        height = random.randint(3, CELL_SIZE)
        cells[coord] = Room(coord[0], coord[1], width, height)

    grid = Grid()
    grid.rooms = list(cells.values())

    # connect every room to one neighbor
    for coord in cells:
        room = cells[coord]
        room1 = findNearestNeighbor(cells, coord)

        if not grid.connected(room, room1):
            grid.corridors.append(Corridor(room, room1))

    return grid
开发者ID:forestbelton,项目名称:PyRogue,代码行数:34,代码来源:mapGen.py


示例20: get_precision

def get_precision(request):
    t = TrainQueries.objects.all()
    t = list(enumerate(t))

    good = 0
    bad = 0
    random.shuffle(t)

    total = len(t)

    trains = [[el.query,"Added" if el.relevant else "Excluded", 0] for i, el in t[:4 * total/5]]
    tests = [[el.query, el.relevant] for i, el in t[4 * total/5: ]]

    words = old_extract_data(trains)

    for test in tests:


        if (old_rsv.calculateRsv(test[0], words) > 1.6):
            if test[1]:
                good += 1
            else:
                bad += 1
        else:
            if test[1]:
                bad += 1
            else:
                good += 1

    return HttpResponse(str(good/float(len(tests))))
开发者ID:alwaysprep,项目名称:AdScope,代码行数:30,代码来源:views.py



注:本文中的random.shuffle函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python random.triangular函数代码示例发布时间:2022-05-26
下一篇:
Python random.setstate函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap