random

Select randomly x files in subdirectories

怎甘沉沦 提交于 2020-05-16 18:16:03
问题 I need to take exactly 10 files (images) in a dataset randomly, but this dataset is hierarchically structured. So I need that for each subdirectory that contains images hold just 10 of them randomly. Is there an easy way to do that or I should do it manually? def getListOfFiles(dirName): ### create a list of file and sub directories ### names in the given directory listOfFile = os.listdir(dirName) allFiles = list() ### Iterate over all the entries for entry in listOfFile: ### Create full path

Generating DNA codon combinations in R

好久不见. 提交于 2020-05-16 04:27:09
问题 I am generating random DNA sequences in R where each sequence is of a set length and contains a user-specified distribution of nucleotides. What I want to be able to do is ensure certain runs of nucleotides are NOT generated in a given sequence. The runs that are disallowed are: "aga", "agg", "taa", "tag" and "tga". Here is my code that simply generates sequences where the above runs MAY occur. I am unsure how best to modify the code to account for the "tabu" runs specified above. library(ape

Ranger Predicted Class Probability of each row in a data frame

柔情痞子 提交于 2020-05-15 21:31:40
问题 With regard to this link Predicted probabilities in R ranger package, I have a question. Imagine I have a mixed data frame, df (comprising of factor and numeric variables) and I want to do classification using ranger. I am splitting this data frame as test and train sets as Train_Set and Test_Set. BiClass is my prediction factor variable and comprises of 0 and 1 (2 levels) I want to calculate and attach class probabilities to the data frame using ranger using the following commands: Biclass

Random integers from an exponential distribution between min and max

[亡魂溺海] 提交于 2020-05-15 11:03:40
问题 I would like to generate random integers on an interval min to max. For a uniform distribution in numpy: numpy.random.randint(min,max,n) does exactly what I want. However, I would now like to give the distribution of random numbers an exponential bias. There are a number of suggestions for this e.g. Pseudorandom Number Generator - Exponential Distribution as well as the numpy function numpy.random.RandomState.exponential , but these do not address how to constrain the distribution to integers

Laravel : How to get random image from directory?

梦想与她 提交于 2020-05-15 03:58:08
问题 I have a directory containing the sub-directory, in each sub-directory there are images. I want to display the images randomly. Below my code in php that works well, but it does not work in Laravel, problem is with opendir() and readdir() . view blade <?php $folder = opendir('images/'); $i = 0; while(false !=($file = readdir($folder))){ if($file != "." && $file != ".."){ $images[$i]= $file; $i++; } } $random_img=rand(0,count($images)-1); ?> <div> <?php echo '<img src="images/'.$images[$random

Best way to revert to a random seed after temporarily fixing it?

霸气de小男生 提交于 2020-05-15 03:54:24
问题 Is this the only way to 'unseed' the random number generator: np.random.seed(int(time.time())) If you have some code that you want to be repeatable (e.g. a test) in a loop with other code that you want to be random each loop, how do you 'reset' the seed to random number generator after setting it? The following code illustrates the issue: import numpy as np def test(): np.random.seed(2) print("Repeatable test:", [np.random.randint(10) for i in range(3)]) for i in range(4): print("Random

How to specify a random seed while using Python's numpy random choice?

青春壹個敷衍的年華 提交于 2020-05-15 02:07:15
问题 I have a list of four strings. Then in a Pandas dataframe I want to create a variable randomly selecting a value from this list and assign into each row. I am using numpy's random choice, but reading their documentation, there is no seed option. How can I specify the random seed to the random assignment so every time the random assignment will be the same? service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L'] df['SERVICE_CODE'] = [np.random.choice(service_code_options ) for i in

How to specify a random seed while using Python's numpy random choice?

不羁岁月 提交于 2020-05-15 02:07:03
问题 I have a list of four strings. Then in a Pandas dataframe I want to create a variable randomly selecting a value from this list and assign into each row. I am using numpy's random choice, but reading their documentation, there is no seed option. How can I specify the random seed to the random assignment so every time the random assignment will be the same? service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L'] df['SERVICE_CODE'] = [np.random.choice(service_code_options ) for i in

How to choose keys from a python dictionary based on weighted probability?

天涯浪子 提交于 2020-05-14 16:32:58
问题 I have a Python dictionary where keys represent some item and values represent some (normalized) weighting for said item. For example: d = {'a': 0.0625, 'c': 0.625, 'b': 0.3125} # Note that sum([v for k,v in d.iteritems()]) == 1 for all `d` Given this correlation of items to weights, how can I choose a key from d such that 6.25% of the time the result is 'a', 32.25% of the time the result is 'b', and 62.5% of the result is 'c'? 回答1: def weighted_random_by_dct(dct): rand_val = random.random()

How to choose keys from a python dictionary based on weighted probability?

主宰稳场 提交于 2020-05-14 16:32:09
问题 I have a Python dictionary where keys represent some item and values represent some (normalized) weighting for said item. For example: d = {'a': 0.0625, 'c': 0.625, 'b': 0.3125} # Note that sum([v for k,v in d.iteritems()]) == 1 for all `d` Given this correlation of items to weights, how can I choose a key from d such that 6.25% of the time the result is 'a', 32.25% of the time the result is 'b', and 62.5% of the result is 'c'? 回答1: def weighted_random_by_dct(dct): rand_val = random.random()