There is a program that needs to get 100 samples from 1000 randomly generated number distributed in [1, 500]
. How can do I get a random sample from the output?
I wrote the following:
N = 1000
x = 1 + 500 * np.random.rand(N)
sp_x = random.sample(x, 100)
I am getting an error:
TypeError: Population must be a sequence or set. For dicts, use list(d).
From your code, the resulting x
is a numpy array.
To check type(x)
output: numpy.ndarray
But the function random.sample(sequence, k)
can only take in a sequence that is a list
, tuple
, string
, or set
. So your code could be:
import random
N=1000
x = 1+500*np.random.rand(N)
x = list(x)
sp_x = random.sample(x,100)
print(len(sp_x))
output: 100
The standard library random
module, and in particular random.sample
, is not really intended to work with numpy. Since you're using arrays, stick with numpy routines since they are generally much more efficient.
To generate the first array more simply, use np.random.uniform
:
x = np.random.uniform(1, 500, size=N)
You can sample the 100 items in a number of ways in numpy. A very basic method is to apply np.random.shuffle
to x
and slice off the first 100 elements:
np.random.shuffle(x)
sp_x = x[:100]
Another option is to usenp.random.choice
:
sp_x = np.random.choice(x, 100, replace=False)
All of the above is using the legacy API. The new Generator
API is preferred and generally much more efficient.
gen = np.random.default_rng()
x = gen.uniform(1, 500, size=N)
sp_x = gen.choice(x, 100, replace=False)
Keep in mind that using np.random.rand
, random.uniform
and random.Generator.uniform
selects floating point values on the interval [1, 500)
. If you wanted to generate integers in [1, 500]
instead, do something like
x = np.random.randint(1, 501, size=N)
Or
x = gen.integers(500, endpoint=True, size=N)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With