+2 votes
in Programming Languages by (29.7k points)
I want to randomly sample some elements from a big list with or without replacement. Is there any Python function that I can use?

1 Answer

+2 votes
by (51.1k points)
edited by

You can use the resample() function of the scikit-learn module. The function resamples arrays or sparse matrices in a consistent way. You can specify the "number of samples" and "with or without replacement" as parameters.

sklearn.utils.resample(*arrays, replace=True, n_samples=None, random_state=None, stratify=None)

 Here is an example using "with replacement":

from sklearn.utils import resample
y = [1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1]
y_sampled = resample(y, replace=True, n_samples=5)
print(y_sampled)
 

 Here is an example using "without replacement":

from sklearn.utils import resample
y = [1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1]
y_sampled = resample(y, replace=False, n_samples=5)
print(y_sampled)


...