Gilgamesh1401
Gilgamesh1401

Reputation: 59

Generate random dataset of string values with Python3 and NumPy

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.

Upvotes: 1

Views: 361

Answers (1)

iacob
iacob

Reputation: 24181

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']

Upvotes: 1

Related Questions