+2 votes
in Programming Languages by (73.8k 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 (349k points)
selected by
 
Best answer

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)

Related questions


...