Python's random module has random.choices for sampling with replacement and random.sample for sampling without replacement. Although random.choices accepts a numpy array and returns a list of randomly selected elements with respect to the first dimension, random.sample raises "TypeError: Population must be a sequence or set. For dicts, use list(d)." On the other hand, random.choices will not accept sets, raising "TypeError: 'set' object does not support indexing."
What I'm curious about is if this is an oversight or if there's an essential reason for restricting random.sample to sequences and sets while random.choices is restricted to objects supporting indexing, despite the functions having very similar purposes.
P.S. if anyone is wondering about how to sample an ndarray, numpy.random.choice samples 1darrays both with and without replacement, and higher-dimension arrays can be effectively sampled with respect to any dimension with advanced indexing where the indices for that dimension are generated with numpy.random.choice
Aucun commentaire:
Enregistrer un commentaire