2

Recently been discovering the numpy package in Python. Is anyone familar with random dataset generation? For floats I use

FOOBAR = (np.random.normal(mean_desired,stdev,N-size_target of_population), dim_of_array)

It works pretty well but not sure how to setup a random string generator for lets say a a set of strings like these: "GK", "A", "M", D" and populate the dataset with these randomly.

1

1 Answer 1

1

You can use random.choices to sample with replacement:

import random

pop = ["GK", "A", "M", "D"]
random_sample = random.choices(pop, k=10)
random_sample
>>> ['D', 'A', 'A', 'GK', 'M', 'D', 'M', 'A', 'GK', 'GK']
Sign up to request clarification or add additional context in comments.

4 Comments

@Gilgamesh1401 Yes, random is part of the python standard libraries: docs.python.org/3/library if for some reason you don't have it, you can install it like so in the command line pip install random
The issue i have with this code is that i get an error "ValueError: Sample larger than population or is negative". I do not want to sample a population. I want to generate a population in the first place... Here by going n=1 i will only select one of the 4 strings. I wanto to repeat the elements in order to generate a n=8000.
@Gilgamesh1401 have edited it to work with n larger than the set of elements.
@Gilgamesh1401 Did this solve your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.