1

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.

iacob
  • 20,084
  • 6
  • 92
  • 119
Gilgamesh1401
  • 49
  • 2
  • 2
  • 9
  • Does this answer your question? [Get a random sample with replacement](https://stackoverflow.com/questions/43281886/get-a-random-sample-with-replacement) – iacob Mar 28 '21 at 11:42

1 Answers1

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']
iacob
  • 20,084
  • 6
  • 92
  • 119
  • @Gilgamesh1401 Yes, `random` is part of the python standard libraries: https://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` – iacob Jun 02 '18 at 17:21
  • 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 Jun 02 '18 at 17:58
  • @Gilgamesh1401 have edited it to work with `n` larger than the set of elements. – iacob Jun 02 '18 at 20:39
  • @Gilgamesh1401 Did this solve your problem? – iacob Mar 28 '21 at 11:18