Generating random data - python

I'm trying to generate random data for 'ACDEFGHIKLMNPQRSTVWY' in a 3-mer form (like, AEF) using following script, but in the outputs I have gotten many similar 3-mers. Would you please advise me on how to do, not to get similar results or remove the same 3-mer?
Thanks in advance,
Berk
import random
def random_AA_seq(length):
return ''.join(random.choice('ACDEFGHIKLMNPQRSTVWY') for i in range(length))
list_size = 10000
lengths = []
for j in range(list_size):
a = int(random.normalvariate(3, 0))
print random_AA_seq(a)

To remove the same items, just make it a set:
print set(random_AA_seq(a))
To get all possible permutations, you could also use itertools...
from itertools import permutations
length = 3
print permutations('ACDEFGHIKLMNPQRSTVWY', length)
... and pick your 3-mers randomly afterwards.

Per comments:
alphabet = "ACDEFGHIKLMNPQRSTVWY"
all_trimers = map("".join, itertools.product(* [alphabet] * 3))
a_few_distinct_trimers = random.sample(all_trimers, 42)

Just
''.join(random.choice(string.ascii_uppercase) for _ in range(3))
should be fine

Updated answer, the following script will return a list of 3mers of the length required. Each 3mer will occur in the list once:
import random
def random_3mers(length):
seqs = set()
while len(seqs) < length:
seqs.add("".join(random.sample("ACDEFGHIKLMNPQRSTVWY", 3)))
lseqs = list(seqs)
random.shuffle(lseqs)
return lseqs
for three_mer in random_3mers(10):
print three_mer
For a length of 10, the following type of output will be displayed:
MKY
KWV
PRY
WKQ
YGI
ANQ
GFL
RQE
SCN
GRY

Related

make a nested list using a for loop and random numbers

I am looking to make a nested list. In each list I will have random numbers generated from 1 - 80 and 1000 lists. I have the random number part down, but can not make the nested list. this is what I have.
def lots_of_draws ():
numbers_to_draw_from = list(range(1, 81))
random_draws = sorted(random.sample(numbers_to_draw_from, k=20,))
random_draws_list = []
time_ = 0
while time_ > 100:
random_draws_list.append(random_draws)
time_+=1
print(time_)
print(random_draws_list)
lots_of_draws()
expected [[1,2,3,4,5,6,7,8,9,11,12,13,20,50,45,58,32,78,80,16],[another 20],[anoter 20],and so on for 100, or 1000]
I don't know what I doing wrong, any help would be great. Thanks
The following code will also fix your problem.
from random import randint
def lots_of_draws():
main_list = []
for i in range(1000):
nested_list = []
for i in range(20):
nested_list.append(randint(0,80))
main_list.append(nested_list)
return main_list
First issue is that your while loop will never run. You probably want while time_ < 100 instead. You'll still not get the expected result after fixing that though, because on each iteration, you're appending the same list to random_draws_list. random_draws is only created once.
Possible fix:
def draw_n(numbers, n):
return sorted(random.sample(numbers, k=n))
def lots_of_draws():
numbers_to_draw_from = range(1, 81)
random_draws_list = []
for time_ in range(100):
random_draws_list.append(draw_n(numbers_to_draw_from, 20))
print(time_)
print(random_draws_list)

Given a set of permutations return one specific number

I want your help to catch an specific number or any number that I asked for in bool_check in this case that number is "4321" so i've been struggling to get True and also I want the program to show me that specific number from this set of permutations.
For example, I want the output to say:
True
4321
So far i got the permutations and the functions but i dont know how to connect everything. Thank you for your time.
from itertools import product
def bool_check(x):
if x == "4321":
return True
else:
return False
def find_key(diferent_dig, quantity_dig):
chars = list(diferent_dig)
perms = []
for c in product(chars, repeat = quantity_dig):
perms.append(c)
return perms
print(find_key("1234", 4))
product returns a tuple of the items, but you are searching for a string.
Eg. (1, 2, 3, 4) instead of 1234
I am not too sure what you are after but maybe something like this will help:
from itertools import product
def find_key(diferent_dig, quantity_dig):
chars = list(diferent_dig)
perms = []
for c in product(chars, repeat = quantity_dig):
perms.append(''.join(c)) # Convert from tuple to String
return perms
print("1234" in find_key("1234", 4)) # returns True
print("5555" in find_key("1234", 4)) # returns False

I cannot catch index from random.shuffle method

I cannot catch index from random.shuffle method.
An error happens
TypeError: %d format: a number is required, not list
My code is
if __name__ =="__main__":
bell_nums = range(1,6)
pairs = list(itertools.combinations(bell_nums,2))
for pair in pairs:
bell_num1=int(pair[0])
bell_num2 = int(pair[1])
train_data = np.empty((0,12),float)
train_label = np.array([])
test_data = np.empty((0,12),float)
test_label = np.array([])
noise_nums = list(range(1,12))
level_nums = list(range(0,10))
random.shuffle(noise_nums)
nfft=2048
nceps = 12
for noise_nums_index in noise_nums[0:10]:
random.shuffle(level_nums)
files_name = glob.glob("learning_sample/%d_%d_%d.wav" % (bell_num1,noise_nums_index,level_nums))
for file_name in files_name:
feature = get_feature(files_name,nfft,nceps)
if len(train_data) ==0:
train_data=feature
else:
train_data=np.vstack((train_data,feature))
train_label=np.append(train_label,bell_num1)
files_name="learning_sample/%d_%d_%d.wav"% (bell_num1,noise_num,level_nums[8])
feature = get_feature(file_name,nfft,nceps)
if len(test_data) ==0:
test_data=feature
else:
test_data=np.vstack((test_data,feature))
test_label=np.append(test_label,bell_num1)
I think level_nums is list type,so this error happen.
But I cannot come up with the way to catch index from random.shuffle method in this case.
I wanna name "learning_sample/%d_%d_%d.wav" this file by using noise_nums_index's number and level_nums's number randomly.In this case, how can I do to name this part?How can I write this?Should I use for statement for random.shuffle(level_nums)?
To select a random element in level_nums you should use: random.choice(level_nums)
In your code:
for noise_nums_index in noise_nums[0:10]:
files_name = glob.glob("learning_sample/%d_%d_%d.wav" % (bell_num1,noise_nums_index,random.choice(level_nums)))
Note that since noise_nums is not define in the code you provide I was no able to check the full code. There might be some other errors.
you can use random.randrange method from standard python library.
for example:
random.randrange(1, 12, 1)
or random.choice method:
a = list(range(1,12))
random.choice(a)
level_nums is a list and you are pointing it at a %d format string.
If you are just looking for a random int, and because you are already using numpy have you considered using np.random.choice()? That would remove the need to even use the shuffle method.
>>> np.random.choice(level_nums)
3
>>> np.random.choice(level_nums)
8
Or just the random int function and get rid of level_nums completely
>>> np.random.randint(1, 11)
6

Generating Random Sequence of a string

I have to write a python code which should take two sequences (s1,s2) generate random strings for sequence 2 (s2) for a given number of trials in python and return both strings (s1 fixed, s2 changed) for each trial. For ex:
input seq: [aabcc,aabcc]
Output seq for 4 trials:
Trial1:[aabcc, aabcc]
Trial2:[aabcc, aaabc]
Trial3:[aabcc, aaaaa]
Trial4:[aabcc, ccaab]
It is like generating random sequence from the given input sequence for the given number of trials. Can someone help me code this using basic for and while loops in python? (i.e no built in functions).
import random
# if you can't use random.choice, this is a one-for-one substitute
def random_choice(seq):
"""
Return a random element from a non-empty sequence
"""
return seq[int(random.random() * len(seq))]
def random_string_from_sample(s, length=None):
"""
Return a random string based on the letter-distribution in s
"""
if length is None:
length = len(s)
return ''.join(random_choice(s) for _ in range(length))
def main():
s0, s1 = "aabcc", "aabcc"
trials = 4
for i in range(1, trials+1):
print("Trial{}:[{}, {}]".format(i, s0, random_string_from_sample(s1)))
if __name__=="__main__":
main()
produces:
Trial1:[aabcc, bbaca]
Trial2:[aabcc, cbaac]
Trial3:[aabcc, cacac]
Trial4:[aabcc, caacc]
This solution extracts the unique characters, so the result won't be weighted against the characters with higher frequency in the input. If you don't want that behavior, just remove the conversion to set.
from random import randint
s = "aabbcc"
chars = list(set(s))
nchars = len(chars)
trials = 4
for i in range(trials):
rng_sample = ''.join([chars[randint(0,nchars-1)] for _ in range(len(s))])
print rng_sample
S1 is obviously easy to return. s2 can be turned into a list and shuffled:
s2 ="aabbcc"
import random
h = list(s2)
random.shuffle(h)
newString = ''.join(h)
print (newString)

Mutually exclusive random sampling from a list

input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
I'm having trouble creating X number of lists of size Y without repeating any elements.
What I have been doing is using:
x = 3
y = 2
import random
output = random.sample(input, y)
# ['mordor', 'thingol']
but if I repeat this then I will have repeats.
I would like the output to be something like
[['mordor', 'thingol'], ['melian', 'hithlum'], ['beleriand', 'eol']]
since I chose x = 3 (3 lists) of size y = 2 (2 elements per list).
def random_generator(x,y):
....
You can simply shuffle the original list and then generate n groups of m elements successively from it. There may be fewer or more than that number of groups possible. Note thatinputis the name of a Python built-in function, so I renamed itwords.
import itertools
from pprint import pprint
import random
def random_generator(seq, n, m):
rand_seq = seq[:] # make a copy to avoid changing input argument
random.shuffle(rand_seq)
lists = []
limit = n-1
for i,group in enumerate(itertools.izip(*([iter(rand_seq)]*m))):
lists.append(group)
if i == limit: break # have enough
return lists
words = ['beleriand', 'mordor', 'hithlum', 'eol', 'morgoth', 'melian', 'thingol']
pprint(random_generator(words, 3, 2))
Output:
[('mordor', 'hithlum'), ('thingol', 'melian'), ('morgoth', 'beleriand')]
It would be more Pythonic to generate the groups iteratively. The above function could easily be turned into generator by making ityieldeach group, one-by-one, instead of returning them all in a relatively much longer list-of-lists:
def random_generator_iterator(seq, n, m):
rand_seq = seq[:]
random.shuffle(rand_seq)
limit = n-1
for i,group in enumerate(itertools.izip(*([iter(rand_seq)]*m))):
yield group
if i == limit: break
pprint([group for group in random_generator_iterator(words, 3, 2)])
rather than randomly taking two things from your list, just randomize your list and iterate through it to create your new array of the dimensions you specify!
import random
my_input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
def random_generator(array,x,y):
random.shuffle(array)
result = []
count = 0
while count < x:
section = []
y1 = y * count
y2 = y * (count + 1)
for i in range (y1,y2):
section.append(array[i])
result.append(section)
count += 1
return result
print random_generator(my_input,3,2)
You could use random.sample in combination with the itertools.grouper recipe.
input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
import itertools
import random
def grouper(iterable,group_size):
return itertools.izip(*([iter(iterable)]*group_size))
def random_generator(x,y):
k = x*y
sample = random.sample(input,k)
return list(grouper(sample,y))
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
for one run, this results in:
[('melian', 'mordor'), ('hithlum', 'eol'), ('thingol', 'morgoth')]
[('hithlum', 'thingol'), ('mordor', 'beleriand'), ('morgoth', 'eol')]
[('morgoth', 'beleriand'), ('melian', 'thingol'), ('hithlum', 'mordor')]
[('beleriand', 'thingol'), ('melian', 'hithlum'), ('eol', 'morgoth')]
[('mordor', 'hithlum'), ('eol', 'beleriand'), ('melian', 'morgoth')]
[('mordor', 'melian'), ('thingol', 'beleriand'), ('morgoth', 'eol')]
And the next run:
[('mordor', 'thingol'), ('eol', 'hithlum'), ('melian', 'beleriand')]
[('eol', 'beleriand'), ('mordor', 'melian'), ('hithlum', 'thingol')]
[('hithlum', 'mordor'), ('thingol', 'morgoth'), ('melian', 'eol')]
[('morgoth', 'eol'), ('mordor', 'thingol'), ('melian', 'beleriand')]
[('melian', 'morgoth'), ('mordor', 'eol'), ('thingol', 'hithlum')]
[('mordor', 'morgoth'), ('hithlum', 'thingol'), ('eol', 'melian')]

Categories

Resources